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:yiibai_ks> CREATE TABLE student(
... student_id int PRIMARY KEY,
... student_name text,
... student_fees varint
... );
cqlsh:yiibai_ks>
cqlsh:yiibai_ks> INSERT INTO student (student_id, student_fees, student_name)
... VALUES(1,5000, 'Maxsu');
cqlsh:yiibai_ks> INSERT INTO student (student_id, student_fees, student_name)
... VALUES(2,3000, 'Minsu');
cqlsh:yiibai_ks> INSERT INTO student (student_id, student_fees, student_name)
... VALUES(3, 2000, 'Modlee');
cqlsh:yiibai_ks>
cqlsh:yiibai_ks> SELECT * FROM student;
student_id | student_fees | student_name
------------+--------------+--------------
1 | 5000 | Maxsu
2 | 3000 | Minsu
3 | 2000 | Modlee
(3 rows)
cqlsh:yiibai_ks>
如下圖所示 -