Can a table have two primary keys?

Can a table have two primary keys?

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

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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.