jobobo
jobobo

Reputation: 389

surrogate keys vs compound keys

Fairly new to database schema (plan to use SQLite). Having said that, I'm thinking about using surrogate keys because the database currently contains a compound key (3 columns), which shows up in most of my tables. I have several tables that contains 3 columns for the unique key and one column containing some information; I also have one table contains 3 column for unique key and the same 3 columns as foreign keys (many parents). Combining all these tables into a single table doesn't seem to make sense because there would be many empty fields.

Any pit falls if I choose one or the other? Which one is generally considered more convenient for programming?

Thank you in advance.

Upvotes: 0

Views: 996

Answers (1)

Larry Lustig
Larry Lustig

Reputation: 50970

Each technique has advantages and disadvantages.

In general, it's easier to write SQL statements and JOINs if you only need to refer to the single surrogate key column. It also reduces the size of your database fairly substantially.

On the other hand, with surrogate keys you often find yourself having to add at least one extra table to your JOINs in order to retrieve information that is part of the surrogate key.

Two additional advantages of a surrogate key:

  1. Many frameworks require the use of an integer primary key field.

  2. If you are binding your records to any kind of user interface control (inputs on a web page, for example), it's considerably easier to attach a single value to the control for identification purposes than it is to encode and decode multiple columns.

Upvotes: 5

Related Questions