Keys
Keys
Keys are one of the basic requirements of a relational database model. It is widely used to identify the
tuples(rows) uniquely in the table. We also use keys to set up relations amongst various columns and
tables of a relational database.
1. Candidate Key
2. Primary Key
3. Super Key
4. Alternate Key
5. Foreign Key
6. Composite Key
Candidate Key
The minimal set of attributes that can uniquely identify a tuple is known as a candidate key.
It is a minimal super key.
It must contain unique values.
It can contain NULL values.
Every table must have at least a single candidate key.
A table can have multiple candidate keys but only one primary key.
The value of the Candidate Key is unique and may be null for a tuple.
There can be more than one candidate key in a relationship.
Primary Key
It is a unique key.
It can identify only one tuple (a record) at a time.
It has no duplicate values, it has unique values.
It cannot be NULL.
Primary keys are not necessarily to be a single column; more than one column can also be a
primary key for a table.
Super Key
Adding zero or more attributes to the candidate key generates the super key.
A candidate key is a super key but vice versa is not true.
Super Key values may also be NULL.
Alternate Key
The candidate key other than the primary key is called an alternate key.
It is a secondary key.
It contains two or more fields to identify two or more records.
Foreign Key
It is a key it acts as a primary key in one table and it acts as secondary key in another table.
It combines two or more relations (tables) at a time.
They act as a cross-reference between the tables.
A Foreign key is a field that can uniquely identify each row in another table.
Composite Key
It acts as a primary key if there is no primary key in a table
Two or more attributes are used together to make a composite key.
Unique Key
Unique Keys are the keys that define the record uniquely in the table. It is different from Primary Keys,
as Unique Key can contain one NULL value but Primary Key does not contain any NULL values.
Artificial Key
Artificial Keys are the keys that are used when no attributes contain all the properties of the Primary
Key or if the Primary key is very large and complex.
Primary Key vs Candidate Key
Primary Key vs Unique Key
Primary Key vs Foreign Key
Super key vs Primary key
Identifies Individual Records: A primary key acts as a unique fingerprint for each row, allowing
efficient retrieval and modification of specific data points.
Enforces Data Integrity: By preventing duplicates, primary keys eliminate confusion and
inconsistencies, ensuring data accuracy and reliability.
Optimizes Queries and Joins: Unique primary keys enable faster search and join operations,
improving database performance and scalability.
No Null Values: Primary keys cannot contain null values, further guaranteeing their uniqueness
and completeness.
Single or Composite: A primary key can be a single column or a combination of multiple columns
for complex identification needs.
Uniqueness Constraints: Many database systems enforce uniqueness through explicit
constraints, but even without them, duplicates within a primary key are automatically rejected.