首頁 > php教程 > PHP开发 > 主體

oracle刪除表字段和oracle表增加字段

高洛峰
發布: 2017-01-06 10:52:46
原創
1581 人瀏覽過

新增欄位的語法:alter table tablename add (column datatype [default value][null/not null],….);

修改欄位的語法:alter table tablename modify (column datatype [default value][null/notueue][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)
);
登入後複製

使用一個SQL語句同時新增三個欄位:另一個欄位來修改一個

:比較正規的寫法是:

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;
登入後複製

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

高級用法:

重命名表ALTER TABLE table_name RENAMETO new_table_c


TABLE table_name RENAME COLUMN supplier_name to sname;

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


1、建立表格的同時建立主鍵約束

(1)無命名

insert into test1
values ('1′,'我们很爱你');
登入後複製

(2)有命名
create table student (
studentid int primary key not null,
studentname varchar(8),
age int);
登入後複製
2、刪除表中已有的主鍵約束(1)無命名表

可用SELECT * from user_conscolumns;主鍵名稱得student表中的主鍵名為SYS_C002715

alter table student drop constraint SYS_C002715;

(2)有命名
alter table students drop constraint yy;
, 335 向資料表中加入主鍵。 primary key(studentid);

更多oracle刪除表字段和oracle表增加字段相關文章請關注PHP中文網!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!