Oracle學習(二) 表的基本操作+merge+truncate

來源:互聯網
上載者:User

(1)建立表的基本文法如下:

create table tableName(columnName dataType [default expression][column columnStraint],...n) [tablespace tableSpaceName]

(2)修改表的基本文法如下:

alter table tableName  [add(columnName dataType [default expression][column columnStraint],...n)] --添加列  [modify(columnName [dataType][default expression][columnStraint],...n)] --修改列  [drop drop_clause] --刪除列或約束條件 drop column columnName

3)刪除表的文法:drop table tableName.

(4)在往表中插入記錄時,如果需要插入某列的值為空白,則值必須置為null,如果列值指定為該列 的預設值,則用default。

(5)merge語句,使用該語句可以實現對錶的更新或插入。文法格式如下:

merge into tableName using tableName on(join_condition) when matched then update set...      when not matched then insert(...) values(...)

這個語句的意思是把using表合并到into表,合并條件是on(condition),當條件滿足時只能是更新 into表中的對應的記錄,當條件不滿足時,則也只能是往into表裡面添加對應的資料,而該資料中也只 能使用using表中目前記錄對應的資料。

樣本如下:

假設有一個student表,那麼以下語句就可以實現當a的id大於b的id的時候把所有student的年齡加2 ,否則就新增一條記錄。

merge into student a using student b on(a.id>b.id) when matched then update set age=age+2 when not matched then insert(id,name,age,sex,no)   values(b.id+100,b.name,b.age,b.sex,b.no);

(6)刪除表記錄之delete和truncate。

delete的文法格式如下:

delete from tableName [where condition]

該語句的意思是刪除tableName表中滿足condition條件的記錄,當condition省略時則刪除表中所有 記錄。

truncate的文法格式如下:

truncate table tableName

該語句的意思是刪除tableName表中的所有記錄,使用truncate可以釋放佔用的資料區塊資料表空間。 truncate刪除是不能復原的,而delete刪除是可以復原的。正因為如此使用truncate刪除所有記錄的速 度比用delete刪除所有記錄的速度快。

查看本欄目更多精彩內容:http://www.bianceng.cn/database/Oracle/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.