MCS-201 · Unit 16

Data Access Using Python

Section 2 of 7

Block 4 · Unit 16Data Access Using Python9 min · Section 2 of 7

16.2The Entity-Relationship and Relational Models

Covers IGNOU MCS-201 §16.2

A data model describes how data is structured, related and constrained. Designers usually start with an entity-relationship (ER) diagram to capture the real world, then turn it into tables in the relational model, which MySQL implements.

Data models

ModelOrganises data asStatus
Flat fileOne table in one file, no relationshipsSimple data only
HierarchicalA tree of parent and child recordsOlder systems
NetworkRecords linked in a graphOlder systems
RelationalTables related through common columnsThe standard model (MySQL, Oracle, PostgreSQL)

The entity-relationship model

ConceptMeaningExampleER diagram symbol
EntityA real-world object that can be identifiedAn employee, a departmentRectangle
AttributeA property of an entityemp_name, ageEllipse
RelationshipAn association between entitiesEmployee works in departmentDiamond
CardinalityHow many of one entity relate to the otherOne-to-one, one-to-many, many-to-one, many-to-manyLabels on the lines
ER diagram: Employee works in Department (many-to-one)
 (emp_id) (emp_name) (emp_addr)          (dept_id) (dept_name)
      \       |       /                       \       /
      [  Employee  ]------< works in >------[ Department ]
                      N                    1

Many employees work in one department, and each employee works in one department at a time.

The relational model

E. F. Codd proposed the relational model in 1970. Data is stored in relations (tables).

Formal termCommon termMeaning
RelationTableA set of rows with the same columns
TupleRow, recordOne entity instance, e.g. one employee
AttributeColumn, fieldOne property, e.g. salary
DomainData type and allowed valuesE.g. salary: positive numbers
DegreeNumber of columns
CardinalityNumber of rows

Keys

KeyDefinitionExample in employee(emp_code, emp_name, email, dept_id)
Super keyAny set of columns that uniquely identifies a row(emp_code, emp_name)
Candidate keyA minimal super key (no column can be removed)emp_code; email
Primary keyThe candidate key chosen to identify rows; unique and NOT NULL; one per tableemp_code
Alternate keyA candidate key not chosen as primary keyemail
Composite keyA key made of two or more columns(order_id, product_id) in an order-lines table
Foreign keyA column that refers to the primary (or another unique) key of another table, linking the twodept_id referring to department(dept_id)

Key takeaways

  • ER model: entities (rectangles), attributes (ellipses), relationships (diamonds), cardinality.
  • Relational model: relation = table, tuple = row, attribute = column.
  • Keys: candidate, primary (unique, not null, one per table), alternate, composite, foreign.
  • Foreign keys implement relationships and referential integrity.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In an ER diagram, what does a diamond represent?

Show answer

Answer: C. A relationship

Rectangles are entities, ellipses are attributes and diamonds are relationships.

Question 2 · multiple choice

What is a row of a relation called in the relational model?

Show answer

Answer: A. A tuple

Rows are tuples (records); columns are attributes (fields).

Question 3 · multiple choice

A table has 5 columns and 120 rows. What are its degree and cardinality?

Show answer

Answer: C. Degree 5, cardinality 120

Degree counts columns; cardinality counts rows.

Question 4 · multiple choice

Which key can contain NULL values?

Show answer

Answer: D. A foreign key (when the relationship is optional)

Primary key columns must never be NULL; a foreign key may be NULL if, for example, an employee is not yet assigned to a department.

Question 5 · multiple choice

In student(roll_no, aadhaar_no, name), both roll_no and aadhaar_no are unique. If roll_no is the primary key, what is aadhaar_no?

Show answer

Answer: B. An alternate key

It is a candidate key that was not chosen as the primary key.

Question 6 · short answer

A doctor can treat many patients and a patient can be treated by many doctors. How should this many-to-many relationship be stored in relational tables?

Show worked answer

Use three tables: doctor(doctor_id, name, ...) with primary key doctor_id, patient(patient_id, name, ...) with primary key patient_id, and a linking table treatment(doctor_id, patient_id, date). In treatment, doctor_id and patient_id are foreign keys referring to the two tables, and together (possibly with date) they form a composite primary key. Each row records one doctor treating one patient.

↑ ↓ move↵ openTab switch type