Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 CLUSTERED or UNIQUE constraint ?

Author  Topic 

tuka
Starting Member

26 Posts

Posted - 2010-04-28 : 05:18:30
Hi

Just needed a bit of guidance:

I am using code generated by MSSS management studio and what I get in an order table is (part):

CREATE TABLE [dbo].[tblOrders](
[intID] [int] IDENTITY(1,1) NOT NULL,


then at the bottom it has
CONSTRAINT [PK_tblOrders] PRIMARY KEY CLUSTERED

shouldn't CLUSTERED be UNIQUE since we are especting unique intergers ids in field intID ?

TIA

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-04-28 : 05:36:20
Primary key indexes are per definition unique.
With the exception that UNIQUE indexes allow one NULL value where PRIMARY KEY indexes allow none NULL value.


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-28 : 05:46:45
PRIMARY KEYs have additional condition that they wont allow a NULL value apart from being unique

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-28 : 06:22:58
Just to add a little to Peso and visakh16's reply. Apologies if you know all this stuff already.

The CLUSTERED keyword here indicates that the PRIMARY KEY should be implemented with a clustered index. You can only have one clustered index per table and it alters the layout of the data. Imagine a phone book. the CLUSTERED INDEX of the phone book is surname, firstname, address. (The data is physically layed out in that order. Obviously that makes it very easy to find the phone number for someone if you know their name.

You can declare a PRIMARY KEY with the NONCLUSTERED hint. This makes a NONCLUSTERED INDEX over the primary key column(s). A nonclustered index is like a lookup list pointing to the data. (like the index at the back of a catalogue that lets you find the correct page number for something you want to buy for example).

The PRIMARY KEY indicates that the column must be unique. (it uniquely identifies a row in the table). The NONCLUSTERED / CLUSTERED keyword determines which sort of index the key uses.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -