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»»

Database Design - Need help in Choosing a primary key Expand / Collapse
Author
Message
Posted Wednesday, April 30, 2014 9:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:17 AM
Points: 9, Visits: 97
Hi All,

I am kind of confused. I need some help in choosing a primary key for the database which i am designing.

I have few tables which contains 5 -15 fields out of it 3 - 9 columns combined to form the uniqueness of the row.

All are un-related tables. Three parent tables connect with 20 child non-related child tables.

I believe it would not be a wise choice to choose 3 to 9 fields for primary key.
But if i use an auto increment as a key will there be of any use as it might never be used to fetch rows. Then why do i still have to go with that?

Or Is it ok to create a primary key of upto 5 attributes?

Can someone help me on this?

Thanks.
Post #1566460
Posted Wednesday, April 30, 2014 10:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 10:44 AM
Points: 1,288, Visits: 1,859
It all depends on your business rules.
If the rule is that you must identify each row uniquely within a table then you need the default primary key.
How will you join 3 tables with 20 child tables?


Alex S
Post #1566479
Posted Wednesday, April 30, 2014 10:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:17 AM
Points: 9, Visits: 97
Thanks,

For example, user id is used as a linkage across multiple tables.

I am concerned about defining a key for the child tables as it requires 3- 9 columns to make the row unique. Will that be optimal?

Or defining a surrogate key will be of any use?
Post #1566485
Posted Wednesday, April 30, 2014 10:25 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:39 PM
Points: 15,618, Visits: 27,998
shafirmubarak (4/30/2014)
Thanks,

For example, user id is used as a linkage across multiple tables.

I am concerned about defining a key for the child tables as it requires 3- 9 columns to make the row unique. Will that be optimal?

Or defining a surrogate key will be of any use?


If 3-9 columns make the row unique, even if you create a surrogate key, you'll still need a unique constraint to ensure the business model is accurately reflected in your data.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1566494
Posted Wednesday, April 30, 2014 10:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:17 AM
Points: 9, Visits: 97
Yes, the unique enforcement would be absolute necessary.

But i was not sure how surrogate key will be of use?
Post #1566496
Posted Wednesday, April 30, 2014 11:01 AM This worked for the OP Answer marked as solution


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:39 PM
Points: 15,618, Visits: 27,998
shafirmubarak (4/30/2014)
Yes, the unique enforcement would be absolute necessary.

But i was not sure how surrogate key will be of use?


In some situations it will help performance to have a narrower index. But that's just a general observation. It's hard to be more specific with no details.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1566511
Posted Wednesday, April 30, 2014 12:24 PM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:00 PM
Points: 13,224, Visits: 12,068
Grant Fritchey (4/30/2014)
shafirmubarak (4/30/2014)
Yes, the unique enforcement would be absolute necessary.

But i was not sure how surrogate key will be of use?


In some situations it will help performance to have a narrower index. But that's just a general observation. It's hard to be more specific with no details.


It can also make sql development a little easier. You would have only 1 parameter for identification of the row instead of 5. Also your where clause is only 1 predicate instead of 5. Now I wouldn't recommend designing your database around ease of coding but that is a difference.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1566548
Posted Wednesday, April 30, 2014 12:49 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 2:09 PM
Points: 132, Visits: 842
The number one rule of a primary key is that it must be "meaningless" yet unique. To be meaningless it must not be one of your four columns that are under user control. Your four columns can and should be an Alternate Key (unique constraint/index).

I've gotten away from Identities as primary keys and I feel liberated. Identities are non-ANSI and prevent batch insertion. Personally, I prefer uniqueidentifiers for the PK.

All non-clustered indexes automatically include the primary key so if you want skinny indexes use a single column for your primary key.

Post #1566556
Posted Wednesday, April 30, 2014 12:59 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:00 PM
Points: 13,224, Visits: 12,068
Bill Talada (4/30/2014)
The number one rule of a primary key is that it must be "meaningless" yet unique. To be meaningless it must not be one of your four columns that are under user control. Your four columns can and should be an Alternate Key (unique constraint/index).

I've gotten away from Identities as primary keys and I feel liberated. Identities are non-ANSI and prevent batch insertion. Personally, I prefer uniqueidentifiers for the PK.

All non-clustered indexes automatically include the primary key so if you want skinny indexes use a single column for your primary key.



Why a guid over an identity? They take up a lot more storage and are a complete PITA to debug. Non-ANSI honestly doesn't really matter because database agnostic is a farce.

I am not sure how switching from one type of generated value to a different (and more painful) one makes you feel liberated.

What do you mean that an identity prevents batch insertion? That is simply not true.

Also, if you are using a super big column like a guid for a primary key you need to make sure your primary key is NOT clustered. You need to find something else to use as your clustered index.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1566565
Posted Wednesday, April 30, 2014 1:03 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:00 PM
Points: 13,224, Visits: 12,068

The number one rule of a primary key is that it must be "meaningless" yet unique.


This is a matter of opinion. There is no reason a primary key MUST be a magic value. That is what surrogate keys do. By your definition you have completely ruled out any natural keys.

It should however be a value that is constant. A good example is an automobile. They all have a VIN. It is constant, it is presented to the user AND it makes a perfect primary key for a table that holds information about cars.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1566566
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse