INSERT
命令用于将数据插入到表的列中。
语法:
INSERT INTO <tablename> (<column1 name>, <column2 name>....) VALUES (<value1>, <value2>....) USING <option>
示例:
在之前的文章中,我们创建一个名为“student
”的表,其中包含列(student_id
, student_fees
, student_name
),需要在student
表中插入一些数据。
我们来看看向“student
”表中插入数据的代码 -
## 创建表 CREATE TABLE student( student_id int PRIMARY KEY, student_name text, student_fees varint ); ## 向表插入数据 INSERT INTO student (student_id, student_fees, student_name) VALUES(1,5000, 'Maxsu'); INSERT INTO student (student_id, student_fees, student_name) VALUES(2,3000, 'Minsu'); INSERT INTO student (student_id, student_fees, student_name) VALUES(3, 2000, 'Modlee');
在执行上面语句插入数据后,可以使用SELECT命令验证是否成功插入了数据。
SELECT * FROM student;
执行结果如下所示 -
cqlsh:zyiz_ks> CREATE TABLE student( ... student_id int PRIMARY KEY, ... student_name text, ... student_fees varint ... ); cqlsh:zyiz_ks> cqlsh:zyiz_ks> INSERT INTO student (student_id, student_fees, student_name) ... VALUES(1,5000, 'Maxsu'); cqlsh:zyiz_ks> INSERT INTO student (student_id, student_fees, student_name) ... VALUES(2,3000, 'Minsu'); cqlsh:zyiz_ks> INSERT INTO student (student_id, student_fees, student_name) ... VALUES(3, 2000, 'Modlee'); cqlsh:zyiz_ks> cqlsh:zyiz_ks> SELECT * FROM student; student_id | student_fees | student_name ------------+--------------+-------------- | 5000 | Maxsu | 3000 | Minsu | 2000 | Modlee (3 rows) cqlsh:zyiz_ks>
如下图所示 -