Cassandra插入數據

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>

如下圖所示 -
Cassandra插入數據