Ali Hussain
Ali Hussain

Reputation: 896

Can We Create On Delete Cascade on exiting foreign Key

Can we create On Delete Cascade on an existing Foreign Key?

I know we can create from SQL design of table going to relationships But I want to do this with the script query?

I also not want to drop foreign key first and then create the foreign key with on delete cascade, I not want this.

Upvotes: 0

Views: 37

Answers (1)

Thom A
Thom A

Reputation: 95827

There is no ALTER CONSTRAINT syntax. If you have created a CONSTRAINT and you need to change it, you need to DROP the CONSTRAINT and then CREATE it again; there is no work around for this.

In this case, using the definition in the comments, you would need to do:

ALTER TABLE [dbo].[HL7_Rx_Resident_NonVerified] DROP CONSTRAINT [FK_HL7_Rx_Resident_NonVerified];
GO

ALTER TABLE [dbo].[HL7_Rx_Resident_NonVerified] WITH CHECK
ADD CONSTRAINT [FK_HL7_Rx_Resident_NonVerified]
    FOREIGN KEY ([MedicationId])
    REFERENCES [dbo].[RxOrder_Resident_NonVerified] ([Id]) ON DELETE CASCADE;
GO

Upvotes: 3

Related Questions