1. Home
  2. Explore
  3. Computer Science
  4. Foreign Keys and Referential Integrity

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Foreign Keys and Referential Integrity

Speaker 1Speaker 2

Foreign Keys and Referential Integrity

10:25
0:00-10:25
  1. Hope

    Today we are building two connected ideas: referential integrity in relational databases, and the first two relational algebra operators, selection and projection. We will use one example academic database throughout, so it is worth learning its schema carefully.

  2. Anna

    Let us begin with the key concept behind referential integrity. What exactly is a foreign key?

  3. Hope

    A foreign key is a group of attributes in one relation whose values are used to refer to tuples in another relation. Referential integrity says that those references must point to tuples that actually exist in the database instance. In other words, the database should not contain dangling references; a foreign-key value should either refer to an existing tuple or, where allowed, be null.

  4. Anna

    And the example database models an institute with departments, students, faculty, courses, enrollments, teaching assignments, and prerequisites. The same schema will be useful later when we discuss queries and SQL.

  5. Hope

    Exactly. The first relation is Student, with attributes roll number, name, degree, year, sex, department number, and advisor. Roll number is underlined in the relational schema, which means it is a key. Its value uniquely identifies a tuple, or row, in Student.

  6. Anna

    The other Student attributes describe the student: degree records the program, year records the year of admission, department number identifies the student's department, and advisor contains the employee ID of the student's faculty advisor. Every student is required to have an advisor, and more details about that advisor can be found in the faculty relation.

  7. Hope

    Next is Department, with department ID, name, HOD, and phone. The phone is the department office number, while HOD stores the employee ID of the faculty member who heads the department. Department ID is chosen as the key, even though the department name could also serve as a key in this example.

  8. Anna

    Then we have Professor, whose attributes are employee ID, name, sex, start year, department number, and phone. Employee ID identifies the professor. Start year records when that faculty member joined, and department number identifies the department in which the professor works.

  9. Hope

    The Course relation records course ID, course name, credits, and department number. Course ID is the key, while course name gives the name of the subject, credits gives the number of credits earned on completion, and department number identifies the department offering the course.

  10. Anna

    Now comes Enrollment, which captures students taking courses and receiving grades. Its key contains four attributes: roll number, course ID, semester, and year.

  11. Hope

    All four are necessary because a student can take several courses, and the same course can be offered repeatedly across different semesters and years. Semester is represented by the attribute named sem, with values odd or even, standing for the August and January semesters. A current-semester enrollment has a null grade, while completed earlier enrollments have an appropriate grade value.

  12. Anna

    So Enrollment does not merely say that a student took a course. It identifies the particular offering of that course and the particular student, and it records the result when the semester is complete.

  13. Hope

    The Teaching relation records which professor teaches which course offering. It contains employee ID, course ID, semester, year, and classroom, with all of these attributes forming the key. No smaller subset is enough: a professor may teach several courses, a course may be taught by several professors, and a course offering is distinguished by its semester and year.

  14. Anna

    For example, one tuple could say that a professor teaches CS3700 in the odd semester of 2019 in classroom CS25. The full combination identifies that particular teaching assignment.

  15. Hope

    Finally, Prerequisite has two attributes: preReqCourse and course ID. A tuple containing C1 and C2 means that C1 is a prerequisite for C2, so C1 must be successfully completed before a student enrolls in C2. Data Structures being a prerequisite for Database Systems is an example.

  16. Anna

    Let us connect these relations through foreign keys. In Student, department number refers to department ID in Department, and advisor refers to employee ID in Professor. Thus, the department and advisor values in a student tuple must identify existing department and professor tuples, subject to the null rule where applicable.

  17. Hope

    In Department, HOD is a foreign key referring to Professor's employee ID. In Professor, department number refers to Department's department ID. In Course, department number also refers to Department's department ID.

  18. Anna

    Enrollment has two important references: roll number refers to Student's roll number, and course ID refers to Course's course ID. Even though these attributes are also components of Enrollment's composite key, they can independently be foreign keys.

  19. Hope

    Teaching contains employee ID referring to Professor, and course ID referring to Course. Prerequisite has two foreign keys: both preReqCourse and course ID refer to Course's course ID, because both values identify courses in the Course relation.

  20. Anna

    One detail can cause confusion: the same concept may have different attribute names in different relations. Department number and department ID represent the department identifier in their respective contexts, but a foreign key does not have to use the same name as the key it references.

  21. Hope

    And when the same attribute name appears in multiple relations, dot notation removes ambiguity. Course.courseID means the course ID attribute from Course, while another relation's qualified name identifies the attribute from that relation. A relational schema diagram conventionally shows relation names, attributes, underlined keys, and arrows from foreign keys to the keys they reference.

  22. Anna

    That diagram is worth keeping available because it supports later relational algebra and SQL queries. Now how does relational algebra fit into the picture?

  23. Hope

    Relational algebra is a set of operators that take relation instances as input and produce new relation instances as output. Some operators are unary, meaning they take one relation, and others are binary, meaning they take two. The operators provide a way to express database retrieval requests, or queries.

  24. Anna

    What makes relational algebra different from simply stating the information we want?

  25. Hope

    Relational algebra is procedural: an expression describes the operations to perform, such as operating on one relation, combining results, and then applying another operation. An expression can be viewed as a tree through which intermediate results flow upward until the final result is produced.

  26. Anna

    The relational query engine uses these operators as a core part of the database system. In this module, though, we treat them as logical or mathematical operators rather than studying how each one is implemented.

  27. Hope

    Right. SQL is largely declarative: it specifies what data is wanted rather than giving the complete procedure for obtaining it. Internally, SQL queries can be translated into relational algebra expressions, which creates opportunities to rewrite and optimize how the query is executed.

  28. Anna

    The operator set includes selection, projection, cross product, union, intersection, difference, and join. Today we focus on selection and projection, and it is also noted that not every listed operator is necessarily required in a minimum set.

  29. Hope

    Selection is a unary operator that chooses tuples satisfying a condition. Its notation is sigma with a condition theta applied to a relation R: sigma sub theta of R. The result has the same schema as R, including the same attributes and their order, but contains only tuples that satisfy theta.

  30. Anna

    So selection filters rows without changing the columns.

  31. Hope

    Exactly. For example, to obtain professors named Giridhar, we select from Professor using the condition name equals the string Giridhar. If several professors have that name, every matching tuple is included, and the resulting relation still has the complete Professor schema.

  32. Anna

    Another example asks for professors who joined between 1980 and 1985, inclusive. The condition combines start year greater than or equal to 1980 with start year less than or equal to 1985.

  33. Hope

    Selection conditions can be basic or composite. A basic condition compares one attribute with another attribute, or compares an attribute with a suitable constant from the appropriate domain. The comparison operators are less than, less than or equal to, greater than, greater than or equal to, equal to, and not equal to.

  34. Anna

    And composite conditions combine basic conditions using Boolean operators: AND, OR, and NOT. The comparisons produce Boolean results, and the logical operators combine those results into a larger selection condition.

  35. Hope

    Selection operations can also be composed. Selecting tuples satisfying condition C2 and then selecting those satisfying C1 gives the same result as doing the two selections in the opposite order. Both expressions retain exactly the tuples satisfying both C1 and C2, so selection is commutative in this sense.

  36. Anna

    That identity matters later because equivalent algebraic expressions can be rewritten into forms that are more efficient to execute.

  37. Hope

    Now compare selection with projection. Projection is also unary, but instead of filtering tuples, it keeps only specified attributes and discards the others. Its notation uses pi followed by a list of attributes and then the input relation.

  38. Anna

    So projection filters columns rather than rows. If we project roll number and name from Student, the result has just those two attributes.

  39. Hope

    Correct. The output schema is exactly the projection list. Because a relation is a set of tuples, the result cannot contain duplicate tuples. If two input tuples become identical after unwanted attributes are removed, only one copy appears in the projected relation.

  40. Anna

    The example with master's students makes that clear. Selecting Student tuples where degree equals MS may produce two different students named Mahesh and another student named Rajesh. Projecting only name produces Mahesh once and Rajesh once, so information distinguishing the two Mahesh tuples is lost.

  41. Hope

    But projecting roll number and name from Student preserves the number of tuples because roll number is a key. No two Student tuples can have the same roll number, so no duplicate projected tuples can arise from that combination.

  42. Anna

    That leads to the final rule about result size. If the projection list contains a key of the input relation, the output has exactly as many tuples as the input. If the projection list does not contain a key, the output may have fewer tuples because different input tuples can collapse into one identical projected tuple.

  43. Hope

    So we have covered the complete example schema, its keys and foreign-key references, the meaning of referential integrity, and the first two relational algebra operators. Selection keeps qualifying tuples and preserves the schema; projection keeps chosen attributes and may remove duplicate results because relations are sets.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent