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
»
SQL Server 2008
»
T-SQL (SS2K8)
»
unique clustered index on 2 columns
unique clustered index on 2 columns
Rate Topic
Display Mode
Topic Options
Author
Message
ahmed7.bi
ahmed7.bi
Posted Sunday, December 12, 2010 1:00 AM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, April 25, 2012 5:55 AM
Points: 19,
Visits: 69
Hi
i have read that only one unique clustered index can be created on a table and many non clustered index
then how come the below commands works fine as it is creating a unique clustered index on 2 columns of a table
create table test4 (id int,pid int,pname char(25))
go
create unique clustered index indx_test on test4 (id, pid) --
if the above case is right then how many columns can we include in a clustered index.???
Post #1033436
ahmed7.bi
ahmed7.bi
Posted Sunday, December 12, 2010 1:15 AM
Grasshopper
Group: General Forum Members
Last Login: Wednesday, April 25, 2012 5:55 AM
Points: 19,
Visits: 69
as mentioned above i have inserted below records
insert into test4 values (1,1,'asd')
go
insert into test4 values (2,1,'asd')
the command executed successfully
my query is that as i have included first two columns in the unique clustered index then how can it accepts duplicates in column 2 ..the what is the use of defining column 2 in unique clustered index ?????????
Post #1033437
ChrisM@home
ChrisM@home
Posted Sunday, December 12, 2010 2:30 AM
SSC Eights!
Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 921,
Visits: 3,813
ahmed7.bi (12/12/2010)
Hi
i have read that only one unique clustered index can be created on a table and many non clustered index
then how come the below commands works fine as it is creating a unique clustered index on 2 columns of a table
create table test4 (id int,pid int,pname char(25))
go
create unique clustered index indx_test on test4 (id, pid) --
if the above case is right then how many columns can we include in a clustered index.???
From BOL section "Clustered Index Design Guidelines" -
"Clustered indexes sort and store the data rows in the table based on their key values. There can only be one clustered index per table, because the data rows themselves can only be sorted in one order."
and -
"Generally, you should define the clustered index key with as few columns as possible."
Low-hanging fruit picker and defender of the moggies
For better assistance in answering your questions, please read
this
.
Understanding and using APPLY, (I)
and
(II)
Paul White
Hidden RBAR: Triangular Joins
/
The "Numbers" or "Tally" Table: What it is and how it replaces a loop
Jeff Moden
Post #1033444
ChrisM@home
ChrisM@home
Posted Sunday, December 12, 2010 2:33 AM
SSC Eights!
Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 921,
Visits: 3,813
ahmed7.bi (12/12/2010)
as mentioned above i have inserted below records
insert into test4 values (1,1,'asd')
go
insert into test4 values (2,1,'asd')
the command executed successfully
my query is that as i have included first two columns in the unique clustered index then how can it accepts duplicates in column 2 ..the what is the use of defining column 2 in unique clustered index ?????????
Your clustered index is unique on column1 AND column2, not column1 OR column2: [1,1] is different to [2,1].
Low-hanging fruit picker and defender of the moggies
For better assistance in answering your questions, please read
this
.
Understanding and using APPLY, (I)
and
(II)
Paul White
Hidden RBAR: Triangular Joins
/
The "Numbers" or "Tally" Table: What it is and how it replaces a loop
Jeff Moden
Post #1033445
ALZDBA
ALZDBA
Posted Sunday, December 12, 2010 4:29 AM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 2:13 PM
Points: 6,866,
Visits: 8,071
There is no need for a clustered index to be unique !
However, if it isn't unique, sqlserver will "uniquify" it for you by adding a 4-byte part to it.
The clustered index actually contains the data pages at its leaf level.
All non-clustered indexes refer to the row using the uniquified clustered index key ! (so the wider your clix columns, the wider your ncix refs)
If there is no clustering index, these indexes refer to the row using the RID.
Books online has great info on index structures and usage. !!
You need to keep in mind an index is unique over the combination of its columns, in the order the columns are specified !
If you need every column to be unique, you'll need to declare a unique index on each of the columns (or a unique constraint at column level)
guidelines:
- create a clustered index !
- keep your clix key columns small
- keep your clix unique if possible (avoind the uniquification by the engine)
- making your clix keys growing sequential may avoid run time page splits
- create a clustered index that serves you well ( range scans ). If you cannot find one to serve you well, just pick the one that goes for the previous guidelines.
Johan
Jul 13
Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere
-
How to post Performance Problems
-
How to post data/code to get the best help
-
How to prevent a sore throat after hours of presenting ppt ?
"press F1 for solution", "press
shift
+F1 for urgent solution"
Need a bit of Powershell? How about
this
Who am I ?
Sometimes this is me
but
most of the time this is me
Post #1033453
L' Eomot Inversé
L' Eomot Inversé
Posted Sunday, December 12, 2010 5:45 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 1:43 PM
Points: 7,182,
Visits: 7,280
ahmed7.bi (12/12/2010)
if the above case is right then how many columns can we include in a clustered index.???
You can have up to 16 key columns in a clustered index, but the column sizes have to add up to less than 900 bytes.
Tom
Is minic a gheibheann béal oscailte dorn dúnta.
Is minig a cheapas beul fosgailte dòrn dùinte.
http://es.linkedin.com/in/tomthomsonsoftware
Post #1033460
GilaMonster
GilaMonster
Posted Sunday, December 12, 2010 7:13 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 4:18 PM
Points: 38,062,
Visits: 30,359
ahmed7.bi (12/12/2010)
i have read that only one unique clustered index can be created on a table and many non clustered index
then how come the below commands works fine as it is creating a unique clustered index on 2 columns of a table
You can only have one clustered index per table, though it doesn't have to be unique. Your command works because it's only creating one clustered index that consists of two columns. If you tried to create two single column clustered indexes, the second create index would fail.
For info on clustered index, have a read over this:
http://www.sqlservercentral.com/articles/Indexing/68563/
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1033470
Abhijeet Chavan
Abhijeet Chavan
Posted Tuesday, December 14, 2010 3:44 AM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, May 28, 2013 2:49 AM
Points: 270,
Visits: 375
beginners SQL book will really help to understand the concept of Indexes.
Like why only one clustered index per table
When we say clustered index can also have non unique rows, so how sql manages to search a row.
Post #1034271
GilaMonster
GilaMonster
Posted Tuesday, December 14, 2010 5:17 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 4:18 PM
Points: 38,062,
Visits: 30,359
Abhijeet Chavan (12/14/2010)
beginners SQL book will really help to understand the concept of Indexes.
Or maybe the introductory series I wrote on indexes?
http://www.sqlservercentral.com/articles/Indexing/68439/
When we say clustered index can also have non unique rows, so how sql manages to search a row.
Covered here:
http://www.sqlservercentral.com/articles/Indexing/68563/
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1034302
Abhijeet Chavan
Abhijeet Chavan
Posted Tuesday, December 14, 2010 11:33 PM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, May 28, 2013 2:49 AM
Points: 270,
Visits: 375
thanks GilaMonster
those are really good articles.
Post #1034902
« Prev Topic
|
Next Topic »
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.