403 Keys in Relational Databases PDF
Document Details
Uploaded by DecisiveGreatWallOfChina1467
Tags
Summary
This document provides an overview of keys in relational databases. It explains the different types of keys, including primary keys, candidate keys, alternate keys, and foreign keys, and their significance in database design and data integrity.
Full Transcript
403 Keys in Relational Databases In relational databases, keys play a crucial role in ** ** ** ** uniquely identif...
403 Keys in Relational Databases In relational databases, keys play a crucial role in ** ** ** ** uniquely identifying records within a table, establishing relationships between tables, and maintaining data integrity. *** Keys ensure that each record in a database can be accurately retrieved, updated, or deleted ** without ambiguity. * This lesson covers the different types of keys used in relational databases. ** ** ** ** Types of Keys ** ** * Primary Key * == A primary key is a unique identifier for each record in a table. ** ** == It ensures that each row in a table is distinct, meaning no duplicate values are allowed in the ~~ ~~ primary key column(s). Primary keys cannot contain null values. ** Example: In a Student table, Roll Number could be a primary key since each student ** ` ` ` ` *** *** has a unique roll number. * Candidate Key * ==* A candidate key is any attribute, or a combination of attributes, that can uniquely ** ** ** ** ** ** ** identify a row in a table. ** *== * == A table can have multiple candidate keys, but only one of them can be chosen as the ** ** ** primary key. ** == * ** Example: In the Student table, both Roll Number and Email (if each student has a ** ` ` ` ` ` ` unique email) could be candidate keys, as both can uniquely identify a student. ** ** * Alternate Key * * An alternate key is a candidate key that is not selected as the primary key. ** ** == ** ** ** ** ~~ ~~ == * It serves as an alternative way to uniquely identify records within the table. == == ** Example: If Roll Number is chosen as the primary key, Email would be an alternate ** ` ` ** ** ` ` key for the Student table. ` ` * Foreign Key * * A foreign key is an attribute in one table that links to the primary key of another table. ** ** == ** ** == * * Foreign keys establish relationships between tables, enforcing referential integrity by == ** ** ** **== ensuring that values in the foreign key column must match values in the referenced primary ** key column. ** * ** Example: In a Student table that records the student data, CourseID , which is the ** ` ` ` ` ** primary key in the Course table, could be a foreign key in the Student table. ** ` ` ** ** ` ` * Composite Key * * A composite key is a key that consists of two or more attributes used together to uniquely ** ** ** ** identify a record. * ==* Composite keys are often used when no single attribute can uniquely identify a row. *== ** Example: In an Enrollment table, a combination of Student ID and Course ID ** ` ` ` ` ` ` might serve as a composite key, uniquely identifying each enrollment record. ** ** * Super Key (Or Superkey) * * * * * * A super key is any combination of attributes that can uniquely identify a row in a table. ** ** ==** ** == * * Super keys include all candidate keys, primary keys, and any additional attributes that == ** ** ** ** ** ** make them unique. == * ==* Super keys may contain more attributes than necessary to ensure uniqueness. *== ** Example: In the Student table, {Roll Number, Name} and {Roll Number, Email} ** ` ` ` ` ` ` are super keys because they uniquely identify each row, although Roll Number alone ** ** * ` ` could also act as a key. * A super key can include attributes that aren’t individually unique, as long as the ** ** * * combination of attributes ensures uniqueness for each row. ** ** Summary Table of Keys ** ** ~ == Key Type == ~ ~ == Purpose == ~ ~ == Example == ~ Primary Key Uniquely identifies each record Roll Number in Student in a table Candidate Key Potential attributes for unique Roll Number, Email identification Alternate Key Candidate key not chosen as Email if Roll Number is primary primary key Foreign Key Establishes a link between two Course ID in Student tables Composite Key Combines multiple attributes to Student ID and Course ID ensure uniqueness Super Key Any set of attributes that {Roll Number, Name} uniquely identifies a row