Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
Article Discussions
»
Article Discussions by Author
»
Discuss content posted by Paul Randal
»
Primary Keys
40 posts, Page 2 of 4
««
1
2
3
4
»
»»
Primary Keys
Rate Topic
Display Mode
Topic Options
Author
Message
Craig@Work
Craig@Work
Posted Wednesday, March 10, 2010 3:26 AM
Ten Centuries
Group: General Forum Members
Last Login: Tuesday, January 29, 2013 4:08 AM
Points: 1,140,
Visits: 306
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
Paul Randal
Paul Randal
Posted Wednesday, March 10, 2010 5:04 AM
SSCommitted
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:23 PM
Points: 1,905,
Visits: 1,601
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
Kimberly L. Tripp
Kimberly L. Tripp
Posted Wednesday, March 10, 2010 6:06 AM
SSC Rookie
Group: General Forum Members
Last Login: Thursday, May 02, 2013 9:26 AM
Points: 27,
Visits: 44
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
Lynn Pettis
Lynn Pettis
Posted Wednesday, March 10, 2010 6:20 AM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 3:09 PM
Points: 21,588,
Visits: 27,386
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
Steve Jones - SSC Editor
Steve Jones - SSC Editor
Posted Wednesday, March 10, 2010 7:17 AM
SSC-Dedicated
Group: Administrators
Last Login: 2 days ago @ 1:47 PM
Points: 31,406,
Visits: 13,722
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
Steve Jones - SSC Editor
Steve Jones - SSC Editor
Posted Wednesday, March 10, 2010 7:43 AM
SSC-Dedicated
Group: Administrators
Last Login: 2 days ago @ 1:47 PM
Points: 31,406,
Visits: 13,722
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
Hugo Kornelis
Hugo Kornelis
Posted Wednesday, March 10, 2010 8:12 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 11:51 AM
Points: 5,232,
Visits: 7,023
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
sistemas 95572
sistemas 95572
Posted Wednesday, March 10, 2010 8:13 AM
SSC Rookie
Group: General Forum Members
Last Login: Wednesday, March 02, 2011 11:32 AM
Points: 29,
Visits: 28
Bottom line: always specify whether you want to create a clustered or nonclustered primary key on the
add constraint PK_..
statement.
Post #880274
bitbucket-25253
bitbucket-25253
Posted Wednesday, March 10, 2010 8:40 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 12:16 PM
Points: 5,100,
Visits: 20,194
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
Chad Crawford
Chad Crawford
Posted Wednesday, March 10, 2010 9:14 AM
SSCrazy
Group: General Forum Members
Last Login: 2 days ago @ 12:52 PM
Points: 2,548,
Visits: 17,348
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 »
40 posts, Page 2 of 4
««
1
2
3
4
»
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.