473,769 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.db o.AccountShare' .

CREATE TABLE salesforce3.dbo ."AccountSha re" ("Id" varchar(18) PRIMARY
KEY , ... , "UserOrGrou pId" varchar(18) CONSTRAINT
FK_UserOrGroupI d6349 FOREIGN KEY REFERENCES "User"(Id) REFERENCES
"Group"(Id) , ... )

Jul 23 '05 #1
2 26998
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 ."AccountSha re"
("Id" varchar(18) PRIMARY KEY
, ...
, "UserOrGrou pId" 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**********@ma rketsquaresolut ions.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.db o.AccountShare' .

CREATE TABLE salesforce3.dbo ."AccountSha re" ("Id" varchar(18) PRIMARY
KEY , ... , "UserOrGrou pId" varchar(18) CONSTRAINT
FK_UserOrGroupI d6349 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_REGISTERE D_SECTION] FOREIGN KEY
(
[Section_ID]
) REFERENCES [dbo].[SECTION] (
[Section_ID]
),
CONSTRAINT [STUDENT_IS_REGI STERED] FOREIGN KEY
(
[Student_ID]
) REFERENCES [dbo].[STUDENT] (
[Student_ID]
)
GO

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

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

ALTER TABLE IS_REGISTERED ADD
CONSTRAINT [PK_IS_REGISTERE D_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**********@m arketsquaresolu tions.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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.db o.AccountShare' .

CREATE TABLE salesforce3.dbo ."AccountSha re" ("Id" varchar(18) PRIMARY
KEY , ... , "UserOrGrou pId" varchar(18) CONSTRAINT
FK_UserOrGroupI d6349 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
19069
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 column. This column is a key field to other tables, so I want to keep the row numbers that are allready inserted. From the Query Analyzer, how do I do this?
2
2032
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 tbldeal as the primary table, select DealID and select DealID from tblCondition, but when it goes to save it I get this error: 'tblDeal (chrisb)' table saved successfully 'tblCondition (chrisb)' table
2
2114
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, ID and VALUE. I would like to create a templated column in the datagrid that is bound to the VALUE column of the datasource.
2
12961
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 'FK_tblOfficePageContent_tblPageContent'. The conflict occurred in database 'CPNCMS', table 'tblPageContent', column 'pageID'. The statement has been terminated. I cant seem to see why I keep getting an error when I do the insert!
1
1878
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 DataGrid items is not maintained on postback for some reason. I basically re-created my DataGrid again just to make sure this was indeed the problem. The ViewState was fine until I added the template column at the beginning of the Columns list. ...
7
2947
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 Null
0
2324
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 questions is... Is the value before the * like a multiplier? So I could give more weight to one column than another by raising that number? I am trying to return a result that searches 5 or 6 columns and column 1 needs to have by far the most weight,...
31
3107
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 like the middle column just to use up that space instead. Any way this is possible? Thanks for your help, it is highly appreciated!
3
3561
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, based on the shelf req column the item number column has more than one row for the same item number therefore my sum of all orders is summing them based on shelf req which i need, what i cant get is how to keep this info plus get the sum of all...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.