Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Identity Key vs Primary Key

Author  Topic 

aoriju
Posting Yak Master

156 Posts

Posted - 2010-07-29 : 08:16:57
Dear all
I have a table with Item_Id as Identity(1,1)
Is any advantage while i am setting this Item_Id as Primary key

Both of them not allow duplicate values....then what's the diff ?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-29 : 08:28:39
http://www.sqlteam.com/article/identity-and-primary-keys


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2010-07-29 : 08:37:25
This article summarise the Difference between Identity key and Primary Key.
My Question is simple.What i am trying to ask is

Any relevance while setting identity key as Primary Key ?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-29 : 08:48:11
One idea is:
You can't replicate a table if it has no primary key.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-29 : 09:15:37
It's called a surrogate key, and only has real value for updating rows...

Would you for example, have an IDENTITY Column on a stae code table, and store the identity value in all you other tables?

Or would you make the stated Code the Primary key.

A state Code is what is considered a Natural Key.

So the debate has always raged on about Surogate vs. Natural Keys.

And I think the camps are split 50/50

M$ for example uses surrogate keys (ad nauseum) for it's catalog for SQL Server...and they have found a mirard of problems in doing that...ever dump a database and have to "fix" users?

Ever look at the stored procedure sp_help?

(I like the comment in the code where it says..."give up"...a personal favorite)

http://weblogs.sqlteam.com/brettk/archive/2004/06/09/1530.aspx



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-07-29 : 09:36:46
quote:
Originally posted by webfred

One idea is:
You can't replicate a table if it has no primary key.


No, you're never too old to Yak'n'Roll if you're too young to die.



Only true for transactional replication.But for merge & snapshot its not necessary.


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-29 : 09:42:55
quote:
Originally posted by Idera

quote:
Originally posted by webfred

One idea is:
You can't replicate a table if it has no primary key.


No, you're never too old to Yak'n'Roll if you're too young to die.



Only true for transactional replication.But for merge & snapshot its not necessary.


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH


right.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-07-29 : 11:32:08
Just to be clear, there is no such thing as an Identity Key. So, is your real question about placing a Unique or Primary Key constraint on an Identity column? Possibly if it should be clustered or not?

If so, then one difference is that Primary Key constraints do not allow NULLs. Where as a Unique Constraint will allow a NULL value. But, if you are going to surrogate, as Brett mentioned, then I think only makes sense to create the Identity column as a Primary Key.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-29 : 12:01:21
quote:
Originally posted by Lamprey
so, then one difference is that Primary Key constraints do not allow NULLs.



I don't think that's true anymore


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-07-29 : 12:06:28
quote:
Originally posted by X002548

quote:
Originally posted by Lamprey
so, then one difference is that Primary Key constraints do not allow NULLs.



I don't think that's true anymore
It certainly is true, it's the definition of primary key. Try and put one on a nullable column and see.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-29 : 12:13:00
maybe it's a unique that allows nulls?

I thought sql server added that recently...DB2 had tis for a long time...

How else does SQL Server establish a Non Identifying relationshp?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-07-29 : 13:24:45
quote:
Originally posted by X002548

maybe it's a unique that allows nulls?
<snip>
Correct. That's why I said:

quote:
Originally posted by Lamprey

If so, then one difference is that Primary Key constraints do not allow NULLs. Where as a Unique Constraint will allow a NULL value.

Go to Top of Page

Celko
Starting Member

23 Posts

Posted - 2010-07-29 : 13:34:50
quote:
Originally posted by aoriju

This article summarise the Difference between Identity key and Primary Key. My Question is simple.What i am trying to ask is Any relevance while setting identity key as Primary Key ?


Since an IDENTITY is a non-relational, proprietary piece of physical metadata, it can never be a key in a properly designed schema. It tells the experienced SQL programmer he has to clean up a bad design and find the real key.

Author of
SQL for Smarties
Thinking in Sets
SQL Puzzles and Answers
Analytics and OLAP in SQL
SQL Programming Style
Standards in SQL
Data & Databases
Trees & Hierarchies in SQL
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-29 : 13:42:05
quote:
Originally posted by Celko

quote:
Originally posted by aoriju

This article summarise the Difference between Identity key and Primary Key. My Question is simple.What i am trying to ask is Any relevance while setting identity key as Primary Key ?


Since an IDENTITY is a non-relational, proprietary piece of physical metadata, it can never be a key in a properly designed schema. It tells the experienced SQL programmer he has to clean up a bad design and find the real key.

Author of
SQL for Smarties
Thinking in Sets
SQL Puzzles and Answers
Analytics and OLAP in SQL
SQL Programming Style
Standards in SQL
Data & Databases
Trees & Hierarchies in SQL




ding...ding...ding...."And in this Corner, weighing in at (SELECT Weight FROM Person WHERE Name = 'Celko'), Joe Celko"




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-03 : 04:34:34
quote:
Originally posted by aoriju

Dear all
I have a table with Item_Id as Identity(1,1)
Is any advantage while i am setting this Item_Id as Primary key

Both of them not allow duplicate values....then what's the diff ?


Identity column without primary or unique key will allow duplicates

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2010-08-03 : 09:42:20
quote:
Originally posted by Celko
Since an IDENTITY is a non-relational, proprietary piece of physical metadata, it can never be a key in a properly designed schema. It tells the experienced SQL programmer he has to clean up a bad design and find the real key.


I'd clarify that it indicates a bad design if your IDENTITY is the ONLY unique key on your table. Each table should have a natural key inherent in the data (and enforced by constraint), but may also include a surrogate key for programmatic or design purposes.
Having an IDENTITY defined on a table does not in itself indicate poor design.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-03 : 09:56:00
Takin on the Joe man...

IDENTITY...avoid completley, except where you can't

Anyone got a good natural key PK for an address table?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-08-03 : 10:09:58
quote:
Anyone got a good natural key PK for an address table
Well, don't know if it's a good one, but if you parse the street number, apt./unit, street name, city, state/province and ZIP or postal code into separate columns, you should do OK with that. Of course if you have to join it to something else it begs for a surrogate key.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-03 : 10:23:48
quote:
Originally posted by X002548


Anyone got a good natural key PK for an address table?



in the UK : postcode + either house number, or house name, is unique - I think! not 100% sure though.

UK Postcode is a "postman's walk" or somesuch - so typically one side of a street in town, or one lane of houses in the country.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-03 : 10:26:18
Well, street#/Street/Zip should be good enough

But this is one area where I use surrogates...with a unique AK on the above

Indexes on City/State, and State

Hoping for Index Intersection

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -