Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can a Primary Key be multiple columns in SQL Server 2005 / 2008 ?

Posted on 2009-07-05
14
Medium Priority
?
1,325 Views
Last Modified: 2012-05-07
Hi,

Can a Primary Key be multiple columns ? Because Primary Key will have Clustered Index by default, it will be physically stored in an order. But if a Primary Key be multiple columns, then how it will be stored and ordered - using leaf nodes / pointers to leaf nodes ?

Thanks
0
Comment
Question by:milani_lucie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
14 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24781574
>Can a Primary Key be multiple columns ?
Yes,  
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24781581
>how it will be stored and ordered - using leaf nodes / pointers to leaf nodes ?
it is not an one word answer, i would recommend you read " Inside SQL "
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24781610
Surely, it can be multiple columns key. The key value is composed from column values and behaves like single column key. The index and table data are at two different places and index leafs contain pointers to original data.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 43

Expert Comment

by:pcelba
ID: 24781631
The clustered vs. nonclustered difference is in necessity of "duplicate sorting" when you create clustered index - index keys are organized in the tree as obvious (the first sorting) and original data are physically sorted by index key values (the second sorting). Thus the time necessary for updates of columns included in clustered index is obviously much longer than time necessary for other columns.
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 450 total points
ID: 24781847
>> Can a Primary Key be multiple columns ?

Yes..

>> Because Primary Key will have Clustered Index by default, it will be physically stored in an order.

Even if it is single or multiple columns, Clustered Index will physically sort records present in the table.

>> But if a Primary Key be multiple columns, then how it will be stored and ordered - using leaf nodes / pointers to leaf nodes ?

As mentioned earlier, It would physically sort based upon the order you specify. For eg:

If you create a Primary key / clustered index on ( a, b , c) then it would be first sorted by a then by b and then by c

Hope this helps
0
 
LVL 43

Assisted Solution

by:pcelba
pcelba earned 300 total points
ID: 24781916
" first sorted by a then by b and then by c" is probably not accurate becaus we could minimize it to sorting by c only :-).

The correct sentence is:
It would be first sorted by a, if a values are equal the b value is added, and if a+b are equal then c value is added. Or, simply said, it is sorted by a+b+c (the ASC/DESC possibility is omitted here).
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24782175
pcelba,
    I typed that meaning this one only. Thanks for explaining to the asker.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 750 total points
ID: 24782234
Answer is simply YES to the first part, there are a few restrictions as to what kind of data (such as not NULL, LOB or BLOB), and ideally is small non-changing data elements.

When creating the Primary Key, if a clustered index does not already exist on the table or a nonclustered index is not explicitly specified, a unique, clustered index is created to enforce the PRIMARY KEY constraint.

You may also want to consider a UNIQUE Constraint and the use of a surrogate key as the Primary Key.

If you do create a clustered index, then by definition, the leaf level of a clustered index and the data pages are the same.

The "KEY VALUE" will result in a sequence of the composite key. However, if spanning different (partitions) filespaces then it is not gauranteed that it will return everything in exact sequence. By default, a clustered index has a single partition. When a clustered index has multiple partitions, each partition has a B-tree structure that contains the data for that specific partition. For example, if a clustered index has four partitions, there are four B-tree structures; one in each partition. You must really use the ORDER BY to ensure correct sequence.

However, assuming a single partition,

table My_abc (a int, b int, c int, d varchar(100), CONSTRAINT PK_My_abc PRIMARY KEY CLUSTERED (a,b,c))

with values (1,1,1,'first c'), (1,2,1,'second b'), (2,1,1,'second a'), (1,1,2,'Second c'), (1,2,2,'Second b and c')

will return thr rows in the sequence of the key

(1,1,1,'first c')
(1,1,2,'Second c')
(1,2,1,'second b')
(1,2,2,'Second b and c')
(2,1,1,'second a')
0
 
LVL 22

Expert Comment

by:dportas
ID: 33168882
>> will return thr rows in the sequence of the key

Without ORDER BY the order of rows returned by a query is undefined. It's quite possible that they will not be returned in the order of the clustered index. In some cases it's very likely that they won't match the clustered index order. The only way to be sure is to specify ORDER BY.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 33171622
Yep, agree, that is why I also said : " You must really use the ORDER BY to ensure correct sequence."

However, in a single partition, then the default return sequence will be data page sequence and with a clustered index, with data held on leaf nodes, well...

However (and again), one should really use the ORDER BY if there is a specific sequence required.

Using the My_abc as an example, and inserting rows out of sequence, then the select will return the rows as expected (again, it must not be relied upon)...

create table My_abc (a int, b int, c int, d varchar(100), CONSTRAINT PK_My_abc PRIMARY KEY CLUSTERED (a,b,c))

insert My_abc values (1,1,1,'first c')
insert My_abc values (1,2,1,'second b')
insert My_abc values (2,1,1,'second a')
insert My_abc values (1,1,2,'Second c')
insert My_abc values (1,2,2,'Second b and c')

select * from my_abc
0
 
LVL 22

Expert Comment

by:dportas
ID: 33172564
>>  in a single partition, then the default return sequence will be data page sequence and with a clustered index, with data held on leaf nodes

Unless the optimiser chooses to use a covering nonclustered index scan instead. Or it performs an unordered (allocation order) scan of the clustered index. Or an "advanced" scan. Or...
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 33172599
*laughing* OK... Enough said... Should have known better  (but the optimizer would need some additional predicates to choose alternatives :) ) :)
0
 
LVL 22

Expert Comment

by:dportas
ID: 33172683
>> the optimizer would need some additional predicates to choose alternatives

Not necessarily...

CREATE TABLE tbl (x INT PRIMARY KEY CLUSTERED, z CHAR(1) UNIQUE);

INSERT INTO tbl VALUES (1,'Z');
INSERT INTO tbl VALUES (2,'Y');
INSERT INTO tbl VALUES (3,'X');
INSERT INTO tbl VALUES (4,'W');

SELECT * FROM tbl;

Result:

x           z
----------- ----
4           W
3           X
2           Y
1           Z
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 33172736
I knew I shouldnt have said anything else :)

But (gosh, cannot believe I am doing this) adding the unique constraint is adding another index and will do an index scan...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question