Types of keys in Database
Types of keys in Database
In a database, a key is a field or combination of fields that uniquely identifies each record in a table. There are several types of keys that can be used in a database, including:
- Primary key: A primary key is a field or combination of fields that uniquely identifies each record in a table. It cannot contain null values and must be unique across all records in the table. A primary key is often used as a foreign key in other tables, to establish relationships between tables.
- Candidate key: A candidate key is a field or combination of fields that could potentially be used as a primary key. A table can have multiple candidate keys, but only one of them can be designated as the primary key.
- Foreign key: A foreign key is a field or combination of fields in one table that refers to the primary key in another table. It is used to establish relationships between tables and to ensure data integrity by preventing the creation of orphan records (records with no corresponding foreign key value in the referenced table).
- Composite key: A composite key is a primary key that is made up of two or more fields. It is used when no single field can uniquely identify a record, and a combination of fields is needed.
- Surrogate key: A surrogate key is a unique identifier that is used as a primary key in a table, but has no inherent meaning. It is often used as an alternative to a natural primary key (such as a customer's social security number) to simplify relationships between tables and to protect sensitive data.