Can We Create On Delete Cascade on exiting foreign Key - sql-server

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.

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

Related

Is there a way to turn off foreign key constraints at database level?

We need to use existing sql to create a bunch of tables and this sql includes ALTER TABLE statements to add in foreign keys. When we run this block of SQL, however, errors are thrown because tables needed for the foreign keys haven't been created yet (they are later on in the block of SQL).
Is there a way to turn off foreign key constraints at database level? (versus at table level) We just need to create all of the tables and foreign keys and then turn it back on.
Thanks!
To disable foreign key constraints when you want to truncate a table:
Use FOREIGN_KEY_CHECKS
SET FOREIGN_KEY_CHECKS=0;
and remember to enable it when you’re done:
SET FOREIGN_KEY_CHECKS=1;
Or you can use DISABLE KEYS:
ALTER TABLE table_name DISABLE KEYS;
Again, remember to enable if thereafter:
ALTER TABLE table_name ENABLE KEYS;
Note that DISABLE KEYS does not work on InnoDB tables as it works properly for MyISAM.
Use ON DELETE SET NULL
If you don’t want to turn key checking on and off, you can permanently modify it to ON DELETE SET NULL:
--Delete the current foreign key first:
ALTER TABLE table_name1 DROP FOREIGN KEY fk_name1;
ALTER TABLE table_name2 DROP FOREIGN KEY fk_name2;
--Then add the foreign key constraints back
ALTER TABLE table_name1
ADD FOREIGN KEY (table2_id)
REFERENCES table2(id)
ON DELETE SET NULL;
ALTER TABLE tablename2
ADD FOREIGN KEY (table1_id)
REFERENCES table1(id)
ON DELETE SET NULL;

cascade foreign key on multiple columns

I am trying to create UPDATE CASCADE Foreign key reference to a table using below scripts.I am getting error saying it may cause cycles or multiple cascade paths.
ALTER TABLE STA_STATIONARY_REQUEST_MASTER WITH CHECK ADD CONSTRAINT [FK_STA_STATIONARY_REQUEST_MASTER_REQUESTOR_ID] FOREIGN KEY([REQUESTOR_ID])
REFERENCES STA_EMPLOYEE_MASTER ([EMPLOYEE_ID])
ALTER TABLE STA_STATIONARY_REQUEST_MASTER WITH CHECK ADD CONSTRAINT [FK_STA_STATIONARY_REQUEST_MASTER_CREATED_BY] FOREIGN KEY([CREATED_BY])
REFERENCES STA_EMPLOYEE_MASTER ([EMPLOYEE_ID])
But when I am creating using below scripts it is created successfully.But still i am not able to update the employee id.
ALTER TABLE STA_STATIONARY_REQUEST_MASTER ADD CONSTRAINT
FK_STA_STATIONARY_REQUEST_MASTER_EMP1
FOREIGN KEY (REQUESTOR_ID) REFERENCES STA_EMPLOYEE_MASTER(EMPLOYEE_ID),
FOREIGN KEY (CREATED_BY) REFERENCES STA_EMPLOYEE_MASTER (EMPLOYEE_ID)
ON UPDATE CASCADE
Please help.

How can I drop a constraint with multiple columns as the primary key in SQL Server?

My code looks like this:
CONSTRAINT user_password_username_password PRIMARY KEY (username, password)
I need to drop this constraint because I updated the foreign keys and no longer can use password in the constraint. I also cannot simply drop the table and recreate it because it is linked to other tables. How can I drop this constraint so only username will make up the primary key?
Drop all foreign key constraints referencing the user_password_username_password constraint. Disable will not work; you must drop them. You may need to drop foreign keys that reference other unique keys on the table, but I'm not sure about that.
Drop the user_password_username_password constraint on the table.
Create a new unique/primary key constraint on the table.
Create or recreate any foreign key constraints.
I believe you can do all this in a single transaction, but if it's a large table I'd consider doing this in single user mode. You'll be dropping a primary key which causes an index rebuild for every index and constraint on the table, then potentially creating several new indexes/constraints in the foreign keys, and then creating the new primary key which again forces all the indexes and constraints on the table to rebuild.

Modify Foreign Key Relationships in TSQL

I would like to see some example how to modify foreign key relationship in TSQL because it is missing here http://technet.microsoft.com/en-us/library/ms175493.aspx
Actually I want to apply delete rule Cascade.
Should it be like?
ALTER TABLE Email MODIFY
CONSTRAINT FK_EmailContact_Email
ON DELETE CASCADE
Thank you!
I found how I can do it
GO
ALTER TABLE EmailContact DROP
CONSTRAINT FK_EmailContact_Email
GO
ALTER TABLE EmailContact ADD
CONSTRAINT FK_EmailContact_Email
FOREIGN KEY (EmailId)
REFERENCES Email (Id)
ON DELETE CASCADE

Unable o create foreign key with the help of code

I am using the code below, but unable to create the foreign key.
USE project;
GO
ALTER TABLE Sale.TempSalesReason
ADD CONSTRAINT FK_TempSale_SalesReason FOREIGN KEY (TempID)
REFERENCES Sale.SalesReason (SalesReasonID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
GO
Please go through the link below and check if you are able to create the foreign key relationships in SQL server.
http://technet.microsoft.com/en-us/library/ms189049.aspx

Resources