Can a table have two primary keys?

Is it possible to have two primary keys on the same table?

    Requires Free Membership to View

No. A table can have only one primary key.

However, that primary key can consist of two columns. For example, consider this table of DatabaseProducts:

Company    Product     Release
IBM        DB2         5
Microsoft  SQL Server  7.0
Microsoft  Access      97
Oracle     Oracle      8
Sybase     ASE         12.5

As it stands, you could define this table with:

create table DatabaseProducts
 ( Company  varchar(20)
 , Product  varchar(20)
 , Release  varchar(10)
 , constraint DatabaseProductPK
        primary key (Company, Product)
 )

If you wanted the table to contain both Microsoft SQL Server 7.0 and Microsoft SQL Server 2000, then the primary key would have to include the Release column as well.

For More Information


This was first published in May 2003

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.