Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating multiple foreign key constraints on a key

Author  Topic 

lucky7456969
Starting Member

6 Posts

Posted - 2015-02-21 : 01:23:44
[code]
CREATE TABLE IssueLog (
ItemNo int NOT NULL IDENTITY PRIMARY KEY
REFERENCES IssueProgress (ItemNo)
ON UPDATE CASCADE
ON DELETE CASCADE,
REFERENCES Approvals(ItemNo)
ON UPDATE CASCADE
ON DELETE SET NULL,
[/code]
[code]
Msg 142, Level 15, State 2, Line 0
Incorrect syntax for definition of the 'TABLE' constraint.
[/code]

I'd like to update or delete the IssueProgress plus update and setting null to the Approvals tables at the same time whenever the ItemNo of the parent table namely IssueLog is updated or deleted.
How can I achieve this?
Thanks
Jack

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-02-21 : 05:15:57
Afaik you cannot use identity fields as foreign keys, so try this:
CREATE TABLE IssueLog (
ItemNo int NOT NULL IDENTITY PRIMARY KEY
FOREIGN KEY (ItemNo) REFERENCES IssueProgress (ItemNo)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (ItemNo) REFERENCES Approvals(ItemNo)
ON UPDATE CASCADE
ON DELETE SET NULL,)

Make sure your references point to the correct fieldnames in the foreign tables (marked in green).
Go to Top of Page
   

- Advertisement -