Lec-10: Foreign Key in DBMS | Full Concept with examples | DBMS in Hindi

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

Video description

👉Subscribe to our new channel:https://www.youtube.com/@varunainashots ►Foreign Key part-2: https://youtu.be/DM2lAomoDrg ► Database Management System(Complete Playlist): https://www.youtube.com/playlist?list=PLxCzCOWd7aiFAN6I8CuViBuCdJgiOkT2Y Other subject-wise playlist Links: -------------------------------------------------------------------------------------------------------------------------------------- ►Design and Analysis of algorithms (DAA): https://www.youtube.com/playlist?list=PLxCzCOWd7aiHcmS4i14bI0VrMbZTUvlTa ►Computer Architecture (Complete Playlist): https://www.youtube.com/playlist?list=PLxCzCOWd7aiHMonh3G6QNKq53C6oNXGrX ► Theory of Computation https://www.youtube.com/playlist?list=PLxCzCOWd7aiFM9Lj5G9G_76adtyb4ef7i ►Artificial Intelligence: https://www.youtube.com/playlist?list=PLxCzCOWd7aiHGhOHV-nwb0HR5US5GFKFI ►Computer Networks (Complete Playlist): https://www.youtube.com/playlist?list=PLxCzCOWd7aiGFBD2-2joCpWOLUrDLvVV_ ►Operating System: https://www.youtube.com/playlist?list=PLxCzCOWd7aiGz9donHRrE9I3Mwn6XdP8p ►Structured Query Language (SQL): https://www.youtube.com/playlist?list=PLxCzCOWd7aiHqU4HKL7-SITyuSIcD93id ►Discrete Mathematics: https://www.youtube.com/playlist?list=PLxCzCOWd7aiH2wwES9vPWsEL6ipTaUSl3 ►Compiler Design: https://www.youtube.com/playlist?list=PLxCzCOWd7aiEKtKSIHYusizkESC42diyc ►Number System: https://www.youtube.com/playlist?list=PLxCzCOWd7aiFOet6KEEqDff1aXEGLdUzn ►Cloud Computing & BIG Data: https://www.youtube.com/playlist?list=PLxCzCOWd7aiHRHVUtR-O52MsrdUSrzuy4 ►Software Engineering: https://www.youtube.com/playlist?list=PLxCzCOWd7aiEed7SKZBnC6ypFDWYLRvB2 ►Data Structure: https://www.youtube.com/playlist?list=PLxCzCOWd7aiEwaANNt3OqJPVIxwp2ebiT ►Graph Theory: https://www.youtube.com/playlist?list=PLxCzCOWd7aiG0M5FqjyoqB20Edk0tyzVt ►Programming in C: https://www.youtube.com/playlist?list=PLxCzCOWd7aiGmiGl_DOuRMJYG8tOVuapB ►Digital Logic: https://www.youtube.com/playlist?list=PLxCzCOWd7aiGmXg4NoX6R31AsC5LeCPHe --------------------------------------------------------------------------------------------------------------------------------------- Our social media Links: ► Subscribe to us on YouTube: https://www.youtube.com/gatesmashers ►Subscribe to our new channel: https://www.youtube.com/@varunainashots ► Like our page on Facebook: https://www.facebook.com/gatesmashers ► Follow us on Instagram: https://www.instagram.com/gate.smashers ► Follow us on Instagram: https://www.instagram.com/varunainashots ► Follow us on Telegram: https://t.me/gatesmashersofficial ► Follow us on Threads: https://www.threads.net/@gate.smashers -------------------------------------------------------------------------------------------------------------------------------------- ►For Any Query, Suggestion or notes contribution: Email us at: gatesmashers2018@gmail.com #foreignKey#Constraints#ReferentialIntegrity#DBMS#GATE#ugcNet