資料庫——修改表

來源:互聯網
上載者:User

標籤:指定欄位   font   style   mit   cas   commit   多個   sql   type   

二、修改表

1、修改表欄位:

a、添加欄位

alter table student

add (sex char(2) default ‘1‘ constraint student_sex_type check(sex=‘1‘ or sex=‘2‘))

注意:新添加的欄位在表的最後一列,不能指定欄位的位置,這與MySQL資料庫不同(MySQL資料庫可以通過after設定添加欄位的先後順序)

b、修改欄位

alter table student

modify(name varchar2(12))

注意:欄位的修改包括修改資料類型(只有對應列為空白指才可以修改)、大小和預設值,其中預設值的修改只會影響後來插入表的資料,對之前的資料不會產生影響。

c、刪除欄位

alter table student

drop(sex)

注意:

①、一次只能刪除一個欄位;

②、一個表至少要保留一個欄位;

③、如果所刪除列(如publisher表中id列)是另一個表的外鍵(book表publisher_id)則該列(publisher表中id列)無法刪除,如所示:

 

 

2、修改表欄位約束:

a、添加約束

alter table student

add constraint student_id_pk primary key(id);--將student表id列設為主鍵

注意:

  1、可以一次添加多個約束,如下:

 

 

 

  2、不能通過添加約束為某個欄位添加not null約束,只能通過修改欄位為其指定not null,如下:

 

 

b、刪除約束

alter table student

drop constraint student_sex_type;--依據約束名刪除約束

alter table student

drop primary key--依據約束類型刪除約束

alter table student

drop unique(id) --依據約束類型刪除約束

注意:drop子句後面可以使用cascade,意為刪除約束的同時與其相依賴的約束也一同刪除,例如:

 

 

c、啟用或禁用約束

   禁用約束:

         alter table student

    disable constraint student_sex_type;--禁用約束

         注意:disable子句後面可以使用cascade,意為禁用約束的同時與其相依賴的約束也一同禁用,例如:

 

 

 

 

 

 

   啟用約束:

    alter table student

    enable constraint student_sex_type;--啟用約束,注意:enable後面不能使用cascade關鍵字

注意:約束無法修改,只能通過先刪除再建立的方式間接修改欄位約束。

三、表重新命名

rename 舊名字 to 新名字

四、截斷表

truncate table 表名——不用執行commit就可以將表資料刪除

delete from 表名——必須執行commit才可以將表資料刪除

    delete、drop、truncate的區別

五、刪除表

drop table 表名

六、注釋

表注釋

comment on table 表名 is ‘注釋內容‘;

欄位注釋

comment on column 表名.欄位名 is ‘注釋內容‘;

資料庫——修改表

聯繫我們

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