首页 > 数据库 > Oracle > SQL入门之5 表的创建与修改1
-- 添加主键
alter table orderitems
add constraint orderitems_pk primary key (order#,item#);
-- 添加外键
alter table orders
add constraint orders_customer#_fk foreign key
(customer#) references customers(customer#);
-- 父表记录删除后,级联删除子表记录
alter table orders
add constraint orders_customer#_fk foreign key
(customer#) references customers(customer#)
on delete cascade;
-- 使用unique约束
alter table books
add constraint books_title_uk unique (title);
-- 使用check约束,在check中不能引用函数如sysdate,user,rownum,也不能引用存储在其他行中的值
alter table orders
add constraint orders_shipdate_ck
check (orderdate<= shipdate);
-- 使用not null约束,可以为此约束指定名称,也可以不指定
alter table books
modify (pubid constraint books_pubid_nn not null);
alter table books
modify (pubid not null);
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/271063/viewspace-908795/ ,如需转载,请注明出处,否则将追究法律责任。