1、基本操作

  1. SHOW VERSION
  2. SHOW HOST
  3. SHOW SESSION
  4. EXPAND ON/OFF
  5. # 使用system键空间
  6. USE system;

2、键空间

2.1、查看键空间

  1. # 查看所有的键空间
  2. DESCRIBE KEYSPACES
  3. # 查看system键空间
  4. DESCRIBE KEYSPACE system

2.2、创建键空间

  1. CREATE KEYSPACE school WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};

{‘class’: ‘SimpleStrategy’, ‘replication_factor’: 3}: 副本放置策略

2.3、删除键空间

  1. DROP KEYSPACE school ;

2.4、修改键空间

  1. ALTER KEYSPACE school WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};

2.5、使用键空间

  1. USE system;

3、表(列簇)

2.1、查看表

  1. # 查看所有表
  2. DESCRIBE TABLES
  3. # 查看system键空间下的peers表
  4. DESCRIBE TABLE system.peers
  5. # 先进入键空间,再查看表
  6. USE system;
  7. DESCRIBE TABLE peers

2.2、创建表

  1. CREATE TABLE student(
  2. id int PRIMARY KEY,
  3. name text,
  4. age int,
  5. gender tinyint,
  6. address text,
  7. interest set<text>,
  8. phone list<text>,
  9. education map<text,text>
  10. );

2.3、删除表

  1. DROP TABLE testtab;

2.4、修改表

  1. # 添加字段
  2. ALTER TABLE student ADD email text;
  3. # 删除字段
  4. ALTER TABLE student DROP email ;

2.5、清空表

  1. # 清空所有的表数据
  2. TRUNCATE student ;

3、索引

3.1、单一主键

  1. CREATE TABLE student(
  2. id int PRIMARY KEY,
  3. name text,
  4. age int,
  5. gender tinyint,
  6. address text,
  7. interest set<text>,
  8. phone list<text>,
  9. education map<text,text>
  10. );

3.2、组合主键

  1. CREATE TABLE testTab(
  2. key_one int,
  3. key_two int,
  4. name text,
  5. PRIMARY KEY(key_one,key_two)
  6. );

组合主键第一部分为分区key,第二部分为集群key,分区key进行hash计算决定将记录存放到那个分区上,集群key决定同一个分区内相同分区key数据的排序,默认为升序

3.2.1、分区键/集群键

  1. CREATE TABLE testTab1(
  2. key_part_one int,
  3. key_part_two int,
  4. key_cluster_one int,
  5. key_cluster_two int,
  6. key_cluster_three int,
  7. name text,
  8. PRIMARY KEY((key_part_one,key_part_two),key_cluster_one,key_cluster_two,key_cluster_three)
  9. );

3.3、创建索引

  1. # 创建索引,指定名字
  2. CREATE INDEX sname ON student (name) ;
  3. # 创建索引不指定名字,系统默认指定名字
  4. CREATE INDEX ON student (age);

自动创建一张表,将原始表格中索引字段作为新索引表的primary key,存储的值为原始数据的primary key

name id
lisi 1

集合列创建索引

  1. # set 集合
  2. CREATE INDEX ON student (interest) ;
  3. # map集合
  4. CREATE INDEX ON student (keys(education));

3.4、删除索引

  1. # 根据索引名字删除
  2. DROP INDEX sname ;
  3. # 查看是否删除了
  4. DESCRIBE TABLE student;

4、数据

4.1、添加数据

  1. INSERT INTO student (id , address , age , gender , name , interest , phone , education ) VALUES ( 1011,'中山路21号',16,1,'Tom',{'游泳','跑步'},['1234564555','1355555555'],{'小学':'城市第一小学','中学':'城市第一中学'});
  2. INSERT INTO student (id , address , age , gender , name , interest , phone , education ) VALUES ( 1012,'朝阳路12号',18,1,'Jack',{'游泳','跑步'},['1234564555','1355555555'],{'小学':'城市第一小学','中学':'城市第一中学'});
  3. # 添加过期时间,自动删除 USING TTL 30, 30s后自动删除
  4. INSERT INTO student (id , address , age , gender , name , interest , phone , education ) VALUES ( 1013,'xxxx',18,1,'xxx',{'游泳','跑步'},['1234564555','1355555555'],{'小学':'城市第一小学','中学':'城市第一中学'}) USING TTL 30;

4.2、删除数据

  1. DELETE from student WHERE id=1011;

4.3、修改数据

4.3.1、普通类型

  1. UPDATE student SET age=17 WHERE id=1011;
  2. UPDATE student SET age=17 ,gender=2 WHERE id=1011;

4.3.2、集合类型

  1. # set类型
  2. UPDATE student SET interest =interest + {'xxx'} where id =1011;
  3. UPDATE student SET interest =interest - {'xxx'} where id =1011;
  4. UPDATE student SET interest ={'sdf','ddddd'} where id =1011;
  5. UPDATE student SET interest ={} where id =1011;
  6. DELETE interest FROM student WHERE id =1011;
  7. # list类型
  8. UPDATE student SET phone=['111111111111'] WHERE id=1011;
  9. UPDATE student SET phone=['111111111111'] + phone WHERE id=1011;
  10. UPDATE student SET phone=phone+['111111111111'] WHERE id=1011;
  11. UPDATE student SET phone=phone-['1111111'] WHERE id=1011;
  12. UPDATE student SET phone[2]=['44454545'] WHERE id=1011;
  13. # map集合
  14. UPDATE student SET education['aa'] ='xxxxxxxxx' WHERE id=1011;
  15. UPDATE student SET education =education-{'bb','cc'} WHERE id=1011;
  16. DELETE education['bb'] FROM student WHERE id=1011;
  17. UPDATE student SET education = education+{'a':'aa'} WHERE id=1011;
  18. UPDATE student SET education = {'aa':'aaaaa','bb':'bbbbb'} WHERE id=1011;

4.4、查询数据

  1. select * from student;
  2. SELECT * FROM student WHERE id=1011;

注意:

  • 主键只能用 = 查询
  • 第二主键支持 <=、 <、 >、>=、=
  • 索引列只支持 =
  • 非索引列非主键字段过滤可以使用 ALLOW FILTERING