从事IT基础架构多年,发现自己原来更合适去当老师……喜欢关注新鲜事物,不仅限于IT领域。
分类: Oracle
2006-02-14 10:13:08
on delete子句可以保证数据的一致性,如下例中,删除t1表记录时,相应的t2表记录将被删除:
create table a_t1(id int primary key,name varchar(20));
create table a_t2(id int primary key references a_t1(id) on delete cascade,qty number)
对于on delete子句的解释如下:
ON DELETE Clause
The ON
DELETE
clause lets you determine how Oracle automatically maintains referential integrity if you remove a referenced primary or unique key value. If you omit this clause, then Oracle does not allow you to delete referenced key values in the parent table that have dependent rows in the child table.
CASCADE
if you want Oracle to remove dependent foreign key values.
SET
NULL
if you want Oracle to convert dependent foreign key values to NULL
. CREATE TABLE dept_20
(employee_id NUMBER(4) PRIMARY KEY,
last_name VARCHAR2(10),
job_id VARCHAR2(9),
manager_id NUMBER(4) CONSTRAINT fk_mgr
REFERENCES employees ON DELETE SET NULL,
hire_date DATE,
salary NUMBER(7,2),
commission_pct NUMBER(7,2),
department_id NUMBER(2) CONSTRAINT fk_deptno
REFERENCES departments(department_id)
ON DELETE CASCADE );