Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi column primary keys?

For example I have 2 tables, Users and UserRelations, and it is a one to many relationship.

For the UserRelations table, I can have an identity column and make it the primary key:

[RelationID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NOT NULL,
[TargetID] [int] NOT NULL,

Or I can design the table like:

[UserID] [int] NOT NULL,
[TargetID] [int] NOT NULL,

and make UserID + TargetID the primary key.

My question is what are the implications of going with each design, which is better for performance?

like image 404
Ray Avatar asked Dec 08 '08 21:12

Ray


People also ask

How do you add a primary key in multiple columns?

We can set PRIMARY KEY constraint on multiple columns of an existing table by using ADD keyword along with ALTER TABLE statement.

How can create primary key in multiple columns in SQL Server?

In Table Designer, click the row selector for the database column you want to define as the primary key. If you want to select multiple columns, hold down the CTRL key while you click the row selectors for the other columns. Right-click the row selector for the column and select Set Primary Key.

Can we have 2 primary keys in a table?

Yes, we can have more than one column as primary key to solve some business requirements. Primary Keys ensures that the column(s) will not have duplicate values , Null in the table.

What are multiple primary keys called?

Sometimes it requires more than one attribute to uniquely identify an entity. A primary key that made up of more than one attribute is known as a composite key.


2 Answers

If you use the former design, with the superfluous identity column, there's no constraint against inserting two rows with identical UserID and TargetID. You'd have to create a UNIQUE constraint over the other two columns, which creates a compound index anyway.

On the other hand, some frameworks (e.g. Rails) insist that every table has a surrogate key named id so the "correct" design may not work. It depends on what code you're writing to use this table design.

like image 86
Bill Karwin Avatar answered Sep 19 '22 18:09

Bill Karwin


This is almost a religious issue. For each person who says use a non-intelligent surrogate key, somebody else points outs that surrogate keys can be considered superfluous, etc, etc. So do whatever feels most comfortable for you and your team.

If you do decide to use a surrogate key, you should also put a unique constraint on the natural (in this case multi-column) key, to keep the integrity of your data.

I usually go for an additional surrogate key, as there are a number of desirable (not necessarily required) primary key characteristics that natural keys sometimes lack:

  • Unique values: The primary key must uniquely identify each row in a table.
  • Non-intelligent: The primary key should preferably have no embedded semantic meaning. In other words, it should not describe characteristics of the entity. A customer ID of 12345 is typically preferred over RoadWarrior.
  • No change over time: The value of a primary key should not normally change. Changing a primary key value means you’re changing the identity of an entity, which doesn’t normally make sense. I prefer non-intelligent keys because they are less likely to change.
  • Single-attribute: A primary key should have the minimum number of attributes possible. Single-attribute primary keys are desirable, because they’re easier for applications to work with, and they simplify the implementation of foreign keys.
  • Numeric: It’s often easier to manage unique values when they are numeric. Most database systems have internal routines that enable auto-incrementing primary key attributes.

From a performance point of view, I suspect there is little difference in most cases. But as with any performance issue, you should measure where you have concerns.

like image 32
HTTP 410 Avatar answered Sep 19 '22 18:09

HTTP 410