Unix Technical Forum

Key and Index on Same Column?

This is a discussion on Key and Index on Same Column? within the SQL Server forums, part of the Microsoft SQL Server category; --> Is there any advantage to doing this: ALTER TABLE testtable ADD CONSTRAINT PK_sysUser PRIMARY KEY NONCLUSTERED (UserID) WITH FILLFACTOR ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 02-29-2008, 09:51 AM
christopher.secord@gmail.com
 
Posts: n/a
Default Key and Index on Same Column?

Is there any advantage to doing this:

ALTER TABLE testtable ADD
CONSTRAINT PK_sysUser
PRIMARY KEY NONCLUSTERED (UserID)
WITH FILLFACTOR = 100,

CONSTRAINT IX_sysUser
UNIQUE NONCLUSTERED (UserID)
WITH FILLFACTOR = 100
GO

over just having the primary key? Does having both an index and a
primary key add anything?

thanks
chris

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 09:51 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Key and Index on Same Column?

On 26 Aug 2005 12:24:19 -0700, christopher.secord@gmail.com wrote:

>Is there any advantage to doing this:
>
>ALTER TABLE testtable ADD
> CONSTRAINT PK_sysUser
> PRIMARY KEY NONCLUSTERED (UserID)
> WITH FILLFACTOR = 100,
>
> CONSTRAINT IX_sysUser
> UNIQUE NONCLUSTERED (UserID)
> WITH FILLFACTOR = 100
>GO
>
>over just having the primary key? Does having both an index and a
>primary key add anything?


Hi Chris,

No.

When you declare a PRIMARY KEY constraint, SQL Server will immediately
create an index to support it. Default is clustered, but in this case,
you override the default and get a non-clustered index.

When you declare a UNIQUE constraint, SQL Server will immediately create
an index to support it. Default is nonclustered; in this case you're
also asking for non-clustered, so non-clustered is what you'll get.

The end result: two exactly identical nonclustered indexes, double
overhead on data modification, and one of those indexes will never be
used.

However, if you define the primary key with the default clustered
option, there are some circumstances where an extra nonclustered index
on the same column might help. If the table is wide, but some queries
use only the primary key value, a scan of the nonclustered index will be
faster than a scan of the clustered index. I'd not use the UNIQUE
constraint to declare such an index, thoug, but use a CREATE INDEX
statement to stress that this is just a supporting index instead of
another constraint.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 09:51 AM
christopher.secord@gmail.com
 
Posts: n/a
Default Re: Key and Index on Same Column?

>If the table is wide, but some queries
>use only the primary key value, a scan of the nonclustered index will be
>faster than a scan of the clustered index.


Thanks. That's good info.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 09:51 AM
Gert-Jan Strik
 
Posts: n/a
Default Re: Key and Index on Same Column?

Chris,

Hugo already explained the technical details. Here is an other aspect of
the issue.

Both a Primary Key and a Unique constraint are logical construct that
give information about your data. They are true regardless of the
database you use.

You have designed you schema based on some world view. You have
determined that a specific column (or set of columns) is unique, and
therefore is a candidate key for the table. What you typically do, is
choose one of the candidate keys to be the Primary Key of the table, and
you specify all the other candidate keys as Unique. It would be very
confusing to specify the same set of colums as both Unique and Primary
Key, since (by definition), the Primary Key means that the key is unique
(and non-null).

If you are looking at these topics from a performance perspective, then
I would suggest you do not use the modelling concepts (Constraints), but
limit yourself to the implementation concepts (i.e. Indexes). This means
that if you remove all indexes, but keep all constraints, then the
database would still work properly (although it might be slow). In your
example, if you want to experiment with extra indexes for performance
reasons, you can simply add a unique index.

Gert-Jan


"christopher.secord@gmail.com" wrote:
>
> Is there any advantage to doing this:
>
> ALTER TABLE testtable ADD
> CONSTRAINT PK_sysUser
> PRIMARY KEY NONCLUSTERED (UserID)
> WITH FILLFACTOR = 100,
>
> CONSTRAINT IX_sysUser
> UNIQUE NONCLUSTERED (UserID)
> WITH FILLFACTOR = 100
> GO
>
> over just having the primary key? Does having both an index and a
> primary key add anything?
>
> thanks
> chris

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 09:52 AM
christopher.secord@gmail.com
 
Posts: n/a
Default Re: Key and Index on Same Column?


Gert-Jan Strik wrote:
> What you typically do, is
> choose one of the candidate keys to be the Primary Key of the table, and
> you specify all the other candidate keys as Unique. It would be very
> confusing to specify the same set of colums as both Unique and Primary
> Key, since (by definition), the Primary Key means that the key is unique
> (and non-null).


I think that what threw me off was that the database even allowed me to
do both. I was trying hard to think of some reason why I would want to
do that and thought I'd go ahead and ask here. You guys have cleared
it up for me.


> This means
> that if you remove all indexes, but keep all constraints, then the
> database would still work properly (although it might be slow).


Is there any situation where removing an index would cause the database
to not function??

chris

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 09:52 AM
Gert-Jan Strik
 
Posts: n/a
Default Re: Key and Index on Same Column?

"christopher.secord@gmail.com" wrote:
[snip]
> > This means
> > that if you remove all indexes, but keep all constraints, then the
> > database would still work properly (although it might be slow).

>
> Is there any situation where removing an index would cause the database
> to not function??


No, from a theoretical point of view, you never need to manually create
an index.

What I meant to say is if you remove the constraints (or never create
them in the first place), then you are very likely to get corruption in
your data, such as orphaned rows (missing Foreign Key), duplicate values
(missing Primary Key / Unique constraint), etc. If you have the proper
constraints in place, you can add or remove indexes without affecting
the correctness of the database.

Or course, from a practical point of view, you do need indexes. This is
because by default only Primary Keys and Unique constraints are
automatically indexed. Foreign Keys are not automatically indexed. Your
system would be unnecessarily slow without indexes (generally more I/O
needed), and SQL-Server's locking strategy would also be very limited,
which means lower concurrency (more users 'waiting' for their
transaction).

Gert-Jan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 09:52 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Key and Index on Same Column?

christopher.secord@gmail.com (christopher.secord@gmail.com) writes:
> I think that what threw me off was that the database even allowed me to
> do both. I was trying hard to think of some reason why I would want to
> do that and thought I'd go ahead and ask here.


I guess the reason that SQL Server did not say anything is that no
one thought the condition was worth the extra piece of code need to
add such a check.

Just because something is possible to do with warning or error message,
does not mean that it is a meaningful or a harmless thing to do.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

LinkBacks (?)
LinkBack to this Thread: http://www.unixadmintalk.com/sql-server/264763-key-index-same-column.html

Posted By For Type Date
vxdisk list shows disk online invalid - Unix Forum This thread Refback 07-01-2008 04:16 PM


All times are GMT. The time now is 06:59 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com