oracle约束

来源:互联网 发布:侯景之乱 知乎 编辑:程序博客网 时间:2024/05/22 08:27

约束

数据完整性: 数据的完整性用于确保数据库数据遵守从一定的商业和逻辑规则,在oracle中,数据完整性可以使用约束,触发器,应用程序(存储过程,函数) 三种方法来实现,在这三种方法中,因为约束易于维护,并且具有最好的性能,所以作为维护数据完整性的首选

约束:约束用于确保数据库满足特定的商业规则。在oracle中,约束包括not null , unique , primary key , foreign key ,和 check 五种。

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

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

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

· primary key : 用于唯一的标志表行的数据,当定义主键约束后,该列不能重复,且不能为null,一张表只能有一个主键,但是一个表可以有多个unique约束

· foreign key : 用于定义主表和从表之间的关系,外键约束定义在从表上面,主表则必须具有主键约束或是unique约束,当定义外键列数据必须在主表的主键列存在或是为null;

|- 建立约束:

SQL> create table goods(

 // 这里的效果跟直接写primary key 是一样的,只是这样可以自己取约束的名字

  2  goodsId char(8)  constraint PK_goodsId primary key, -- 主键约束

  3  goodsName varchar(30),

  4  unitprice number(7,2)  constraint CK_unitprice check(unitprice > 0), --check约束

  5  category varchar(10),

  6  provider varchar(30));

SQL> create table customer(

  2  customerId char(8) constraint PK_customerId primary key,

  3  name varchar2(20)  not null, --not null 约束

  4  adddress varchar2(50),

  5  email varchar2(30)  constraint  UQ_email  unique, --unique约束

      //注意一下default不是约束,不能使用constraint

  6  sex char(4) default '' constraint CK_sex check(sex in ('','')),

  7  cardId char(18) ) ;

SQL> create table purchase(

  2  customerId char(8), -- 也可以直接写 references customer(customerId)--外键约束

  3  goodsId char(8),

  4  nums number(5),

//下面是表级定义,和上面的效果是一样的,只是把约束统一写在后面

  5 constraint FK_customerId_purchase foreign key (customerId) references customer(customerId), --外键约束

  6  constraint FK_goodsId_purchase foreign key (goodsId)  references goods(goodsId),

  7  constraint CK_nums  check (nums between 1 and 30)); -- 表示范围时使用between and 

|- 追加约束

当发现上面建表的时候,有些约束没有建立,就可以追加约束

|-==== 增加字段的 not null 约束

SQL> alter  table  goods modify  goodsName  not null;

这样也可以修改字段的默认值 default

SQL> alter table  customer modify sex  default  '';

如果要取消默认值,可以:

SQL> alter table  customer modify sex  default  null ;

|-==== 增加unique约束

SQL> alter  table  customer  add  constraint  UQ_cardId  unique(cardId);

|-==== 增加check约束

SQL> alter table  customer  add constraint CK_cardId check( length(cardId) > 18 );

|-==== 增加 primary 约束

SQL> alter table goods add constraint PK_goodsId  primary key (goodsId);

|-==== 增加foreign约束

SQL> alter table purchare add constraint FK_goodsId_purchase foreign key (goodsId)  references  goods(goodsId),                                --这里要用括号括起来

!!!!!当表里面已经有了数据了,此时里面的数据不满足要改的约束,如果是这样的话,就要加上enable novalidate ; 在 sql server中是使用 with nocheck ;

|- 删除约束:

SQL> alter table 表名 drop constraint 约束名 ;

当时如果是主键约束的话,可能会出错,因为主键约束还涉及到一个从表的外键,所以要使用cascade 

SQL> alter table 表名 drop  primary key ;

ORA-02273: 此唯一/主键已被某些外部关键字引用

SQL> alter table 表名 drop primary key cascade ;

|- 查看约束:

主要是数据字典中的两个视图:user_constraints , user_constraints_cons_columns

1,查看某个用户的所有约束

SQL> select constraint_name  from  user_constraints  where owner='HWT'; --注意要大写

2,查看某个表的所欲约束

SQL> select constraint_name  from  user_constraints  where table_name='GOODS';--注意要大写

3,查看某个字段上所拥有的约束

SQL> select constraint_name from user_cons_columns  where column_name='GOODSID';

--注意要大写,column_name 要大写

原创粉丝点击