In SQL, a key is a column or a combination of columns that uniquely identifies a record in a table. Keys are used to enforce integrity constraints and to establish relationships between tables. There are several types of keys in SQL
Example:
know what's SQL key and his types
In SQL, a key is a column or a combination of columns that uniquely identifies a record in a table. Keys are used to enforce integrity constraints and to establish relationships between tables. There are several types of keys in SQL
Example:
A Primary Key is a column (or a set of columns) that uniquely identifies each row in a table. It cannot contain duplicate or NULL values.
CREATE TABLE Employee (
employee_id INT PRIMARY KEY, -- Uniquely identifies each employee
First_Name VARCHAR (50),
Last_Name VARCHAR (50)
);
A Foreign Key is a column (or a group of columns) in one table that references the primary key in another table. This establishes a relationship between tables and enforces referential integrity.
CREATE TABLE Departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
A Unique Key ensures that all the values in a column (or a combination of columns) are distinct. Unlike the primary key, a unique key column may allow a single NULL (depending on the DBMS).
CREATE TABLE users (
user_id INT PRIMARY KEY
UserName VARCHAR (50) UNIQUE,
EMAIL VARCHAR (50) UNIQUE
);
A Candidate Key is any column (or combination of columns) that can uniquely identify a row. A table can have multiple candidate keys. One of these is chosen as the primary key, and the others are known as alternate keys.
CREATE TABLE Users (
user_id INT, -- Candidate Key 1
username VARCHAR(50), -- Candidate Key 2
email VARCHAR(100), -- Candidate Key 3
PRIMARY KEY (user_id), -- Chosen as the Primary Key
UNIQUE (username), -- Alternate Key (remaining candidate)
UNIQUE (email) -- Alternate Key (remaining candidate)
);
Alternate Key
An Alternate Key is simply a candidate key that was not selected as the primary key. In the previous example, both username
and email
are alternate keys.
Example Explanation:
Users
table above, since user_id
is used as the primary key, username
and email
serve as alternate keys.A Composite Key is a primary key that consists of more than one column. The combination of these columns is used to uniquely identify a row.
CREATE TABLE OrderDetails (
order_id INT,
product_id INT,
quantity INT,
PRIMARY KEY (order_id, product_id) -- Composite key made of two columns
);
A Super Key is any combination of columns that uniquely identifies a row in a table. It might include extra columns that are not necessary for unique identification (i.e., it may not be minimal).
CREATE TABLE Students (
student_id INT,
name VARCHAR(50),
age INT,
PRIMARY KEY (student_id)
);
notes outlining each type of key in SQL databases:
Primary Key
Foreign Key
Unique Key
Candidate Key
Alternate Key
Composite Key
Super Key
In summary, these notes are the essential points you need to know when documenting or outlining the types of keys in SQL. They help clarify the distinct differences and the specific uses of each key type in designing a robust database.