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

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
milani_lucieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
>Can a Primary Key be multiple columns ?
Yes,  
0
Aneesh RetnakaranDatabase AdministratorCommented:
>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
pcelbaCommented:
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

pcelbaCommented:
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
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
>> 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
pcelbaCommented:
" 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
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
pcelba,
    I typed that meaning this one only. Thanks for explaining to the asker.
0
Mark WillsTopic AdvisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dportasCommented:
>> 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
Mark WillsTopic AdvisorCommented:
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
dportasCommented:
>>  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
Mark WillsTopic AdvisorCommented:
*laughing* OK... Enough said... Should have known better  (but the optimizer would need some additional predicates to choose alternatives :) ) :)
0
dportasCommented:
>> 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
Mark WillsTopic AdvisorCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.