【HCIA Gaussdb】学习汇总-数据库管理(SQL语法 库表 索引操作)-5

# 简单查询
select * from table_reference
# 创建表
create table TB(staff_id int primary key , course_name char(50) , exam_date datetime);
# 插入数据
insert into TB values(10,"ljj","2019-10-1112:00:00")

# distinct 消除重复列
select table1.name,table2.name from table1,table2

# left join 连接查询
select a.name,b.name,a.number,b.number from a left join b on a.name=b.name

# 子查询
select * from a where number > (select avg(number) from a)
create table a select * from b ; # 创建一个和B一样的表 并导入所有数据

# 合并结果集
select * from a union select * from b -----> a+b 除去相同部分
select * from a union all select * from b ------> a+b 所有

# 差异结果集
select * from a minus/except select * from b ------a - b a除去所有和b相同的部分

# 分组
select * from a group by(number,name)

 

 

 # 创建分区表

create table TB(id int primary key auto_increment,name char(30),time datetime, grade int) partition by range(id)(
partition training1 values less than (100),
partition training2 values less than (200)
);

# 新增列
alter table TB add username char(50);
# 删除类
alter table TB drop username
# 修改列
alter table TB change username user varchar(10);
alter table TB modify user varchar(20);

# 删除表
drop table TB;
# 恢复表
flashback table TB to before drop

# 创建索引
create index idx_posts on TB(id) online;
# 删除索引
drop index idx_posts on TB;

# 创建视图
create view v_all as select * from TB ;
desc TB ==> describe TB 查看结构
# 删除视图
drop view if exists v_all;

创建序列
# 创建 seq_auto_extend序列生成器 当前用户为该生成器的所有者 起点是10 最大值为200 自增步长为2
create sequence seq_auto_extend start with 10 maxvalue 200 increment by 2 cycle;


# 获取值 下一个值 或者当前值
select seq_auto_extend {nextval | currval} from DUAL
# 获取序列自增
insert into test values( seq_auto_extend.nextval,'name')
# 删除序列
drop sequence if exists seq_auto_extend

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-19 11:26  阿里云的奥斯卡  阅读(2538)  评论(0编辑  收藏  举报