建表sql
create table 表名猾编;
create table student(
sno number(2) constraint pk_dept primary key,
sname varchar2(14) unique key,
sex varchar2(1)?foreign key ;
備注:
主鍵(不為空,不重復卵迂,primary key )
唯一(不重復气破,空可以出現1次,unique)
外鍵(該字段的值暑椰,必須在指定外部表中存在霍转,foreign key)
刪表sql
drop table 表名;
drop table student一汽;
改表名
alter table 舊表名 rename to 新表名;
alter table older_student rename to new_student ;
查數據庫中所有表
select * from user_tables t;
索引(index)
主鍵索引(創(chuàng)建表時避消,默認有主鍵,不用寫主鍵)
普通索引
create unique index uq_ename_idx on studnet(sname)
權限(o用戶:owner所有權限用戶召夹,c用戶:customer使用客戶用戶沾谓;grant:把表的增刪改查權限有選擇性的賦權給其它用戶(比如程序連接的數據庫用戶名)
sql查詢方法圖:
組合因子
1、視圖:表戳鹅,sql查詢后的結果
2均驶、操作方法
from
join on
where?
group by
having
select:rownum、rowid
top
order by
2組合規(guī)則
1.操作方法傳入一個視圖枫虏,輸出一個視圖
2.操作方法可以套接成串執(zhí)行妇穴,但是有固定的先后順序
表連接
1、左連接(查詢的結果以左邊為準隶债,保證左邊的數據都有)
select * from a left join b where a.id=b.id?
2腾它、右連接(查詢的結果以右邊為準,保證右邊的數據都有)
select * from a right join b where a.id=b.id
3死讹、全連接(查詢的結果顧及兩邊都有)
select * from a full join b on a.sno=b.sno 瞒滴,沒有的有為空顯示
4、sql加減法
sql和sql的結果可以做加減運算(minuus(減)赞警、union(加)妓忍、union all(加全部)、intersect(交叉))
4.1? sql1 minus sql2? ?(查詢兩條sql有差異的地方)
4.2 sql1 union sql2 (查詢兩條sql的并集)
4.3 sql1 union all sql2(查詢兩條sql的并集,交集多取一次)
4.4 sql1 intersect sql2(查詢兩條sql的交集)