ITPub博客

首页 > 数据库 > Oracle > SQL入门之5 表的创建与修改1

SQL入门之5 表的创建与修改1

原创 Oracle 作者:wmlm 时间:2007-04-05 08:36:56 0 删除 编辑
几种约束[@more@]

-- 添加主键
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/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: SQL入门之6 sql*plus
请登录后发表评论 登录
全部评论

注册时间:2008-04-08

  • 博文量
    398
  • 访问量
    3716774