Can I add ON DELETE CASCADE option to an existing FOREIGN KEY using alter.
Printable View
Can I add ON DELETE CASCADE option to an existing FOREIGN KEY using alter.
Yes that can be specified.
First you drop the existing constraint.
Alter table drop constraint constraintname;
ex:- alter table drop constraint fk_dept
Then once again add the constraint.
alter table add constraint constraintname foreign key(deptno)
references dept(deptno) on delete cascade
ex:- alter table add constraint dept_emp foreign key(deptno)
references dept(deptno) on delete cascade
First Drop foreign key constraint using below command
alter table emp drop constraint fk1
After that add fk constraint, cascade option using this command
ALTER TABLE dept
ADD CONSTRAINT fk1
FOREIGN KEY (deptno)
REFERENCES emp(dno) ON DELETE CASCADE;