Oracle中使用alter table來增加,刪除,修改列的文法

來源:互聯網
上載者:User

alter table tablename add (column datatype [default value][null/not null],….);

alter table tablename modify (column datatype [default value][null/not null],….);

alter table tablename drop (column);

這裡分別是使用alter table 來增加、刪除和修改一個列。

下面是具體的例子:
create table test1
(id varchar2(20) not null);

alter table test1
add (name varchar2(30) default ‘無名氏’ not null);

alter table test1
modify (name varchar2(16) default ‘unknown’);

alter table test1
drop column name;

以上分別建立一個表,增加,更改,刪除一個列。其中需要注意的是如果某一列中已經存在值,如果你要修改的為比這些值還要窄的列寬這樣將會出現一個錯誤。

例如前面如果我們插入一個值
insert into test1
values (’1′,’我們很愛你’);

然後曾修改列: alter table test1
modify (name varchar2(8));
將會得到以下錯誤:
ERROR 位於第 2 行:
ORA-01441: 無法減小列長度, 因為一些值過大

PS:改變已存在的表的列名

alter table table_name rename column col_old to col_new

聯繫我們

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