標籤:
添加列:
alter table tablename add columnName datatype (not null); -------需要注意一點的是在添加一列為非空的時候, 表必須是空表.
刪除列:
alter table tablename drop column columnName
改變列的資料類型:
alter table tablename modify columnName datatype
改變列名:
alter table tablename rename column columnName to new_columnName
刪除資料:
delete tablename;
truncate table tablename;
drop table tablename; --------刪除表
常用的彙總函式:
取整函數
floor()-----向上取整
ceil()-----向下取整
trunc()-----取整
round()-----四捨五入取整
查詢年齡的時候可以使用這個函數
select trunc(months_between(*,*)/12) from table-----------前者是未來的日期
或者
select trunc(sysdate-dataDate)/365 from table
months_between()返回的是差多少個月, 兩個日期資料相減是差多少天
union和union all------------一個去除重複一個全部顯示
case的用法
先上兩種文法:
case when 欄位名=... then ...(無限接) else ... end
case 欄位名 when ... then ...(無限接) else ... end
不寫 end 是豬, 忘了N+1次了
decode()函數
decode()函數有點類似於case...when的用法
decode(欄位名,‘值1‘,‘值1的對應值‘,‘值2‘,‘值2的對應值‘...(無限接)...,‘default‘)
vm_concat()函數
select sname,student.sno,wm_concat(cno) from student join score on student.sno=score.sno group by sname,student.sno order by student.sno
student表:
score表:
查詢結果:
concat函數:
將兩個查詢結果串連到一起去, 最多兩個
select concat(sname,sbirthday) from student
replace()函數:
replace(欄位名,‘值1‘,值2)
查詢欄位的內容, 如果他是‘值1‘, 就替換成‘值2‘
nvl()和nvl2():
nvl(欄位名,‘值‘)
如果查詢的這個欄位的值為空白(null), 就返回‘值‘
nvl2(欄位名,‘值1‘,‘值2‘)
如果查詢結果不為空白, 返回‘值1‘, 如果為空白返回‘值2‘
oracle 11g SQL語句補充學習