修改表和约束(alter语句)
-
測試的表:
?? ?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语句)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: /etc/fstab详解
- 下一篇: Razor与HTML混合输出陷阱与技巧