Solved

SQL Self Join PK on PK?

Posted on 2014-02-15
7
259 Views
Last Modified: 2014-03-03
I'm looking at database in Sql Mgmt Studio in a database diagram and noticed one table which self joins on it's PK (PK -> PK). The PK is never used again to link another table, although it does contain foriegn keys which  other tables link to it.

Q. Why did the former db dev so this?
0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39862083
Why did the former db dev so this?

This is a tough one!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39862172
I agree.  This is pure conjecture on my part, bu I suspect a "recursive" table.  For example, a table of employees where one employee reports to someone who in turn reports to someone else and all the way up the food chain.
0
 
LVL 15

Assisted Solution

by:Deepak Chauhan
Deepak Chauhan earned 150 total points
ID: 39862190
Primary key is not only just to link another table.  

It can either be a normal attribute that is guaranteed to be unique. When you specify a PRIMARY KEY constraint for a table, by default a clustered index is created for the primary key columns. This index also permits fast access to data when the primary key is used in queries. So this can be use to avoid NULL values, Duplicate values.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WorknHardr
ID: 39862808
I understand a self-join table where a PK links to a FK. For example, unlimited Parent-Child row data for a treeview. I've also read about speeding-up self-reference queries. I'm guessing its a fix instead of good solution.
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 150 total points
ID: 39862931
For this to make a lot of sense, there needs to be a relationship between two (or more) rows that can be satisfied by the primary key.

Manger/Employee doesn't fit this well as there can be an arbitrary number of employees per manager and each manager can be one of an arbitrary number of employees that report to a higher level manager.  This is best solved by an additional column with the primary key of the row's manager.

Invoices have the same general behavior in that the number of items that make up the invoice is typically arbitrary.

Inventory, assembly, etc.  all fall into this category.


So the key is understanding the data in the table.  It could be that the data is paired by consecutive ID values (0,1), (2, 3), (4, 5), etc.

  SELECT * FROM t as t0
  INNER JOIN t as t1 ON t0.id / 2 = t1.id / 2;


It could be that the data is static and that the PK relationship is a binary tree.

  SELECT * FROM t as parent
  LEFT JOIN t as lchild on parent.id *2 = lchild.id
  LEFT JOIN t as rchild on parent.id * 2 + 1 = rchild.id;


To understand why this was designed as it is, we really need to know what the data is.

Kent
0
 

Author Comment

by:WorknHardr
ID: 39877381
Sorry for the delay. Here's the table create and data...

Note: Only 4 rows in the table!

[Create Table]
CREATE TABLE [dbo].[Types]
(
      [id] [int] IDENTITY(1,1) NOT NULL,
      [content_id] [int] NOT NULL,
      [details] [varchar](max) NULL
)

ALTER TABLE [dbo].[Types]  WITH CHECK
ADD  CONSTRAINT [FK_Types_Types] FOREIGN KEY([id])
REFERENCES [dbo].[_Types] ([id])
GO

ALTER TABLE [dbo].[Types] CHECK CONSTRAINT [FK_Types_Types]
GO

[Data]
id      content_id       details
11      14                       These details are for the benefit of....
12      28                       nothing
23      4                       Use this form for prior...
24      31                       NULL
0
 

Author Closing Comment

by:WorknHardr
ID: 39900290
Thx
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question