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

Primary Keys Expand / Collapse
Author
Message
Posted Tuesday, March 9, 2010 9:20 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 9:02 AM
Points: 2,040, Visits: 1,668
Comments posted to this topic are about the item Primary Keys

Paul Randal
CEO, SQLskills.com: Check out SQLskills online training!
Blog:www.SQLskills.com/blogs/paul Twitter: @PaulRandal
SQL MVP, Microsoft RD, Contributing Editor of TechNet Magazine
Author of DBCC CHECKDB/repair (and other Storage Engine) code of SQL Server 2005
Post #879902
Posted Tuesday, March 9, 2010 9:35 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 31, 2014 6:28 AM
Points: 1,426, Visits: 342
Tricky question!
Post #879907
Posted Tuesday, March 9, 2010 9:46 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:15 PM
Points: 17,823, Visits: 15,755
Nice question. Keep 'em coming.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #879914
Posted Tuesday, March 9, 2010 9:51 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, July 3, 2014 9:19 PM
Points: 505, Visits: 1,691
A thought provoking question, but I'm not too keen on the answer. Unless Paul would like to pay for my airfares???

S.


Post #879920
Posted Tuesday, March 9, 2010 11:26 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, July 26, 2013 2:49 AM
Points: 18, Visits: 41
A Primary Key is always enforced by a clustered index at creation?
Correct answer (SQL Server Central) : False

I would just like to add a small point, whenever you create a Primary Key, by default its ALWAYS going to be Clustered. Unless you create a Non Clustered Index Primary Key.

So, when you are saying that IF a Clustered Index already exists, then your what you are mentioning is valid. Else the Primary key, which is usually created at when you are creating the table, would be Clustered index unless specified.

Kindly reply if there is something else to add or I haev misquoted anywhere.

Warm regards,
Sameer Chachad.



Post #879949
Posted Wednesday, March 10, 2010 12:30 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Thursday, April 10, 2014 9:41 AM
Points: 695, Visits: 264
hi sameer,

nice question.... you r question it self has the answwer..

The actual question asked is

A Primary Key is always enforced by a clustered index at creation?

which is not always done, it is enforced by a clustered index only when there is no clustered index existing on that table. If there is an clustered index existing in that table, the primary key u r going to create will be enforced by a non clustered index.

please notice the word "always" in the question...



Anil Kubireddi
Post #879977
Posted Wednesday, March 10, 2010 1:03 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:03 PM
Points: 1,334, Visits: 3,069
A Primary Key is created by defaut as a Clustered Index, unless a Clustered Index already exists on the table, which is really to say that the Clustered Index does not have to necessarily be the Primary Key. It is the semantics of how you are phrasing the question that is a little tricky and confusing for some. However, as you state Paul in your explanation, the more important piece to emphasize here is the reasoning of where you decide to put your Clustered Index on your table. Application functionality can drive this at times and a PK is not always the first choice . That said, primary keys do tend to make make good candiates for the CI simply because they and their auto-incrementing data (singleton PK columns are many times IDENTITY columns as well, although not required to be) automatically satisfy two main requirements of a Primary Key. NO NULLS and Unique data, where Unique Keys can allow one NULL and are created Non-Clustered by default and can have more than one UK on a table I believe. Both PK's and UK's can be made up of more than one column as well. . :)

"Technology is a weird thing. It brings you great gifts with one hand, and it stabs you in the back with the other. ..."
Post #879987
Posted Wednesday, March 10, 2010 1:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:52 PM
Points: 6,048, Visits: 8,328
sameerchachad 69959 (3/9/2010)
A Primary Key is always enforced by a clustered index at creation?
Correct answer (SQL Server Central) : False

I would just like to add a small point, whenever you create a Primary Key, by default its ALWAYS going to be Clustered. Unless you create a Non Clustered Index Primary Key.

So, when you are saying that IF a Clustered Index already exists, then your what you are mentioning is valid. Else the Primary key, which is usually created at when you are creating the table, would be Clustered index unless specified.

Kindly reply if there is something else to add or I haev misquoted anywhere.

Warm regards,
Sameer Chachad.


The answer given by Paul is correct but incomplete. The explanation correctly describes what happens if you do not specify a clustering option for the PRIMARY KEY constraint, but it doesn't include the extra possibilities that arise from adding the keyword CLUSTERED or NONCLUSTERED to the constraint.

These are the scenarios that are possible when a PRIMARY KEY constraint is created:

1) No clustered index exists for the table, no clustering option specified for the constraint --> Clustered index is created. (This is probably the most common scenario)
2) No clustered index exists for the table, CLUSTERED option specified for the constraint --> Clustered index is created.
3) No clustered index exists for the table, NONCLUSTERED option specified for the constraint --> Non-clustered index is created.
4) A clustered index already exists for the table, no clustering option specified for the constraint --> Non-clustered index is created.
5) A clustered index already exists for the table, CLUSTERED option specified for the constraint --> No index created; error 1902 raised; constraint not created.
6) A clustered index exists for the table, NONCLUSTERED option specified for the constraint --> Non-clustered index is created.


Still, a great question. I really hope that Paul and Kimberly continue to submit questions, even if they don't have a seminar to promote.

(edit: small but important typo in option 6 - thanks, Shaiju C.K., for alerting me to it)



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #879994
Posted Wednesday, March 10, 2010 1:27 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 7:12 AM
Points: 366, Visits: 455
trickey question...already existing Clustered Index just went off from my mind and I answered TRUE...I was wondering why Paul would have asked such a simple question and w/o giving it a 2nd thought I happened to answered it wrong...
Post #879996
Posted Wednesday, March 10, 2010 2:21 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 2:15 AM
Points: 70, Visits: 43
Paul Randal,

When you ask\ write any question mention your assumption.
Post #880018
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse