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 Wednesday, March 10, 2010 3:26 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, June 5, 2014 6:18 AM
Points: 1,140, Visits: 326
Good question :).

Forces thinking about why you might not want to, and that that executing DDL statements without understanding the underlying context can lead to unexpected results.

I don't think there was any need for the question to lay out any further assumptions, as it is a DBAs/SQL Developers jobs to question their own assumptions about the environment they are developing against/managing (?).
Post #880052
Posted Wednesday, March 10, 2010 5:04 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, September 11, 2014 3:05 AM
Points: 2,039, Visits: 1,665
Folks - this is another questions written by Kimberly - not sure why it's posted under my account instead of hers.

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 #880089
Posted Wednesday, March 10, 2010 6:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 28, 2014 4:30 PM
Points: 27, Visits: 59
Hey there everyone - I know people are feeling like was trying to trick them in the question but I guess the "always" part is what I thought would have been the dead give away. It's really rather simple actually - a Primary Key is NOT always enforced by a clustered index. I think if you read it this way it's simple. As stated by others, the obvious case is when you actually state NONCLUSTERED in the index definition but the "tricky" one is when a clustered index always exists. This is the one that I think a lot of people forget about... But, there are a bunch of combinations that are actually possible and Hugo's answer is very complete. Be sure to review that.

The end result is that there are a lot of things that people think "always" happen in SQL Server and when we become complacent - we get caught (myself included - that's not meant negatively... we all have to be careful!!). I've seen many cases where people just expect that their PKs are always enforced by a clustered and most are...but, then they have problems and there's that one (and then I hear, oh, yeah, I forgot I.........).

Even more so I see a lot of confusion around these three things: the PK, the clustered index and an identity column. There are a lot of assumptions that those are the same thing - even some that think they are inseparable. It's really important to realize that they do not have the be the same thing. They are 3 completely different things (yes, the often come together) but they really are controllable at any level. And, sometimes they should be. It's really more about making the *right* choices - for YOUR workloads. Understanding the internals to make informed decisions about your table design so that you get the best performance in Microsoft SQL Server. What I do in MSSQL is not going to be the same thing I'd do in other environments. This is what's tricky. This is why we're all here.

It's *very* hard in SQL Server to ever say "always" or "never"... except for turning on autoshrink. *NEVER* do that!!!

Cheers,
Kimberly
Post #880117
Posted Wednesday, March 10, 2010 6:20 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:03 AM
Points: 20,703, Visits: 32,344
nargade 3404 (3/10/2010)
Paul Randal,

When you ask\ write any question mention your assumption.


No assumptions needed. If you look at the syntax for creating a PRIMARY KEY, the default may be to create a clustered index, but that is not always how it is created. You can create it as a nonclustered index as well.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #880129
Posted Wednesday, March 10, 2010 7:17 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 7:17 AM
Points: 31,080, Visits: 15,526
sameerchachad 69959 (3/9/2010)

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.



I think you're emphasizing this a little wrong. The default setting is clustered, but you change that at creation time. So it's not that it always creates the index clustered, but that if you don't change the setting, that is what is done.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #880202
Posted Wednesday, March 10, 2010 7:43 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 7:17 AM
Points: 31,080, Visits: 15,526
FYI, all of these came with Paul's name on them, so he gets to be the target :)






Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #880247
Posted Wednesday, March 10, 2010 8:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:33 AM
Points: 6,020, Visits: 8,289
Steve Jones - Editor (3/10/2010)
sameerchachad 69959 (3/9/2010)

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.



I think you're emphasizing this a little wrong. The default setting is clustered, but you change that at creation time. So it's not that it always creates the index clustered, but that if you don't change the setting, that is what is done.
Correction - the default is not "clustered", but "clustered if no clustered index exists yet, otherwise nonclustered".



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #880273
Posted Wednesday, March 10, 2010 8:13 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 1:16 PM
Points: 29, Visits: 32
Bottom line: always specify whether you want to create a clustered or nonclustered primary key on the add constraint PK_.. statement.
Post #880274
Posted Wednesday, March 10, 2010 8:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:17 PM
Points: 5,332, Visits: 25,261
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.


Heck either way. I've had the good fortune to listen to Kimberly and Paul for an about an hour and a half at a local user group meeting, and was more than willing to sit for a longer time, but alas, they had already spent a full day giving their seminar and were entitled to some rest / relaxation.

The amount of information these 2 individuals can impart, getting you to think ... is amazing. So let them promote their seminars, and if you have the opportunity to attend do so

Paul, Kimberly - thanks for a thought provoking question.


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #880292
Posted Wednesday, March 10, 2010 9:14 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:46 PM
Points: 2,391, Visits: 17,940
I liked the question! I've found that in our environment the primary key is very often a bad choice for the clustered index, so I have become more aware of this "default" of late and been teaching those around me how to evalute good CI candidates and not just leave it at the default.

Thanks Kimberly!
Chad
Post #880336
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse