Last post Sep 05, 2016 12:12 PM by Chris Zhao
Sep 03, 2016 04:57 PM|ldoodle|LINK
I need to add another primary key, but I get this error:
There's a ton of relationships. One thing I've never done before is programmatically 'export' them to re-add them afterwards. Is that even possible?
Sep 03, 2016 07:03 PM|PatriceSc|LINK
Do you mean that you try to add another column to the existing primary key or that you try to add another primary key (you can have a single pk per table)?
You don't have to delete all fk constraints but you have to delete those that are using the current primary key. Do a backup first and make sure you changed those tables as well so that you'll be able to recreate fk constraints once deleted (you may have
to add columns as well if not done already).
You can use ALTER TABLE (see
https://msdn.microsoft.com/en-us/library/ms190273.aspx) to drop/create FOREIGN KEY constraints.
Sep 03, 2016 07:33 PM|balu.devara|LINK
make sure one thing keep scripts for which tables are realted to it ,
after delete relation ship and update table level primary key
and next execute foreign key scripts for re map to primary key table.
Sep 05, 2016 12:12 PM|Chris Zhao|LINK
One thing I've never done before is programmatically 'export' them to re-add them afterwards. Is that even possible?
#Create Foreign Key Relationships
Create a foreign key in an existing table
ALTER TABLE Sales.TempSalesReason
ADD CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID)
REFERENCES Sales.SalesReason (SalesReasonID)
ON DELETE CASCADE
ON UPDATE CASCADE
#Delete Foreign Key Relationships
To delete a foreign key constraint
ALTER TABLE dbo.DocExe
DROP CONSTRAINT FK_Column_B;