Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1328
  • Last Modified:

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
0
milani_lucie
Asked:
milani_lucie
  • 4
  • 3
  • 3
  • +2
3 Solutions
 
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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 & ArchitectCommented:
>> 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 & ArchitectCommented:
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
 
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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now