三种方法维护数据完整性:ORACLE完整性约束,数据库触发器,应用程序代码。
应尽量使用ORACLE完整性约束,可靠性和效率高,容易修改,使用灵活,记录在数据字典。
ORACLE五种约束:非空 not null,定义 的列不能为空。只能在列级定义
唯一,unique,表中每一行所定义 的列或列值不能相同
主键primary key 不能包括空值,主键唯一标识表中每一行,一列或几列组合为主键
外键foreign key 指明一列或几列的组合为外键以维护从表chilD table和主表 parent table 之间的引用完整性referential integrity
条件约束check,表中每一行要满足约束条件。约束加上表上,创建表时可以定义
BYS@bys1>select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.table_name = upper('&AA');
Enter value for aa: emp
old 1: select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.table_name = upper('&AA')
new 1: select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.table_name = upper('emp')
CONSTRAINT_NAME COLUMN_NAME
------------------------------ ---------------
PK_EMPNO EMPNO
BYS@bys1>select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.constraint_name = b.constraint_name and b.constraint_type = 'P' and a.table_name =upper('&table_name');
Enter value for table_name: emp
old 1: select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.constraint_name = b.constraint_name and b.constraint_type = 'P' and a.table_name =upper('&table_name')
new 1: select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.constraint_name = b.constraint_name and b.constraint_type = 'P' and a.table_name =upper('emp')
CONSTRAINT_NAME COLUMN_NAME
------------------------------ ---------------
PK_EMPNO EMPNO
BYS@bys1>col owner for a10
BYS@bys1>col table_name for a10
BYS@bys1>select * from user_cons_columns;
OWNER CONSTRAINT_NAME TABLE_NAME COLUMN_NAME POSITION
---------- ------------------------------ ---------- --------------- ----------
BYS PK_OBJ_ID TEST2 OBJECT_ID 1
BYS SYS_C0011203 TEST1 OBJECT_ID 1
BYS PK_EMPNO EMP EMPNO 1
BYS@bys1>select constraint_name,table_name,constraint_type,status,deferrable,deferred,validated from dba_constraints where owner='BYS';
CONSTRAINT_NAME TABLE_NAME C STATUS DEFERRABLE DEFERRED VALIDATED
------------------------------ ---------- - -------- -------------- --------- -------------
SYS_C0011203 TEST1 P
PK_EMPNO EMP P ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
PK_OBJ_ID TEST2 P ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
>select constraint_name,table_name,constraint_type,status,deferrable,deferred,validated from user_constraints;
CONSTRAINT_NAME TABLE_NAME C STATUS DEFERRABLE DEFERRED VALIDATED
------------------------------ ---------- - -------- -------------- --------- -------------
FK_DEPTNO EMP R ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
PK_DEPT DEPT P ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
PK_EMP EMP P ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
SYS_C0011265 TEST P ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
PK_A TEST1 P ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
alter table dept add constraint pk_dept primary key(deptno);
alter table dept2 add primary key(dname);
alter table emp add constraint fk_deptno foreign key(deptno) references dept(deptno)
alter table emp add constraint fk_deptno foreign key(deptno) references dept(deptno);
注:on delete cascade和on delete set null的作用是用来处理级联删除问题的,
如果你需要删除的数据被其他数据所参照(主、外键),那么你应该决定到底希望oracle怎么处理那些参照这些即将要删除数据的数据的.
你可以有三种方式:
禁止删除,这也是oracle默认的。
将那些参照本值的数据的对应列赋空,使用on delete set null关键字。即删除本表的列中的值,对本列有外键引用的表的相应的列有相应值行会被修改为NULL。
将那些参照本值的数据一并删除,使用on delete cascade关键字,即删除本表的列中的值,对本列有外键引用的表的相应的列有相应值行会被删除
alter table dept add constraint valid_deptno check (); DEPTNO列的值需要少于5000
SQL>alter table customer add constraint abc check (address in (’海淀’,’朝阳’,’东城’,’西城’,’通州’,’崇文’,’昌平’)); 增加客户的住址只能是’海淀’,’朝阳’,’东城’,’西城’,’通州’,’崇文’,’昌平’;
alter table customer add constraint aaa unique(cardId);
SQL>alter table dept modify dname not null;
因为非空约束不能指定约束名,应该先通过查询表和列所对应的的约束信息,找出约束名,再删除。如下:
BYS@bys1>alter table dept drop constraint SYS_C0011725;
alter table dept2 modify constraint pk_d2 initially immediate;
alter table emp drop constraint PK_EMPNO;
alter table drop constraint evtid_pk; 删除其它用户的约束--需要DBA权限
altertable emp drop primary key cascade; 删除主键约束的时候,如果在些主键上的外键创建时未指定参数,直接删除主键报错,要加上cascade参数。
alter table scott.emp rename constraint to
更多详情见请继续阅读下一页的精彩内容:
相关阅读:
Oracle完整性约束
Oracle的约束和索引
从Oracle的约束到索引
Oracle常用数据类型和完整性约束
ORA-02291: 违反完整约束条件 …… - 未找到父项关键字