oracle 11g SQL語句補充學習

來源:互聯網
上載者:User

標籤:

添加列:

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語句補充學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.