Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Primary-Foreign Key Expand / Collapse
Author
Message
Posted Monday, May 31, 2010 10:22 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:41 PM
Points: 815, Visits: 2,020
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.



Post #930519
Posted Tuesday, June 1, 2010 12:00 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Saturday, August 30, 2014 11:41 PM
Points: 448, Visits: 3,338
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.


David
Post #930535
Posted Tuesday, June 1, 2010 5:25 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:41 PM
Points: 815, Visits: 2,020
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.



Post #930622
Posted Thursday, June 3, 2010 3:36 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, November 14, 2013 1:05 AM
Points: 96, 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
Post #931948
Posted Thursday, June 3, 2010 4:12 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Saturday, August 30, 2014 11:41 PM
Points: 448, Visits: 3,338
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.


David
Post #931964
Posted Thursday, June 3, 2010 4:18 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, November 14, 2013 1:05 AM
Points: 96, Visits: 277
Thanks for the perspective
Post #931967
Posted Thursday, April 21, 2011 6:15 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 1, 2012 12:23 AM
Points: 27, 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
Post #1096926
Posted Thursday, April 21, 2011 6:47 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Saturday, August 30, 2014 11:41 PM
Points: 448, Visits: 3,338
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.


David
Post #1096942
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse