Oracle 增加修改刪除欄位

來源:互聯網
上載者:User

標籤:寫法   表結構   create   命名   刪除   one   sql語句   stat   ble   

添加欄位的文法: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);

 

使用一個SQL語句同時添加三個欄位:

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

age integer default 22 not null,

has_money number(9,2)

);

 

修改一個欄位

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

 

另:比較正規的寫法是:

-- Add/modify columns 
alter table TABLE_NAME rename column FIELD_NAME to NEW_FIELD_NAME;

 

刪除一個欄位

alter table test1
drop column name;

 

需要注意的是如果某一列中已經存在值,如果你要修改的為比這些值還要小的列寬這樣將會出現一個錯誤。

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

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

---------------------------------------------------------------------------------------------------------------

進階用法:

重新命名表
ALTER TABLE table_name RENAME TO new_table_name;

 

修改列的名稱

文法:
ALTER TABLE table_name RENAME COLUMN supplier_name to sname;

範例:
alter table s_dept rename column age to age1;

 

附:建立帶主鍵的表>>

create table student (
studentid int primary key not null,
studentname varchar(8),
age int);

 

1、建立表的同時建立主鍵約束
(1)無命名
create table student (
studentid int primary key not null,
studentname varchar(8),
age int);
(2)有命名
create table students (
studentid int ,
studentname varchar(8),
age int,
constraint yy primary key(studentid));


2、刪除表中已有的主鍵約束
(1)無命名
可用 SELECT * from user_cons_columns;
尋找表中主鍵名稱得student表中的主鍵名為SYS_C002715
alter table student drop constraint SYS_C002715;
(2)有命名
alter table students drop constraint yy;


3、向表中添加主鍵約束
alter table student add constraint pk_student primary key(studentid);

 

 ---------------------------------------------------------------------------------------------------------------

轉載自http://georgeinfo.blog.163.com/blog/static/163683341201112655637737/

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.