>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 "
Main Topics
Browse All TopicsHi,
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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.
>> 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
" 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).
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')
>> 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.
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
>> 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...
>> 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
Business Accounts
Answer for Membership
by: aneeshattingalPosted on 2009-07-05 at 15:45:32ID: 24781574
>Can a Primary Key be multiple columns ?
Yes,