当前位置: 首页 > 教程 > Cassandra >

Cassandra插入数据

精华
小牛编辑
185浏览
2023-03-14

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>

如下图所示 -