473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

More than one column FOREIGN KEY constraint specified for column

How can you indicate that a FOREIGN KEY constraint references two
columns in two different tables?

"SQL Server Books Online" show an example of how to reference two
columns in the SAME table:
REFERENCES ref_table [ ( ref_column [ ,...n ] )

Here is the error and the 'bad' SQL code:

Server: Msg 8148, Level 16, State 1, Line 4
More than one column FOREIGN KEY constraint specified for column
'UserOrGroupId', table 'salesforce3.dbo.AccountShare'.

CREATE TABLE salesforce3.dbo."AccountShare" ("Id" varchar(18) PRIMARY
KEY , ... , "UserOrGroupId" varchar(18) CONSTRAINT
FK_UserOrGroupId6349 FOREIGN KEY REFERENCES "User"(Id) REFERENCES
"Group"(Id) , ... )

Jul 23 '05 #1
2 26916
By definition, a Foreign Key can reference only one table. However, you
can define more than one foreign key on the same column, for example:

CREATE TABLE salesforce3.dbo."AccountShare"
("Id" varchar(18) PRIMARY KEY
, ...
, "UserOrGroupId" varchar(18)
, ...
, CONSTRAINT FK_AccountShare_Users FOREIGN KEY (UserOrGroupID)
REFERENCES "User"(Id)
, CONSTRAINT FK_AccountShare_Groups FOREIGN KEY (UserOrGroupID)
REFERENCES "Group"(Id)
)

But if you want to do what I think you want to do, then you are out of
luck. You should not try to give one column two different meanings. If
an AccountShare sometimes has a relation with User and sometimes with
Group, then you should create two columns: one column UserID that
references table User, and one column GroupID that references Group.
Make sure you make both columns nullable.

If you require that an AccountShare is either a User or a Group, then
you can ensure this behavior by adding the following constraint:
CONSTRAINT CK_AccountShare_UserOrGroup
CHECK ( (UserID IS NULL AND GroupID IS NOT NULL)
OR (UserID IS NOT NULL AND GroupID IS NULL) )

Hope this helps,
Gert-Jan
ad**********@marketsquaresolutions.com wrote:

How can you indicate that a FOREIGN KEY constraint references two
columns in two different tables?

"SQL Server Books Online" show an example of how to reference two
columns in the SAME table:
REFERENCES ref_table [ ( ref_column [ ,...n ] )

Here is the error and the 'bad' SQL code:

Server: Msg 8148, Level 16, State 1, Line 4
More than one column FOREIGN KEY constraint specified for column
'UserOrGroupId', table 'salesforce3.dbo.AccountShare'.

CREATE TABLE salesforce3.dbo."AccountShare" ("Id" varchar(18) PRIMARY
KEY , ... , "UserOrGroupId" varchar(18) CONSTRAINT
FK_UserOrGroupId6349 FOREIGN KEY REFERENCES "User"(Id) REFERENCES
"Group"(Id) , ... )

Jul 23 '05 #2
Here's how I did it a while back. Define all Pks in the main tables when
creating. Then define the FKs in the child tables. Then make the combined
attributes a PK made of the 2 Fks. It worked for me.

I was trying to combine 2 FKs into one PK, and SQL Server won't take it like
that. you can do PK(field1, field2) though, and the 2 fields can be FKs..

ALTER TABLE [dbo].[IS_REGISTERED] ADD
CONSTRAINT [FK_IS_REGISTERED_SECTION] FOREIGN KEY
(
[Section_ID]
) REFERENCES [dbo].[SECTION] (
[Section_ID]
),
CONSTRAINT [STUDENT_IS_REGISTERED] FOREIGN KEY
(
[Student_ID]
) REFERENCES [dbo].[STUDENT] (
[Student_ID]
)
GO

ALTER TABLE [dbo].[IS_QUALIFIED] ADD
CONSTRAINT [COURSES_IS_QUALIFIED] FOREIGN KEY
(
[Course_ID]
) REFERENCES [dbo].[COURSE] (
[Course_ID]
),
CONSTRAINT [FACULTY_IS_QUALIFIED] FOREIGN KEY
(
[Faculty_ID]
) REFERENCES [dbo].[FACULTY] (
[Faculty_ID]
)
GO

ALTER TABLE [dbo].[SECTION] ADD
CONSTRAINT [FK_SECTION_COURSE] FOREIGN KEY
(
[Course_ID]
) REFERENCES [dbo].[COURSE] (
[Course_ID]
)
GO

ALTER TABLE IS_REGISTERED ADD
CONSTRAINT [PK_IS_REGISTERED_STUDENT] PRIMARY KEY(Student_ID, Section_ID)

GO

ALTER TABLE IS_QUALIFIED ADD
CONSTRAINT [PK_IS_QUALIFIED_FACULTY] PRIMARY KEY(Faculty_ID, Course_ID)
<ad**********@marketsquaresolutions.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How can you indicate that a FOREIGN KEY constraint references two
columns in two different tables?

"SQL Server Books Online" show an example of how to reference two
columns in the SAME table:
REFERENCES ref_table [ ( ref_column [ ,...n ] )

Here is the error and the 'bad' SQL code:

Server: Msg 8148, Level 16, State 1, Line 4
More than one column FOREIGN KEY constraint specified for column
'UserOrGroupId', table 'salesforce3.dbo.AccountShare'.

CREATE TABLE salesforce3.dbo."AccountShare" ("Id" varchar(18) PRIMARY
KEY , ... , "UserOrGroupId" varchar(18) CONSTRAINT
FK_UserOrGroupId6349 FOREIGN KEY REFERENCES "User"(Id) REFERENCES
"Group"(Id) , ... )

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gunnar Vøyenli | last post by:
Hi (SQL Server 2000) I have an existing table (t) with a column that is NOT an identity column (t.ID), but it has manually inserted "row numbers". I want to make this column become an identity...
2
by: Beacher | last post by:
Hello, I'm trying to create a relationship between two tables, a one to many tblDeal being the primary table, I'm trying to create a relationship between tblDeal and tblCondition .. i set...
2
by: | last post by:
Hello All, I am having a lot of difficulty trying to bind a templated column, that is programmatically created for a datagrid, to a datasource column. I have a datasource containing 2 columns,...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
1
by: Ken Varn | last post by:
I have a problem where my DataGrid would not maintain the ViewState of my databound rows. I finally narrowed down the problem. If my first column is a template column, the view state for the...
7
by: am72de | last post by:
Hi all, I've posted this problem some weeks ago, but noone had a solution. Perhaps now someone could help me. I have the following tables: Create Table T1 ( ID1 int Not Null , ID2 int Not...
0
by: chadsmith76 | last post by:
I am trying to find some documentation on using things like ORDER BY 5*(column LIKE ' term') + 4*(column LIKE ' term') I don't even know what it is called so it is difficult to search. My...
31
by: Sarita | last post by:
Hello, this might sound stupid, but I got a really nice homepage template which unfortunately is a 3-Column Fixed Width CSS format. Now I don't have any content for the right column and would...
3
by: NewlytoSQL | last post by:
Hi all, im fairly new to SQL and im stuck half way through a query, im using DB2 here is what im tryng to do. i have a query that brings back an item number , shelf req, sum of all orders columns,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.