Search code examples
sqlprimary-keyprimary-key-design

Why does an SQL table have multiple primary keys?


On my job (non programming job that takes advantage of some of my programming skills), I was asked to generate a report from our outsourced, custom made program's SQL database.

When looking at the tables I see that many of them have multiple fields assigned as PK, why is that? What advantage does that have over a serial, numeric id?


Solution

  • Data integrity. A key means a uniqueness constraint that prevents duplicate data entering the database. Keys help ensure that facts in the database can be properly identified with the real-world objects or concepts that they are supposed to describe. In practice not everything can or should be identified by a single attribute in a single column.

    Note: A key with more than one attribute is not multiple keys. It's still just one key (called a composite key).