
Introduction of SQL Keys
In Structured Query Language (SQL), SQL keys play a vital role in maintaining the integrity and reliability of a database. They help in uniquely identifying records and establishing relationships between SQL tables. This guide will cover all types of SQL keys, their functionalities, and how they ensure database consistency.
What are SQL Keys?
SQL keys are attributes or sets of attributes that help to uniquely identify rows in an SQL table. They prevent duplication, enforce referential integrity, and create meaningful relationships between database tables.
Types of SQL Keys in Databases
1. Primary Key in SQL
A primary key is a column (or a combination of columns) that uniquely identifies each row in a table. It ensures that each record is unique and does not contain NULL values.
Characteristics of Primary Keys:
- Must be unique for each row.
- Cannot contain NULL values.
- Each table can have only one primary key.
- Often indexed for faster search operations.
Example of a Primary Key:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(50)
);
Here, EmployeeID
serves as the primary key, ensuring each employee has a unique identifier.
2. Foreign Key in SQL
A foreign key is used to establish a relationship between two tables. It refers to the primary key of another table, enforcing referential integrity.
Characteristics of Foreign Keys:
- It links two tables together.
- The foreign key column must contain values that exist in the primary key column of another table.
- Ensures data consistency between related tables.
Example of a Foreign Key:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmployeeID INT,
OrderDate DATE,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);
Here, EmployeeID
in the Orders
table is a foreign key referencing EmployeeID
in the Employees
table.
3. Unique Key in SQL
A unique key ensures that all values in a column are unique across the table, but unlike a primary key, it allows NULL values.
Characteristics of Unique Keys:
- Ensures uniqueness of values.
- Allows one NULL value in the column.
- A table can have multiple unique keys.
Example of a Unique Key:
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Email VARCHAR(255) UNIQUE,
PhoneNumber VARCHAR(15)
);
Here, the Email
column is defined as unique, preventing duplicate emails in the database.
4. Composite Key in SQL
A composite key consists of two or more columns that together uniquely identify a row in the table.
Characteristics of Composite Keys:
- Composed of multiple attributes.
- Used when a single column is insufficient for uniqueness.
- Helps maintain database normalization.
Example of a Composite Key:
CREATE TABLE CourseRegistrations (
StudentID INT,
CourseID INT,
RegistrationDate DATE,
PRIMARY KEY (StudentID, CourseID)
);
In this example, StudentID and CourseID together form a composite key, ensuring that a student cannot register for the same course more than once.
1. Can a Table Have Multiple Primary Keys in SQL?
No, a table can only have one primary key, but it can be composed of multiple columns (composite key).
2. What is the Difference Between a Unique Key and a Primary Key in SQL?
A primary key does not allow NULL values and ensures uniqueness, while a unique key allows one NULL value but also enforces uniqueness.
3. Can a Foreign Key be a Primary Key in Another SQL Table?
Yes, a foreign key can be a primary key in its own table while referencing another table’s primary key.
4. What Happens if a Foreign Key Value is Deleted in SQL?
It depends on the referential action set in the table. Options include:
- CASCADE: Deleting a parent row also deletes related rows.
- SET NULL: Sets the foreign key column to NULL.
- RESTRICT: Prevents deletion if references exist.
5. How Do SQL Keys Improve Database Performance?
SQL keys enhance performance by:
- Reducing redundancy in SQL tables.
- Ensuring faster searches through indexing.
- Maintaining referential integrity in SQL databases.
Understanding SQL keys is fundamental for designing efficient and reliable SQL databases. Primary keys, foreign keys, unique keys, and composite keys work together to ensure data consistency, eliminate redundancy, and maintain integrity. By implementing these SQL key types correctly, we can optimize SQL database performance and prevent anomalies.