How to Alter Constraint

SqlOracle

Sql Problem Overview


SQL How to Alter Constraint

Below is 1 of my constraint

CONSTRAINT ACTIVEPROG_FKEY1 FOREIGN KEY(ActiveProgCode) REFERENCES PROGRAM(ActiveProgCode),

I want to add in

ON DELETE CASCADE

to the constraint above.

How do i alter that existing constraint ACTIVEPROG_FKEY1 and add

ON DELETE CASCADE

to constraint ACTIVEPROG_FKEY1

Consider ACTIVEPROG_FKEY1 is at Table ACTIVEPROG

Sql Solutions


Solution 1 - Sql

You can not alter constraints ever but you can drop them and then recreate.

Have look on this

ALTER TABLE your_table DROP CONSTRAINT ACTIVEPROG_FKEY1;

and then recreate it with ON DELETE CASCADE like this

ALTER TABLE your_table
add CONSTRAINT ACTIVEPROG_FKEY1 FOREIGN KEY(ActiveProgCode) REFERENCES PROGRAM(ActiveProgCode)
    ON DELETE CASCADE;

hope this help

Solution 2 - Sql

No. We cannot alter the constraint, only thing we can do is drop and recreate it

ALTER TABLE [TABLENAME] DROP CONSTRAINT [CONSTRAINTNAME]

> Foreign Key Constraint

Alter Table Table1 Add Constraint [CONSTRAINTNAME] Foreign Key (Column) References Table2 (Column) On Update Cascade On Delete Cascade

> Primary Key constraint

Alter Table Table add constraint [Primary Key] Primary key(Column1,Column2,.....)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1777711View Question on Stackoverflow
Solution 1 - Sqluser1819920View Answer on Stackoverflow
Solution 2 - SqlandyView Answer on Stackoverflow