Unable o create foreign key with the help of code - sql-server

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

Related

Migrating a self referencing table using Azure Data Factory

I am trying to migrate a table which has 2 colums.
Id - Primarky key
Parent ID - Foreign key populated by the value above (Id).
(So basically the FK is in the same table)
When I migrate this, I get the following error.
"The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint"
Please let me know how to deal with this.
Thanks in advance
Disable the foreign key constraint first. Then migrate your data. And afterwards you can enable the constraint again.
ALTER TABLE Purchasing.PurchaseOrderHeader
NOCHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
ALTER TABLE Purchasing.PurchaseOrderHeader
CHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
Disable foreign key constraints with INSERT and UPDATE statements

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.
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

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.

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

how to add foreign key constraint, SQL Server

I have a registration table with primary key RegId. I have another table named Users, also contain RegId as Foreign key.
When I delete one RegId from registration, I have to delete RegId from Users. Can anybody help?
Define the foreign key with "ON DELETE CASCADE".
You can do this is in T-SQL or in design view in SSMS
You could to alter the main table with a constraint saying that a Delete On Cascade must be done.
Based on your inputs, something similar to this:
ALTER TABLE dbo.Registration
ADD CONSTRAINT FK_Registration_Users_Cascade
FOREIGN KEY (RegId) REFERENCES dbo.Users(RegId) ON DELETE CASCADE
Also, you can achieve this with the SQL Management Studio selecting your table (in Design mode) and go to Relationships option. There you'll see the "INSERT and UPDATE specifications" button for setting this things.
MSDN Article
Cascading Referential Integrity Constraints

Resources