Know and understand characteristics of primary key and foreign keys

18 Databases

Objective: Know and understand characteristics of primary key and foreign key 🔑

Primary Key (PK) 📚

A primary key is a column (or set of columns) that uniquely identifies each row in a table. Think of it as a student ID card – no two students can have the same ID.

  • Uniqueness: Each value must be different.
  • Not Null: Every row must have a value.
  • Stable: The value rarely changes.
  • Index: Most DBMS automatically index the PK for fast look‑ups.
Table Primary Key Column
Students student_id
Courses course_id

Foreign Key (FK) 🔗

A foreign key is a column that creates a link between two tables. It references the primary key of another table, ensuring data consistency. Imagine a class roster that lists the student_id of each student – that ID must exist in the Students table.

  1. It points to a PK in another table.
  2. It enforces referential integrity – you cannot insert a value that doesn't exist in the referenced table.
  3. It can be nullable if the relationship is optional.
  4. It can be part of a composite key.
Table Foreign Key Column References
Enrollments student_id Students.student_id
Enrollments course_id Courses.course_id

Exam Tips 📝

  • When asked to identify a PK, look for a column that is unique and non‑null.
  • For FK questions, check that the column matches the data type of the referenced PK.
  • Remember that a table can have only one PK but many FKs.
  • Use the analogy of a library: the book’s ISBN is the PK; the author ID in the authors table is a FK.
  • Practice drawing ER diagrams: PKs are underlined, FKs are dotted lines pointing to the PK they reference.

Revision

Log in to practice.

3 views 0 suggestions