Hello people

Saturday, April 29, 2006

Surrogate Keys

What are surrogate keys?
Artificial keys used as a primary key , instead of usual natural, multicolumn primary key, are called surrogate keys.

Advantages
1. Natural keys usually have some meaning for the business. If there is some change in the business then there might be a requirement to change the nutural key. This means change in the primary key, which would cause a cascading effect onto the all the promary pforiegn key relationships.
If we use surrogate keys then this problem can be avoided because the columns used in the natural key above, would be noraml columns and can be changed whenever required. The uniqueness can be maintained by have unuique indexes.
2. Surrogate keys occupy less space.A 4 byte unasigned integer provides more than enough values for any application and often occupies less space.
3. Surrogate keys can increase performance. As indexing a numeric key is better then indexing a multi column key.
4. Joins are much faster on integer keys.

Disadvantages
1. Surrogate keys increases the number of joins in a query. This is because the tables contain only the numeric ids as the foreign key. TO get the actual information joins have to be made between the tables.
2. Users do not understand the meaning of surrogate keys.So querying on the basis of keys become difficult.If there is an insert operation to be done then the problem becomes worse, not only understanding is required but also new keys need to be generated, which can be an extra burden on the user as well as the performance.
3. Extra indexes have to be built on the columns that were a part of the natural key, to preserve uniqueness and this may make the updates slower.

0 Comments:

Post a Comment

<< Home