【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

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM