There are no items in your cart
Add More
Add More
Item Details | Price |
---|
• Yash Gupta • Feb 27, 2025 • 12 mins read
In relational database management systems (RDBMS), keys play a crucial role in ensuring data integrity and establishing relationships between tables. SQL keys help uniquely identify records, enforce constraints, and maintain database consistency. This blog explores the different types of SQL keys and their significance in database management.
A Primary Key uniquely identifies each record in a table and ensures that no two rows have the same key value.
Characteristics:
Example:CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,Name VARCHAR(100),Email VARCHAR(100));
CustomerID | Name | |
1 | Anisha | anisha@example.com |
2 | Pankaj | pankaj@example.com |
A Foreign Key establishes a relationship between two tables by referring to the Primary Key of another table.
Characteristics:
Example:CREATE TABLE Orders (OrderID INT PRIMARY KEY,CustomerID INT,OrderDate DATE,FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));
OrderID | CustomerID | OrderDate |
1001 | 1 | 2025-02-20 |
1002 | 2 | 2025-02-21 |
A Candidate Key is a column or combination of columns that can uniquely identify a row. One candidate key is selected as the Primary Key.
Characteristics:
Example:CREATE TABLE Employees (EmployeeID INT UNIQUE,Email VARCHAR(100) UNIQUE,PhoneNumber VARCHAR(15) UNIQUE);
EmployeeID | PhoneNumber | |
1 | anisha@company.com | 123-456-9870 |
2 | pankaj@company.com | 876-983-2546 |
A Super Key is a set of one or more columns that uniquely identifies a row. It includes Candidate Keys and may have extra attributes.
Characteristics:
Example:
A Composite Key consists of two or more columns that together uniquely identify a record.
Characteristics:
Example:CREATE TABLE StudentCourses (StudentID INT,CourseID INT,EnrollmentDate DATE,PRIMARY KEY (StudentID, CourseID));
StudentID | CouseID | EnrollmentDate |
1 | 101 | 2025-02-23 |
2 | 102 | 2025-02-26 |
3 | 101 | 2025-02-27 |
A Unique Key ensures that the column values remain distinct but allows NULL values.
Characteristics:
Example:CREATE TABLE Products (ProductID INT PRIMARY KEY,SerialNumber VARCHAR(50) UNIQUE);
ProductID | SerialNumber |
1 | DS-12345 |
2 | DS-12346 |
An Alternate Key is a Candidate Key that is not chosen as the Primary Key.
Characteristics:
Example:If EmployeeID is chosen as the Primary Key, then Email becomes an Alternate Key in the Employees table.
A Surrogate Key is an artificially generated key, usually a numeric value, that substitutes for a natural key.
Characteristics:
Example:CREATE TABLE Orders (OrderID INT AUTO_INCREMENT PRIMARY KEY,OrderNumber VARCHAR(20) UNIQUE);
OrderID | OrderNumber |
1 | ORD-1001 |
2 | ORD-1002 |
SQL keys are fundamental in database design. They ensure data integrity, uniqueness, and efficient data retrieval. The appropriate key type depends on the specific use case.
Key Takeaways:
Understanding and implementing these keys correctly ensures a well-structured, optimized, and error-free database. 🚀
Yash Gupta
I am passionate about tech and coding. I share expert insights on Test Automation (Selenium, Cypress, Playwright), API Automation, JavaScript, Python, Svelte, Vue.js, ReactJS, Angular, Flutter, and more. Stay updated with the latest trends! 🚀