Master SQL Keys

Learn how SQL Keys work with Fullstackgada's fun approach!

๐Ÿ”‘ SQL Keys Explorer

Key Concepts: SQL Keys help uniquely identify records in a database. Click any key type to learn more!
What are SQL Keys?
Keys uniquely identify records in a database. Click a key type to explore!

๐ŸŽญ Fun Analogies

Learn with Fun: Understand database keys with real-world analogies!

๐Ÿ  Community Analogy

๐Ÿค”
Which key do you want to learn about? Select one above!

๐ŸŽฎ Interactive Example

Select a key type to see an interactive example...

๐Ÿ“Š Visual SQL Keys Demonstration

๐Ÿ‘จโ€๐ŸŽ“ Students Table Example

๐Ÿ—๏ธ StudentID (PK) Name Email ๐Ÿ”„ AadharNo (AK) Phone
101 Jethalal jethalal@gokuldham.com 1234-5678-9012 9876543210
102 Bhide bhide@discipline.com 2345-6789-0123 8765432109
103 Popatlal popat@reporter.com 3456-7890-1234 7654321098
Key Points:
๐Ÿ—๏ธ Primary Key: StudentID - Uniquely identifies each student
๐Ÿ”„ Alternate Key: AadharNo - Unique but not chosen as Primary
๐ŸŽฏ Candidate Keys: StudentID, Email, AadharNo - All can uniquely identify records

๐Ÿ“š Enrollment Table Example

๐Ÿงฉ StudentID (CK) ๐Ÿงฉ SubjectCode (CK) Grade Semester
101 MATH101 A 1
101 PHY101 B+ 1
102 MATH101 A+ 1
Key Points:
๐Ÿงฉ Composite Key: (StudentID + SubjectCode) - Together uniquely identify records
๐Ÿ”— Foreign Key: StudentID - Links to Students tableโ€™s Primary Key
๐Ÿฆธ Super Key: (StudentID + SubjectCode + Grade) - Includes extra columns but still unique

๐Ÿ”— Table Relationships Visualization

/* Students Table */
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Email VARCHAR(100) UNIQUE,
AadharNo VARCHAR(12) UNIQUE
);
/* Enrollment Table */
CREATE TABLE Enrollment (
StudentID INT,
SubjectCode VARCHAR(10),
Grade VARCHAR(5),
PRIMARY KEY (StudentID, SubjectCode),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);

๐Ÿ” SQL Keys Comparison

Key Type Definition Properties Fun Analogy
๐Ÿ—๏ธ Primary Key Uniquely identifies each record Not NULL, Unique, Only one per table Like the head of a communityโ€”only one leader!
๐Ÿ”— Foreign Key Links a column to a Primary Key in another table Can be NULL, Maintains referential integrity Like a connection to a trusted friend!
๐ŸŽฏ Candidate Key Any column(s) that can uniquely identify records Multiple per table, Unique, Minimal Like candidates eligible to lead the community!
๐Ÿงฉ Composite Key Combination of two or more columns Unique together, May not be unique individually Like a team effortโ€”both members needed!
๐Ÿฆธ Super Key Any combination that uniquely identifies a row May include extra columns, Not minimal Like identifying someone with extra details!
๐Ÿ”„ Alternate Key Candidate Key not chosen as Primary Unique but not selected as Primary Like a backup leader who didnโ€™t get chosen!