-
测试的表:
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');