现在的位置: 首页 > 综合 > 正文

约束

2013年07月02日 ⁄ 综合 ⁄ 共 1321字 ⁄ 字号 评论关闭

1.  可以给约束命名,也可以由Oracle服务器使用SYS_Cn格式产生一个名称

2.  所有约束都存储在数据字典中,通过查看USER_CONSTRAINTS数据字典表。

3.  NOT NULL约束:

Eg: Create table employees

(

           Last_name varchar(25) not null(系统命名),

           Hire_date DATE constraint emp_hire_date_nn(用户命名) not null

)

4.  Unique 约束:表的任意两行在指定列或列集合中都没有重复值,允许插入空值

Eg: constraint emp_email_uk UNIQUE(email) 

5.  Primary Key 约束:

Eg: constraint dept_id_pk PRIMARY KEY(department_id)

6.  Primary Key 约束:

Eg: constraint emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id)

7.  ON DELETE CASCADE:当删除父表中的行时,也删除字表中的相关行。

8.  ON DELETE SET NULL:将相关外键值转换为空值。

9.  CHECK约束:

Egconstraint emp_salary_min check(salary>0)

10. 添加约束,使用ALTER TABLE可以添加或删除约束,但不可以修改它的结构,启用或禁用约束,使用MODIFY字句添加NOT NULL约束

Alter table employees

Add constraints emp_manager_fk foreign key(manager_id) references employees(employee_id)

11. 删除约束:alter table employees drop constraint emp_manager_fk;

                             Alter table departments drop primary key cascade;

12. 禁用约束:alter table employees disable constraint emp_emp_id_pk cascade;

13. 启用约束:alter table employees enable constraint emp_emp_id_pk;

14. 级联约束:

         CASCADE CONSTRAINTS 字句和DROP COLUMN字句一起使用,删除涉及到在已删除列上定义的主键或唯一关键字的所有引用完整性约束。

         Alter table test drop (pk) cascade constraints;

15. 查看约束: 数据字典

         Select constraint_name,constraint_type , search_condition from user_constraints;

16. 查看与约束关联的列:

         Select constraint_name,column_name from user_cons_column;

【上篇】
【下篇】

抱歉!评论已关闭.