A primary key is a candidate key that is selected by the database designer to identify tuples uniquely in a relation. A relation may contain many candidate keys. It is a column that has been configured as the unique identifier field for the table. This column cannot have NULL values. When the designer selects one of them to identify a tuple in the relation, it becomes a primary key. It means that if there is only one candidate key, it will be automatically selected as the primary key. It typically appears as columns in relational database tables.
A primary key’s main features are:
- It must contain a unique value for each row of data.
- It cannot contain null values.
Suppose a relation STUDENT contains different attributes such as RegNo, Name, and Class. The attribute Reg No uniquely identifies each student in a table. It can be used as the primary key for this table. Databases use keys to compare, sort, and store records, and to create relationships between records. The attribute Name cannot uniquely identify each row because two students can have the same names. It cannot be used as a primary key.
Tips for Choosing Good Primary Keys –
- Keep it short, a short primary key means the database management system can process it more quickly than a long primary key.
- Use a number for the primary key whenever possible.
- Don’t use any special characters or a mix of upper and lower capitalization.