SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Primary-Foreign Key


Primary-Foreign Key

Author
Message
RonKyle
RonKyle
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7040 Visits: 3621
An interesting design challenge. I don't have a good proposal off the top of my head, but will think about this. One thing your proposed solution would run into, however, is that in the employee table a manager would have to have the correct department for which he as been identified as the manager in the dept table.

Actually, now that I've had a minute to think about this, what about the following.

tblEmployee
EmployeeID PK
DeptID FK references tblDept

tblDept
DeptID PK

tblDeptManager
DeptID PK, FK reference tblDept
ManagerID,
FK ManagerID, DeptID reference tblEmployee EmployeeID, DeptID

It's almost as if you need a facilitating table used in a M=M construction. In this case you are facilitating a 1=M where part of the relationship in the parent comes from the child.

I still will think about this a little more to see if there's a way to do this w/o using an additional table. If you assume that the dept is the parent, as there's many employees in a dept but not the other way around, maybe a two field foreign relation key is the solution, although I can't see that one working at the moment.



sqlvogel
sqlvogel
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2118 Visits: 3708
RonKyle,

Your design still allows departments without managers. It's easy to create a one-to-one mapping using a table with two candidate keys:

CREATE TABLE Dept
(DeptCode INT NOT NULL, DeptName VARCHAR(50) NOT NULL, EmployeeNumber INT NOT NULL,
PRIMARY KEY (DeptCode),
UNIQUE (EmployeeNumber));

This doesn't ensure that every Manager belongs to a department however. To do that you would need a similar constraint on the Manager table but then you can't populate either table without disabling the constraint(s). The problem is that SQL lacks the ability to update multiple tables simultaneously (multiple assignment). Without multiple assignment SQL's ability to support multi-table constraints is severely limited.
RonKyle
RonKyle
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7040 Visits: 3621
I don't think you can avoid a design that does that through the database design, if only because somehting has to go in first.. It does, however, account for the fact that the relationship between Dept and Employee is 1-M, but the relationship between Dept and Manager is 1-1. They can't therefore be in the same entity.

Here is one possibity you might try: create a view with only the managers. Relate the Dept table to the view rather than back at the table. I only rarely use views, but this might be worth an attempt. If you try this, I'd be interested in finding out the result.



Pete Cox
Pete Cox
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 277
Cute problem. Not really a real world situation though, a department will not always have a manager and being a manager is a role of an employee
sqlvogel
sqlvogel
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2118 Visits: 3708
It was just intended as a simple example of a common situation. If you prefer then think about the example where an Order must contain at least one Order Item, which is the same generic problem.

The SQL committee thought the problem serious enough that they created a (not very good) workaround in the form of "deferrable" constraints, but that's a feature that isn't supported by SQL Server (no bad thing in my opinion, I'm not really a fan of deferrable constraints).

It's interesting that virtually all data modelling languages (UML, ORM, IDEF1X for example) make the distinction between mandatory and optional references but SQL does not because for most practical purposes it has no way to implement the mandatory ones.
Pete Cox
Pete Cox
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 277
Thanks for the perspective
keshabheda
keshabheda
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 4
Hi All,

I ran across the issue which is stated in this topic. Two tables having primary key as foreign key to each other.

My problem is I want to insert records in both tables with a single insert statement.
Something like this Insert Into Table1(1,
Select PKID From Table2 WHERE PKID = Insert Into Table2(1, ?)
)

Is this Possible ?

What is the best way to go about it ?


----------------------
DB Example :
----------------------


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table1](
[PKID] [int] NOT NULL,
[FKID] [int] NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[PKID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table2](
[PKID] [int] NOT NULL,
[FKID] [int] NULL,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
[PKID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


GO
ALTER TABLE [dbo].[Table1] WITH CHECK ADD CONSTRAINT [FK_Table1_Table2] FOREIGN KEY([FKID])
REFERENCES [dbo].[Table2] ([PKID])
GO
ALTER TABLE [dbo].[Table1] CHECK CONSTRAINT [FK_Table1_Table2]


GO
ALTER TABLE [dbo].[Table2] WITH CHECK ADD CONSTRAINT [FK_Table2_Table1] FOREIGN KEY([FKID])
REFERENCES [dbo].[Table1] ([PKID])
GO
ALTER TABLE [dbo].[Table2] CHECK CONSTRAINT [FK_Table2_Table1]


Regards,
Keshav
sqlvogel
sqlvogel
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2118 Visits: 3708
keshabheda (4/21/2011)
Hi All,

I ran across the issue which is stated in this topic. Two tables having primary key as foreign key to each other.

My problem is I want to insert records in both tables with a single insert statement.
Something like this Insert Into Table1(1,
Select PKID From Table2 WHERE PKID = Insert Into Table2(1, ?)
)

Is this Possible ?

What is the best way to go about it ?


You would have to do two inserts. Although you can insert to two tables with one statement you cannot do that if there is a foreign key constraint between them.

In your case both foreign keys are nullable so you have the option of inserting nulls into both tables and then updating the foreign key with the correct values. An alternative is to create a new table for the two foreign key columns and remove them from Table1 and Table2.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search