oracle 约束

来源:互联网 发布:java开源erp与crm系统 编辑:程序博客网 时间:2024/05/29 07:13

如果在建表时忘记建立必要的约束,则可以在建表后使用alter table命令为表增加约束,但是要注意:增加not null约束时,需要使用modify选项,而增加其他四种约束使用add选项。 删除表之后,约束也跟着删了。

A、not null(非空)

如果在列上定义了not null,那么当插入数据时,必须为列提供数据。

create table table_not_null (id number(10) not null);

select * from dba_constraints where table_name='TABLE_NOT_NULL'

select * from dba_cons_columns  where table_name='TABLE_NOT_NULL'

alter table table_not_null drop constraint SYS_C0029257;

alter table table_not_null modify id not null;

alter table table_not_null add constraint not_null_c check(id is not null)创建约束

alter table table_not_null disable constraint not_null_c;禁用约束

alter table table_not_null enable constraint not_null_c;启用约束


B、unique(唯一)

当定义了唯一约束后,该列值是不能重复的,但是可以为null。

create table table_unique (id number(10) constraint unique_c unique);//constraint unique_c 名称可以省略 系统自动分配名字

select * from dba_constraints where table_name='TABLE_UNIQUE'

select * from dba_cons_columns  where table_name='TABLE_UNIQUE'

alter table table_unique drop constraint unique_c //删除唯一约束

alter table table_unique add constraint unique_c unique(id);

alter table table_unique disable constraint unique_c

alter table table_unique enable constraint unique_c


C、primary key(主键)

用于唯一的标识表行的数据,当定义主键约束后,该列不但不能重复而且不能为NULL。一张表最多只能有一个主键,但是可以由多个unique约束。

create table table_primary_key(id number(10) constraint table_pri1 primary key, name char(10));

select * from dba_constraints where table_name='TABLE_PRIMARY_KEY'

select * from dba_cons_columns  where table_name='TABLE_PRIMARY_KEY'

alter table table_primary_key drop constraint TABLE_PRI1 cascade;相关的外键也删除了

alter table table_primary_key drop constraint TABLE_PRI1;

alter table table_primary_key add constraint table_pri1 primary key(id);

alter table table_primary_key disable constraint table_pri1 cascade;相关连的外键也禁用

alter table table_primary_key enable constraint table_pri1;


D、foreign key(外键)

用于定义主表和从表之间的关系,外键约束要定义在从表上,主要则必须具有主键约束或是unique约束,当定义外键约束后,要求外键列数据必须在主表的主键列存在或是为NULL。

create table table_foreigen_key (myid number(10) constraint foreign_11 references table_primary_key(id),name char(10));

select * from dba_constraints where table_name='TABLE_FOREIGEN_KEY'

select * from dba_cons_columns  where table_name='TABLE_FOREIGEN_KEY'

alter table table_foreigen_key drop constraint foreign_11

alter table table_foreigen_key add constraint foreign_11 foreign key(myid) references table_primary_key(id);

alter table table_foreigen_key add constraint foreign_11 foreign key(myid) references table_primary_key(id) ON DELETE CASCADE;记录跟主表保持一致,级联删除.

alter table table_foreigen_key disable constraint foreign_11

alter table table_foreigen_key enable constraint foreign_11


SQL的主键和外键的作用:

外键取值规则:空值或参照的主键值。

(1)插入非空值时,如果主键表中没有这个值,则不能插入。

(2)更新时,不能改为主键表中没有的值。

(3)删除主键表记录时,你可以在建外键时选定外键记录一起级联删除还是拒绝删除。

(4)更新主键记录时,同样有级联更新和拒绝执行的选择。


E、check

用于强制行数据必须满足的条件

create table table_check(id number(10) constraint check_1 check(id between 1 and 199));

select * from dba_constraints where table_name='TABLE_CHECK'

select * from dba_cons_columns  where table_name='TABLE_CHECK'

alter table table_check drop constraint check_1;

alter table table_check add constraint  check_1 check(id between 1 and 199);

alter table table_check disable constraint  check_1

alter table table_check enable constraint  check_1


综合案例:(转载)

http://www.cnblogs.com/Ronger/archive/2011/10/10/2205515.html


商品售货系统表设计案例

现在有一个商店的数据库,记录客户及其购物情况,由下面三个表组成:

商品表Goods(商品号GoodsId,商品名GoodName,单价UnitPrice,商品类别Categroy,供应商Provider)

客户表Customers(客户号CustomerId,姓名Name,住址Address,电邮Email,性别Gender,身份证CardId)

销售表Purchases(客户号CustomerId,商品号GoodsId,购买数量Num)

请用SQL语言完成下列功能:

建表,在定义中要求声明:

(1)每个表的主外键。

(2)客户的姓名不能为空值。

(3)单价必须大于0,购买数量必须在1~30之间。

(4)电邮不能够重复。

(5)客户的性别必须是男或女,默认是男。

sql>create table Goods(

GoodsId char(8) primary key ,--主键

GoodName varchar2(50),

UnitPrice number(10,2) check(UnitPrice>0),--单价必须大于0

Category varchar(30),

Provider varchar(100));

sql>create table Customers(

CustomerId char(8) primary key,

Name varchar2(30) not null, --姓名不能为空

Address varchar2(150),

Email varchar2(100) unique,--必须唯一

Gender char(2) default('男') check(Gender in('男','女')),

CardId char(18));

sql>create table Purchases(

CustomerId char(8) references Customers(CustomerId), --外键

GoodsId char(8) references Goods(GoodsId),

Num number(10) check(Num between 1 and 30)));

2、添加约束

如果在建表时忘记建立必要的约束,则可以在建表后使用alter table命令为表增加约束,但是要注意:增加not null约束时,需要使用modify选项,而增加其他四种约束使用add选项。

(1)增加商品名也不能为空

sql>alter table Goods modify GoodsId not null;

(2)增加身份证也不能重复

sql>alter table Customers add constraint UQ_CardId unique(CardId);

(3)增加客户的住址只能是海淀、朝阳、东城、西域

sql>alter table Customers add constraint CK_Address check(Address in ('海淀','朝阳','东城','西域'));

3、删除约束

当不再需要某个约束时,可以删除。

sql>alter table 表名 drop constraint 约束名称;

特别说明:

在删除主键约束的时候,可能有错误。比如

sql> alter table 表名 drop primary key;

这是因为如果在两表存在主从关系,那么在删除主表主键约束时,必须带上cascade选项。

sql> alter table 表名 drop primary key cascade;

4、显示约束信息

A、显示约束信息

通过查询数据字典视图user_constraints,可以显示当前用户所有的约束的信息。

sql>select constraint_name,constraint_type,status,validated from user_constraints where table_name='Goods';

B、显示列约束

通过查询数据字典视图user_cons_column,可以显示约束所对应的表列信息。

sql>select column_name,position from user_cons_columns where constraint_name='CK_Address';

表级定义和列级定义

A、表级定义

表级定义是指在定义了所有列后,再定义约束,这里需要注意,not null约束只能在列级上定义。

案例:

sql> create table Goods(

GoodsId char(8),GoodsName varchar2(50), Category varchar(30),

constraint PK_GoodsId primary key(GoodsId));

B、列级定义

列级定义是在定义列的同时定义约束。

案例:

sql>create table Goods(

GoodsId char(8) constraint PK_GoodsId primary key ,--主键

GoodName varchar2(50),

UnitPrice number(10,2) check(UnitPrice>0),--单价必须大于0

Category varchar(30),

Provider varchar(100));


原创粉丝点击