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

2021-08-31 21:17:39 字數 3356 閱讀 2498

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

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

create table t1(

id varchar2(32) primary key,

name varchar2(8) not null,

age number,

)新增表注釋:

comment on table t1 is '個人資訊';

新增字段注釋:

comment on column t1.id is 'id';

comment on column t1.nameis '姓名';

comment on column t1.age is '年齡';

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

語句:

comment on table 表名 is '表的注釋資訊';

comment on column 表名.欄位名 is '欄位的注釋資訊';

注意表名的大小寫

例如:1、建立表:

create table student(

id varchar2(32) primary key,

name varchar2(8) not null,

age number

);2、新增表注釋:

comment on table student is '個人資訊';

修改表注釋:

comment on table student is '學生個人資訊';

3、新增字段注釋:

comment on column student.id is 'id';

comment on column student.name is '姓名';

comment on column student.age is '年齡';

修改字段注釋:

comment on column student.id is '學生id';

comment on column student.name is '學生姓名';

comment on column student.age 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...