The relational model stores everything in tables. This topic names the parts of a table, shows what a schema is and explains how an ER diagram is turned into tables.

Terms of the relational model

Formal term Everyday term Meaning
Relation Table A set of rows with the same columns
Tuple Row or record One entry in the table
Attribute Column or field A named property
Domain Allowed values Data type and range of a column
Degree Number of columns 5 columns means degree 5
Cardinality Number of rows 120 students means cardinality 120

Rows have no fixed order, and a relation holds no duplicate rows.

Relational schema and instance

The schema is the design of a table: its name, columns and types. The instance is the data inside it at one moment.

student(roll_no INT, name VARCHAR(40), marks INT, dept_id INT)

This line is a schema. The rows currently stored are the instance, and they change all the time while the schema changes rarely.

Integrity rules

  • Entity integrity: a primary key can never be NULL.
  • Referential integrity: a foreign key must match an existing primary key value, or be NULL.
  • Domain integrity: values must come from the column's domain.

Converting an ER diagram to tables

  1. Each strong entity becomes a table; its key becomes the primary key.
  2. A 1:N relationship: put the key of the "1" side into the "N" side as a foreign key.
  3. An M:N relationship becomes a new table whose primary key combines both keys, plus any relationship attributes.
  4. A multi-valued attribute becomes its own table with the entity key.
  5. A weak entity becomes a table whose key is the owner key plus its partial key.

Applying this to the registrar example:

department(dept_id PK, name)
student(roll_no PK, name, phone, dept_id FK -> department)
course(course_id PK, title, credits, dept_id FK -> department)
enrolment(roll_no FK, course_id FK, grade, PRIMARY KEY (roll_no, course_id))

Common mistakes

  • Confusing degree (columns) with cardinality (rows).
  • Putting the foreign key on the wrong side of a 1:N relationship.
  • Forgetting the extra table that an M:N relationship needs.

Practice questions

  1. Define relation, tuple, attribute and domain.
  2. Differentiate schema and instance.
  3. State entity integrity and referential integrity.
  4. How is an M:N relationship converted into tables?
  5. Convert an ER diagram of a hospital with doctors and patients into tables.

Answers to the practice questions

  1. Relation: table; tuple: row; attribute: column; domain: allowed values of a column.
  2. Schema is the structure of a table; instance is the data it holds at a moment.
  3. Entity integrity: primary key is never NULL. Referential integrity: a foreign key matches an existing key or is NULL.
  4. As a separate table with both keys combined as the primary key, plus any relationship attributes.
  5. doctor(doctor_id PK, name, speciality), patient(patient_id PK, name, age), appointment(doctor_id FK, patient_id FK, date, PRIMARY KEY (doctor_id, patient_id, date)).

Sources

  • Official BCA syllabus Database Management Systems, course code CC202, Unit I
  • Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill