Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

enter image description here

like image 820
Friend Avatar asked Apr 30 '14 15:04

Friend


People also ask

Can you modify a foreign key?

You cannot modify the key in a single statement, see the ALTER TABLE syntax, in which there is no ALTER CONSTRAINT available. You must use 2 ALTER TABLE statements to accomplish what you want. Delete the key in the first one using an ALTER TABLE DROP FOREIGN KEY.

How do I change the reference of a foreign key in SQL?

To modify a foreign key. In Object Explorer, expand the table with the foreign key and then expand Keys. Right-click the foreign key to be modified and select Modify.

Can we update foreign key in a table in SQL?

For our data with default specification (No Action) SQL Server would not allow an update or delete operation on referenced values of the primary key table. Since we did not define a specification for our foreign key the default No Action is used.

How do I remove a foreign key reference in SQL?

To delete a foreign key constraintIn Object Explorer, expand the table with the constraint and then expand Keys. Right-click the constraint and then click Delete. In the Delete Object dialog box, click OK.


1 Answers

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
like image 156
Friend Avatar answered Sep 23 '22 17:09

Friend