Oracle 增加修改刪除欄位與新增注釋

2021-08-30 13:22:04 字數 4161 閱讀 8777

新增欄位的語法: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)

notnull

);

增加乙個字段:

alter

table test1 add

(name varchar2(30)

default 『無名氏』 not

null

);

使用乙個sql語句同時新增三個字段:

alter

table test1

add(name varchar2(30)

default 『無名氏』 not

null

,age integer

default

22not

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

keynot

null

,studentname varchar(8

),age int

);

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

(1)無命名

create

table student (

studentid int

primary

keynot

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)

;

####################################建立oracle資料庫表時候加上注釋#################################

create

table t1(

id varchar2(32)

primary

key,

name varchar2(8)

notnull

,age number,

)

新增表注釋:

comment

ontable t1 is

'個人資訊'

;

新增字段注釋:

comment

oncolumn t1.id is

'id'

;comment

oncolumn t1.nameis '姓名'

;comment

oncolumn t1.age is

'年齡'

;

alter

table club_hot_leads drop

column chlactivitytype1;

alter

table t1 add

(a1 varchar

(600))

;comment

oncolumn t1.a1 is

'我是a1a1a1'

;

alter

table crm add

(ccity varchar2(50)

null);

alter

table crm add

(cmonth number null);

alter

table crm add

(packetname varchar2(

500)

null);

comment

oncolumn crm.ccity is

'發市'

;comment

oncolumn crm.cmonth is

'發月份'

;comment

oncolumn crm.packetname is

'包名稱'

;

Oracle 增加修改刪除字段

新增欄位的語法 alter table tablename add column datatype default value null not null 修改欄位的語法 alter table tablename modify column datatype default value null ...

Oracle增加修改刪除字段 主鍵

alter table xgj rename column old name to new name alter table tablename modify column datatype default value null not null 假設表xgj,有乙個欄位為name,資料型別char...

MySQL增加 修改 刪除字段

1 增加字段 語法 alter table 表名 add 列名 字段型別 示例 alter table perple add name varchar 200 改進 增加預設值 alter table perple add name varchar 200 default null 增加備註 alt...