Define the term Foreign Key with respect to RDBMS. Explain how it is used to establish a relationship between two tables. Give one example to support your answer.

Answer:

Answer by student

A foreign key is an attribute or a set of attributes in a table that refers to the primary key of another table. It is used to establish a relationship between two tables by ensuring that the values in the foreign key column are valid and consistent with the values in the primary key column of the referenced table. 

For example, if there are two tables Student and Course, where Student has an attribute CourseID that refers to the primary key ID of Course, then CourseID is a foreign key in Student.

Detailed answer by teachoo

-lock-

  • A foreign key is an attribute or a set of attributes in a table that refers to the primary key of another table.
  • The primary key is an attribute or a set of attributes that uniquely identifies each tuple in a table.
  • A foreign key is used to establish a relationship between two tables by ensuring that the values in the foreign key column are valid and consistent with the values in the primary key column of the referenced table. This is called referential integrity .
  • A foreign key can also be called a referencing attribute or a child attribute .
  • A foreign key can help to avoid data redundancy and inconsistency by linking related data in different tables.
  • For example, if there are two tables Student and Course, where Student has an attribute CourseID that refers to the primary key ID of Course, then CourseID is a foreign key in Student. This means that each student can enroll in only one course, and the course ID must exist in the Course table. The relationship between Student and Course can be represented as:

Student

Course

StudentID (PK)

ID (PK)

Name

Name

CourseID (FK)

Duration

 

-endlock-

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.