Database Reference
In-Depth Information
constraint. No value can be inserted in the FOREIGN KEY
constraint column having no exactly similar value in the
referenced PRIMANARY KEY constraint column of parent
table.
Example:
CREATE TABLE emp_test
(empno NUMBER(4) CONSTRAINT pk_dept_test
PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(10),
deptno NUMBER(3),
CONSTRAINT fk_emp_test FOREIGN KEY (deptno)
REFERENCES dept_test(deptno)
);
Once this table gets created you can say that you have developed
a “link” between the two tables and this link will be permanent.
The two beauties of PRIMARY/FOREIGN KEY relationship are
as follows.
•
Keeping in focus PRIMARY KEY constraint column
value(s) of parent table, if someone tries to delete a
record from the parent table and there are corresponding
value(s) in one or more child table(s) FOREIGN KEY
constraint column, this task with result in Referential
Integrity constraint violation error. But we can override
this clause if we add ON DELETE CASCADE clause as
shown in the example below.
Search WWH ::
Custom Search