September 30, 2010 at 2:10 pm
Hi,
I'm running into a situation where I need a field in one table (Foreign Key) to be pulled from the Primary Key in one of two other tables. The Primary Keys and the Foreign Key I hope to setup are GUID's so there won't be duplicates between the two Primary Key columns, so is this possible? Below is some code I've put together to test this, and though SQL does let me create two Foreign Key constraints, inserting a record from either table gives a conflict:
CREATE TABLE Collateral (
CollateralGUID UniqueIdentifier DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
CollateralWhatever NVARCHAR(MAX))
CREATE TABLE Customer (
CustomerGUID UniqueIdentifier DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
CustomerWhatever NVARCHAR(MAX))
CREATE TABLE Comment (
CommentID NUMERIC(18,0) PRIMARY KEY IDENTITY,
LinkGUID UNIQUEIDENTIFIER NOT NULL,
CommentText NVARCHAR(MAX))
ALTER TABLE Comment ADD CONSTRAINT [FK_Comment] FOREIGN KEY (LinkGUID) REFERENCES Collateral(CollateralGUID)
ALTER TABLE Comment ADD CONSTRAINT [FK_Comment2] FOREIGN KEY (LinkGUID) REFERENCES Customer(CustomerGUID)
INSERT INTO Collateral (CollateralWhatever) VALUES ('Test1'),('Test2')
INSERT INTO Customer (CustomerWhatever) VALUES ('Test3'),('Test4')
INSERT INTO Comment (LinkGUID, CommentText)
SELECT TOP 1 CustomerGUID, 'Test Comment' FROM Customer
-- The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Comment".
INSERT INTO Comment (LinkGUID, CommentText)
SELECT TOP 1 CollateralGUID, 'Test Comment' FROM Collateral
-- The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Comment2".
Thanks for any suggestions on how to do this, if it's even possible. I'm setting this up on SQL Server 2008 (soon to be R2) if that helps.
Sam
September 30, 2010 at 2:23 pm
samalex (9/30/2010)
I'm running into a situation where I need a field in one table (Foreign Key) to be pulled from the Primary Key in one of two other tables. The Primary Keys and the Foreign Key I hope to setup are GUID's so there won't be duplicates between the two Primary Key columns, so is this possible?
This looks like multi-master referential integrity - you can implement via triggers instead of implementing via FK constraints.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.September 30, 2010 at 2:32 pm
PaulB-TheOneAndOnly (9/30/2010)
This looks like multi-master referential integrity - you can implement via triggers instead of implementing via FK constraints.
I'm unfamiliar with multi-master referential integrity and can't find much about it via Google. Can you suggest a starting point in researching this?
Thanks --
Sam
September 30, 2010 at 2:40 pm
samalex (9/30/2010)
PaulB-TheOneAndOnly (9/30/2010)
This looks like multi-master referential integrity - you can implement via triggers instead of implementing via FK constraints.
I'm unfamiliar with multi-master referential integrity and can't find much about it via Google. Can you suggest a starting point in researching this?
Sure. Please check link next... http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/48327;pt=47764/*
This is a Sybase document but, you know... we are family 🙂
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply