修改表和約束 alter語句

2021-09-27 02:00:43 字數 1717 閱讀 3203

create table t_user(

id number constraint user_id_pk primary key,

name varchar2(100),

salary number

);//  drop table t_user;

//在表中新增乙個新的列  add

alter table t_user

add birthday date;

//刪除表的某列  drop

alter table t_user

drop column birthday;

//給表中的列新增約束    add constraint

//這個約束相當於之前的表級約束

alter table t_user

add constraint user_name_un

unique(name);

//測試剛新增的唯一約束是否生效     

insert into t_user(id,name) values(1,'zs');

insert into t_user(id,name) values(2,'zs');

//刪除表中的約束       drop constraint

alter table t_user

drop constraint user_name_un;

//修改表的名字:     rename to

rename t_user to mytest;

rename mytest to t_user;

//修改表中某列的型別    modify

alter table t_user

modify (name varchar2(500));

//讓約束失效:必須知道約束的名字   disable

alter table t_user

disable constraint user_id_pk cascade;

//測試是否設定成功        

insert into t_user(id,name) values(1,'zs1');

insert into t_user(id,name) values(1,'zs2');

//讓失效的約束再次生效    enable

alter table t_user

enable constraint user_id_pk;

//截斷表中的資料(刪除),不需要提交,預設已經提交,並且不能回滾    truncate

truncate table t_user;

相當於:

delete from t_user;

commit;

//給表新增注釋    comment on 

comment on table t_user is '很好';

//給列新增注釋    

comment on column t_user.name is 'good';

//檢視表中注釋

select * from user_tab_comments where table_name=upper('t_user');

//檢視列中的注釋

select * from user_col_comments 

where 

comments is not null 

and 

table_name=upper('t_user');

修改表和約束

alter也是ddl語句 update是修改表或試圖的資料內容 alter是修改表或試圖的資料結構 在表中新增乙個新的列 alter table 表名 add 列名 列約束 刪除表的某列 alter table 表名 drop column 列名 給表中新增約束,相當於表級約束 alter tabl...

MYSQL修改表結構語句 alter

mysql修改表結構語句 alter create table user id int primary key auto increment,username varchar 20 unique,password varchar 20 not null,age int,birthday date 修...

oracle建立表和約束的SQL語句

1 建立模擬的資料表 1.1.建立學生表student create table student stuid number not null,學生id stuname varchar2 10 not null,名稱 gender varchar2 10 not null,性別 age number ...