Solved

SQL PrimaryKey best practice

Posted on 2011-03-16
5
254 Views
Last Modified: 2012-05-11
I am creating a table ProjectSites that will be a child table to another table Projects and it will only have three columns, here is the current schema

ProjectSites Table:

ProjectSiteID          identity  pk
ProjectID                int          fk
SiteID                     int          fk

So the idea is, there can be one or more Sites assigned to every project. There will never be a case where the same Site is assigned more than once to the same Project. My question is this, Should I make the PK of the table a combination of all three columns so that no mistakes can ever be made?
0
Comment
Question by:AkAlan
  • 2
  • 2
5 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 333 total points
ID: 35149761
Yes that's the only way to meet all your conditions.
0
 
LVL 6

Author Comment

by:AkAlan
ID: 35149770
Ok, I thought that was so, I was just thinking about any performance issues that could cause. Thanks.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 333 total points
ID: 35149796
Will it impact performance? yes but  marginally given they are int columns. It is unavoidable given the requirements specified and best practice
0
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 167 total points
ID: 35149831
You can also Add Unique constraint to two of the fields i.e. other than one PK field.
0
 
LVL 6

Author Closing Comment

by:AkAlan
ID: 35162240
Thanks to both.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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