﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / Primary-Foreign Key / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 02:03:51 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>[quote][b]keshabheda (4/21/2011)[/b][hr]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 ?[/quote]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.</description><pubDate>Thu, 21 Apr 2011 06:47:36 GMT</pubDate><dc:creator>sqlvogel</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>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 ONGOSET QUOTED_IDENTIFIER ONGOCREATE 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 ONGOSET QUOTED_IDENTIFIER ONGOCREATE 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]GOALTER TABLE [dbo].[Table1]  WITH CHECK ADD  CONSTRAINT [FK_Table1_Table2] FOREIGN KEY([FKID])REFERENCES [dbo].[Table2] ([PKID])GOALTER TABLE [dbo].[Table1] CHECK CONSTRAINT [FK_Table1_Table2]GOALTER TABLE [dbo].[Table2]  WITH CHECK ADD  CONSTRAINT [FK_Table2_Table1] FOREIGN KEY([FKID])REFERENCES [dbo].[Table1] ([PKID])GOALTER TABLE [dbo].[Table2] CHECK CONSTRAINT [FK_Table2_Table1]Regards,Keshav</description><pubDate>Thu, 21 Apr 2011 06:15:52 GMT</pubDate><dc:creator>keshabheda</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>Thanks for the perspective</description><pubDate>Thu, 03 Jun 2010 04:18:53 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>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.</description><pubDate>Thu, 03 Jun 2010 04:12:51 GMT</pubDate><dc:creator>sqlvogel</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>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</description><pubDate>Thu, 03 Jun 2010 03:36:10 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>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.</description><pubDate>Tue, 01 Jun 2010 05:25:30 GMT</pubDate><dc:creator>RonKyle</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>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.</description><pubDate>Tue, 01 Jun 2010 00:00:00 GMT</pubDate><dc:creator>sqlvogel</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>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.tblEmployeeEmployeeID  PKDeptID FK references tblDepttblDeptDeptID PKtblDeptManagerDeptID PK, FK reference tblDeptManagerID,FK ManagerID, DeptID reference tblEmployee EmployeeID, DeptIDIt'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.</description><pubDate>Mon, 31 May 2010 22:22:50 GMT</pubDate><dc:creator>RonKyle</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>I don't know about wrong or not but it's a different example from the ones I had in mind.Take a Manager table and a Department table (obviously manager may not be the same thing as employee - not all employees are managers). Now enforce the rule that each department has one manager and each manager has one department.This is a special case of a 1-N relationship where N=1. In general most 1-N relationships are not easily supported in SQL unless N is allowed to be zero. That's because SQL FOREIGN KEY constraints are always optional on the referencing side of the relationship, ie the "parent" row must exist but a referencing row does not have to exist.</description><pubDate>Mon, 31 May 2010 03:40:55 GMT</pubDate><dc:creator>sqlvogel</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>OK. Let me give an example. I have one tblEmployee table with EmployeeID as Primary Key and another column DepartmentID. Another tblDepartment with DepartmentID as Primary Key and another column ManagerID as Foreign Key from the Employee table. The column DepartmentID in tblEmployee is the foreign Key from the tblDepartment. Now the above mentioned simple design is implementable and logical. If not, plz let me know. I am expecting many employees against one department. But One department will have only one manager and his ID is refered in the tblDepartment (this will be done by implemented logic). Secondly, you are right, the logic to keep the tables related two ways should be coded properly. But still, the data in both tables is insertable independently (keeping the Foreign key column NULL).Please correct me if I am wrong.</description><pubDate>Mon, 31 May 2010 03:27:19 GMT</pubDate><dc:creator>Atif-ullah Sheikh</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>[quote]Any specific example? There might be but not seen any situation yet for circular relationships.[/quote]I expect you have come across relationships that ought to be mandatory in both directions. It's just that SQL makes it hard or impossible to implement them declaratively and so database designers have got into the habit of ignoring them or having them implemented in application code. Take almost any example of a multi-table relationship that includes the words "at least" or "exactly": Order must have at least one Order Item; every Employee must be assigned to exactly one Department and every Department must contain at least one Employee; every Branch requires exactly one Branch Manager.</description><pubDate>Mon, 31 May 2010 02:45:22 GMT</pubDate><dc:creator>sqlvogel</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>[quote][b]David Portas (5/28/2010)[/b][hr]As Ron says, it's not necessarily wrong from a logical perspective. In fact, relationships of this kind which are mandatory in both directions are perfectly common in data modelling and in business requirements - it's just that SQL Server is a poor tool for implementing such rules.For practical reasons it's usually necessary to compromise in some way. For example you could populate the tables with some initial data before enabling the constraint(s) or you could remove one of the constraints altogether.[/quote]Any specific example? There might be but not seen any situation yet for circular relationships.</description><pubDate>Sun, 30 May 2010 22:33:52 GMT</pubDate><dc:creator>Atif-ullah Sheikh</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>As Ron says, it's not necessarily wrong from a logical perspective. In fact, relationships of this kind which are mandatory in both directions are perfectly common in data modelling and in business requirements - it's just that SQL Server is a poor tool for implementing such rules.For practical reasons it's usually necessary to compromise in some way. For example you could populate the tables with some initial data before enabling the constraint(s) or you could remove one of the constraints altogether.</description><pubDate>Fri, 28 May 2010 07:56:36 GMT</pubDate><dc:creator>sqlvogel</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>Without knowning the entities represented by the tables, no answer can be given for why it's wrong, or even that it is wrong, although it probably is.  If you tell us what the tables represent, it should be possible to suggest an easier method to model the relationship.</description><pubDate>Fri, 28 May 2010 05:37:17 GMT</pubDate><dc:creator>RonKyle</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>Its wrong design but Why not?[code="sql"]SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE 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]GOALTER TABLE [dbo].[Table1]  WITH CHECK ADD  CONSTRAINT [FK_Table1_Table2] FOREIGN KEY([FKID])REFERENCES [dbo].[Table2] ([PKID])GOALTER TABLE [dbo].[Table1] CHECK CONSTRAINT [FK_Table1_Table2]SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE 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]GOALTER TABLE [dbo].[Table2]  WITH CHECK ADD  CONSTRAINT [FK_Table2_Table1] FOREIGN KEY([FKID])REFERENCES [dbo].[Table1] ([PKID])GOALTER TABLE [dbo].[Table2] CHECK CONSTRAINT [FK_Table2_Table1]GOInsert into Table2	Select 1,NULL[/code]The commands execute successfully.Am I missing something?</description><pubDate>Thu, 27 May 2010 06:13:37 GMT</pubDate><dc:creator>Atif-ullah Sheikh</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>That's a completely circular reference. While you can set those up, they're impossible to maintain.</description><pubDate>Tue, 25 May 2010 06:06:47 GMT</pubDate><dc:creator>Grant Fritchey</dc:creator></item><item><title>RE: Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>There is no way to do this simply.This is a bad design.To enable this, you will have to drop the foreign keys, do the insert (s) then recreate the foreign keys WITH(CHECK)</description><pubDate>Tue, 25 May 2010 04:32:50 GMT</pubDate><dc:creator>Stewart "Arturius" Campbell</dc:creator></item><item><title>Primary-Foreign Key</title><link>http://www.sqlservercentral.com/Forums/Topic927314-145-1.aspx</link><description>Hai,There are 2 tables. Table 1 contains col1 as primary key and col2 as foreign key refers to Table 2 - col1. The same way Table 2 contains col1 as primary key and col2 refers to Table 1 - col 1.Can anyone tell me how can we insert values in Table 2?</description><pubDate>Tue, 25 May 2010 03:18:14 GMT</pubDate><dc:creator>Krish-853432</dc:creator></item></channel></rss>