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
 General SQL Server Forums
 New to SQL Server Programming
 Clustered and Keys

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2010-08-06 : 05:37:21
Hello,

When should I use clustered or not clustered for primary keys?

And in a table that performs only Many to Many relationship (please, see ResourcesConstratains) should I have another primary key instead of using the composition of the other two?

This is my T-SQL code:

create table dbo.Constraints
(
Id int identity not null,
[Type] nvarchar(100) not null,
constraint Constraints_PK primary key clustered(Id)
)

create table dbo.Resources
(
Id int identity not null,
Content varbinary(max) filestream constraint Resources_Content_DF default(0x),
[Description] nvarchar(800) null,
[Key] uniqueidentifier not null rowguidcol constraint Resources_Key_U unique,
Locked bit not null constraint Resources_Locked_DF default(0),
[Name] nvarchar(100) not null
constraint Resources_PK primary key clustered(Id)
)

create table dbo.ResourcesConstraints
(
ResourceId int not null,
ConstraintId int not null,
[Value] nvarchar(100) not null,
constraint ResourcesConstraints_PK primary key clustered(ResourceId, ConstraintId)
)

alter table dbo.ResourcesConstraints
add constraint ResourcesConstraints_Resources_FK foreign key(ResourceId) references dbo.Resources(Id) on delete cascade on update cascade,
constraint ResourcesConstraints_Constraints_FK foreign key(ConstraintId) references dbo.Constraints(Id) on delete no action on update cascade;


Thanks,
Miguel

kashyap_sql
Posting Yak Master

174 Posts

Posted - 2010-08-06 : 07:20:04
go through the link you will find more information on clusters
http://www.sql-server-performance.com/articles/clustering/main.aspx

With Regards
Kashyap M
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-08 : 02:14:49
you can use clustered for primary keys.
For the PK of ResourcesConstraints you can use composite key itself.

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

Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 08:43:14
before creating indexes, you should ask yourself what kind of sql / application operations will you be doing against these tables? What is your bussines model for these three tables? What will be the most common SQL query?

then you will decide on which column you will put clustered index (and/or primary/composite key), which columns will be supporting (composite) keys and/or indexes etc.
Go to Top of Page
   

- Advertisement -