Lec-10: Foreign Key in DBMS | Full Concept with examples | DBMS in Hindi
What is a Foreign Key in Databases?
Introduction to Foreign Keys
- The video introduces the concept of foreign keys in databases, explaining their significance and usage.
- It is structured into two parts: defining what a foreign key is and discussing its purpose in databases.
Definition of Foreign Key
- A foreign key is described as an attribute or set of attributes that references the primary key of either the same table or another table.
- Emphasis on the nature of primary keys being either simple (one attribute) or composite (multiple attributes).
Referential Integrity
- The concept of referential integrity is introduced, highlighting its importance in maintaining data consistency across tables.
- The video mentions that foreign keys are crucial for enforcing referential integrity within database systems.
Understanding Tables and Relationships
Example with Student and Course Tables
- Two example tables are presented: "student" as the base table containing student data, with roll number as the primary key.
- The "course" table includes course ID, course name, and roll number as a foreign key linking back to the student table.
Importance of Common Attributes
- For two tables to be related, at least one common attribute must exist; here, roll number serves this purpose.
- Roll number acts as a foreign key in the course table referencing the primary key from the student table.
Data Entry and Constraints
Data Entry Rules
- When entering data into tables, it’s essential to ensure that any value entered as a foreign key exists in its referenced primary key column first.
Referencing Tables Explained
- The terms "referencing table" (where the foreign key exists) and "referenced table" (where the primary key resides) are clarified for better understanding.
Creating Foreign Keys in SQL
SQL Syntax for Creating Tables
- An example SQL command illustrates how to create a course table with roll number defined as a foreign key referencing the student table's primary key.
Altering Existing Tables
Understanding Foreign Keys in Database Management
Introduction to Foreign Keys
- The process of adding a foreign key constraint involves specifying the table name and the constraint name, such as "FK Name". A foreign key is a type of constraint that establishes a relationship between two tables.
- When creating a foreign key, it references the primary key from another table. In this case, the roll number serves as the foreign key referencing the primary table named "student".
- After creating the initial table, you can alter it to add constraints. This includes defining which column will serve as the foreign key and what primary key it references.
Key Points on Foreign Keys
- It is important to note that while both tables may use "roll number" as their identifier, they do not need to have identical names for their columns.
- A single table can contain multiple foreign keys; however, there can only be one primary key per table. This distinction is crucial for maintaining database integrity.
Conclusion and Next Steps