SQl Candidate, Super, Primary, Foreign Key Types with Example -DeveloperIndian
Difference between Primary Key, Candidate Key, Foreign Key, and Super Key
In database management, keys play a crucial role in ensuring data integrity and establishing relationships between tables. SQL uses different types of keys, each with a specific purpose. Below, we discuss Primary Key, Candidate Key, Foreign Key, and Super Key with examples.
A Primary Key uniquely identifies each record in a table. It ensures that no two rows have the same value for this key, and it cannot contain NULL values. There can be only one primary key per table.
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
name VARCHAR(100),
address VARCHAR(255),
phone VARCHAR(20)
);
Here, employee_id
is the Primary Key as it uniquely identifies each employee.
A Candidate Key is a minimal set of attributes that can uniquely identify a record. A table can have multiple candidate keys, but one is selected as the Primary Key.
CREATE TABLE emp (
employee_id INT,
name VARCHAR(100),
address VARCHAR(255),
phone VARCHAR(20),
UNIQUE (employee_id)
);
Here, employee_id
is a Candidate Key as it uniquely identifies records, but it is not yet the primary 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 maintain referential integrity.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
);
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
name VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);
Here, dept_id
in the employee table is a Foreign Key referencing dept_id
in the department table.
A Super Key is a set of one or more attributes that uniquely identify a row in a table. A Candidate Key is a minimal Super Key, meaning it contains no unnecessary attributes.
CREATE TABLE student (
student_id INT,
name VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(15),
UNIQUE (student_id, email)
);
Here, (student_id, email)
is a Super Key because it uniquely identifies a record.
Key Type | Definition | NULL Allowed | Uniqueness | Number per Table |
---|---|---|---|---|
Primary Key | Uniquely identifies each row | No | Yes | Only 1 |
Candidate Key | Minimal set of attributes that can identify a row | No | Yes | Multiple |
Foreign Key | Refers to a Primary Key in another table | Yes | No | Multiple |
Super Key | A set of attributes that uniquely identifies a row | Yes | Yes | Multiple |
By understanding these key types, you can effectively design and maintain relational databases in SQL.
What is the difference between a Primary Key and a Candidate Key?
A Primary Key is a Candidate Key chosen as the table's unique identifier. A table can have multiple Candidate Keys, but only one Primary Key.
Can a Foreign Key be NULL?
Yes, a Foreign Key can contain NULL values if it is not mandatory for every record to have a corresponding value in the referenced table.
Can a table have multiple Candidate Keys?
Yes, a table can have multiple Candidate Keys, but only one can be set as the Primary Key.
What is the difference between a Primary Key and a Super Key?
A Super Key is a set of attributes that uniquely identify a row, while a Primary Key is a minimal Super Key without unnecessary attributes.
Why do we need Foreign Keys in SQL?
Foreign Keys ensure referential integrity by linking records across different tables, preventing orphan records.
By mastering these SQL keys, you can build well-structured, efficient, and secure databases. 🚀
Primary Key- "Each record in a table is given a special identification by the PRIMARY KEY constraint. Primary keys cannot have NULL values and must have UNIQUE values. There can only be one main key in a table."
Candidate Key - A candidate key is the bare minimum set of attibute that can successfully identify a tuple. As An Example
"A subset of the super key known as a "candidate key" is capable of uniquely identifying the other table properties."
Every table must have at least a single candidate key.
Foreign Key -A field (or group of fields) in one table that refers to the PRIMARY KEY in another table is known as a FOREIGN KEY. The foreign key table is known as referenced or parent table.
Super Key -A tuple's set of properties that can be used to identify it exclusively is referred to as a super key. A super key is a collection of one or more keys that distinguishes the rows in a database. It allows for NULL values.