7 Relational Database Design

Database Management System · Unit 7 · 7 hrs

Relational Database Design

Exam-focused notes for Relational Database Design (Database Management System, CSC265): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.

Relational Database Design in short

This unit turns an ER diagram into a set of relational tables, lays out informal design guidelines for spotting a poor schema, and then formalizes "poor schema" precisely through functional dependencies and the normal forms (1NF through 4NF), the part of the syllabus where marks are lost or won on rigor.

Relational Database Design: notes

Relational Database Design Using ER-to-Relational Mapping

Once an ER diagram exists, it is mapped to relational tables by a fixed set of rules, one rule per ER construct.

  • Strong entity types become a table whose columns are the entity's attributes, with the entity's key attribute as the table's primary key.
  • Weak entity types become a table holding the weak entity's own attributes plus the primary key of its owner (identifying) entity, which serves as part of the weak entity's key and as a foreign key back to the owner; for example, a DEPENDENTS weak entity owned by EMPLOYEE becomes DEPENDENTS(Employee_ssn, Dependent_name, ...) with Employee_ssn a foreign key referencing EMPLOYEE.
  • Binary $1{:}1$ relationships are mapped by placing the primary key of either participating entity as a foreign key in the table of the other (the choice is usually driven by which side has total participation).
  • Binary $1{:}N$ relationships place the primary key of the "one" side as a foreign key in the table on the "many" side; a STUDENT enrolled in one COURSE ($1{:}N$ from COURSE to STUDENT) puts Course_id as a foreign key inside the STUDENT table. A separate relationship table is only needed if the relationship itself carries its own attributes.
  • Binary $M{:}N$ relationships always need their own table, containing the primary keys of both participating entities (together forming its primary key) plus any attributes of the relationship itself; STUDENT enrolled in many COURSEs becomes an ENROLLED(Sid, Cid) table.
  • Multivalued attributes get their own table, holding the entity's primary key alongside the multivalued attribute, since a fixed number of columns cannot hold an unbounded number of values.
  • Composite attributes are not given a separate column at all; only their simple component attributes are added directly to the entity's table (an Address composite becomes separate Street, City, State columns, not one Address column).
  • $n$-ary relationships ($n > 2$) always get their own table, holding the primary keys of every participating entity as foreign keys plus any of the relationship's own attributes.
  • Specialization/generalization maps the superclass's primary key down into each subclass table as both its primary key and a foreign key referencing the superclass, so each subclass table holds only its own extra attributes plus the inherited key.
  • Aggregation (treating a whole relationship as a higher-level entity participating in a further relationship) is mapped like an $n$-ary relationship: a new table holds the primary keys of the aggregated relationship's participants together with the primary key of the further entity involved, plus any attributes of its own.

Informal Design Guidelines for Relational Schemas

Before any formal theory, four informal guidelines separate a good relational schema from a poor one.

Guideline 1: clear semantics. Design each relation so its meaning is easy to state in one sentence; do not combine attributes from more than one entity type or relationship type into a single relation, since a relation mixing several real-world concepts becomes semantically ambiguous.

Guideline 2: avoid redundancy and update anomalies. Storing the natural join of what should be several base relations as one relation typically wastes storage and introduces three kinds of update anomaly. An insertion anomaly forces an unrelated fact to be duplicated (or forces a NULL) just to insert one new fact, for example being unable to record a new course's fee until at least one student has enrolled in it, because course and enrollment are stored in a single table. A deletion anomaly loses an unrelated fact as a side effect of deleting something else, for example losing all record of a course's fee if the last student enrolled in it withdraws. A modification anomaly requires updating many rows to keep one fact consistent, for example needing to update the fee in every row of every student enrolled in a course if that course's fee changes; missing even one row leaves the data inconsistent. A relation vulnerable to these is called a fat relation, one grouping too many independent facts together.

Guideline 3: minimize NULLs. Attributes whose value frequently does not apply to a given tuple should not be placed in that relation at all, since a proliferation of NULLs wastes space and complicates aggregate functions and joins, whose results become unpredictable in the presence of NULL.

Guideline 4: avoid spurious tuples. A relation should only be split (or two relations joined) on an attribute that is genuinely a (primary key, foreign key) pair; joining on any other shared attribute can generate extra, meaningless rows, called spurious tuples, that were never part of the original data and cannot be traced back to it.

What this unit covers

  • Relational Database Design Using ER-to-Relational Mapping
  • Informal Design Guidelines for Relational Schemas
  • Functional Dependencies
  • Normal Forms Based on Primary Keys
  • General Definitions of Second and Third Normal Forms
  • Boyce-Codd Normal Form
  • Multivalued Dependency and Fourth Normal Form
  • Properties of Relational Decomposition

Normal Forms Based on Primary Keys

2080 · 10 marks

Define normalization. Why normalization is important in database design? Explain 1NF, 2NF and 3NF with suitable example.[10]

Show solved answer

Normalization in Database Design

Definition of Normalization

Normalization is the process of organizing the attributes and tables of a relational database to minimize data redundancy and dependency. It involves decomposing a large, poorly structured table into smaller, well-structured tables while preserving data integrity and relationships. The process is guided by a series of rules called Normal Forms (NF).


Why Normalization is Important in Database Design

Normalization is important for the following reasons:

ReasonExplanation
Eliminates RedundancyAvoids storing the same data in multiple places, saving storage space
Prevents Update AnomaliesEnsures that updating one record does not require updating many records
Prevents Insertion AnomaliesAllows inserting data without requiring unrelated data to be present
Prevents Deletion AnomaliesPrevents accidental loss of useful data when deleting a record
Improves Data IntegrityMaintains consistency and accuracy of data across the database
Simplifies QueriesWell-structured tables make querying easier and more efficient

Types of Anomalies (Before Normalization)

Consider the following unnormalized table:

StudentIDStudentNameCourseIDCourseNameInstructorName
1RamC01DBMSDr. Sharma
1RamC02OSDr. Thapa
2SitaC01DBMSDr. Sharma
  • Update Anomaly: If Dr. Sharma's name changes, it must be updated in multiple rows.
  • Insertion Anomaly: A new course cannot be added without a student.
  • Deletion Anomaly: Deleting Sita's record also deletes information about Course C01.

Normal Forms

1. First Normal Form (1NF)

Definition: A relation is in 1NF if:

  • All attributes contain only atomic (indivisible) values.
  • Each column contains values of a single type.
  • Each row is unique (has a primary key).
  • There are no repeating groups or multi-valued attributes.

Example of a table NOT in 1NF:

StudentIDStudentNameCourses
1RamDBMS, OS
2SitaDBMS

Here, the Courses column contains multiple values, which violates 1NF.

Converting to 1NF:

StudentIDStudentNameCourse
1RamDBMS
1RamOS
2SitaDBMS

Now each cell has an atomic value. This table is in 1NF. Primary Key: (StudentID, Course)


2. Second Normal Form (2NF)

Definition: A relation is in 2NF if:

  • It is already in 1NF.
  • Every non-key attribute is fully functionally dependent on the entire primary key (no partial dependency).

Partial Dependency: A non-key attribute depends on only a part of a composite primary key.

Example of a table in 1NF but NOT in 2NF:

StudentIDCourseStudentNameInstructorName
1DBMSRamDr. Sharma
1OSRamDr. Thapa
2DBMSSitaDr. Sharma

Primary Key: (StudentID, Course)

Functional Dependencies:

  • (StudentID, Course) --> InstructorName (full dependency - OK)
  • StudentID --> StudentName (partial dependency - VIOLATION)

StudentName depends only on StudentID, not on the full composite key.

Converting to 2NF (Remove partial dependencies):

Student Table:

StudentIDStudentName
1Ram
2Sita

Enrollment Table:

StudentIDCourseInstructorName
1DBMSDr. Sharma
1OSDr. Thapa
2DBMSDr. Sharma

Now all non-key attributes are fully dependent on the primary key. Both tables are in 2NF.


3. Third Normal Form (3NF)

Definition: A relation is in 3NF if:

  • It is already in 2NF.
  • There is no transitive dependency of non-key attributes on the primary key.

Transitive Dependency: A non-key attribute depends on another non-key attribute, which in turn depends on the primary key. If A --> B and B --> C, then A --> C is a transitive dependency.

Example of a table in 2NF but NOT in 3NF:

StudentIDCourseInstructorIDInstructorName
1DBMSI01Dr. Sharma
1OSI02Dr. Thapa
2DBMSI01Dr. Sharma

Primary Key: (StudentID, Course)

Functional Dependencies:

  • (StudentID, Course) --> InstructorID (full dependency - OK)
  • InstructorID --> InstructorName (transitive dependency - VIOLATION)

Here: (StudentID, Course) --> InstructorID --> InstructorName

InstructorName depends on InstructorID (a non-key attribute), not directly on the primary key.

Converting to 3NF (Remove transitive dependencies):

Enrollment Table:

StudentIDCourseInstructorID
1DBMSI01
1OSI02
2DBMSI01

Instructor Table:

InstructorIDInstructorName
I01Dr. Sharma
I02Dr. Thapa

The transitive dependency is now gone. InstructorName is stored exactly once, in the table whose key it actually depends on, while the Enrollment table keeps only InstructorID as a foreign key. Both tables are in 3NF, and the original information can be recovered without loss by joining them on InstructorID.

The anomalies listed at the beginning disappear with it. Correcting Dr. Sharma's name now changes a single row of the Instructor table, so there is no update anomaly. A new instructor can be recorded before any student enrols under that instructor, so there is no insertion anomaly. Deleting the last enrolment for a course no longer erases the instructor's details, so there is no deletion anomaly.


Summary of the Three Normal Forms

Normal FormRequirementProblem Removed
1NFAll attribute values atomic, no repeating groupsMulti valued cells that cannot be queried or indexed
2NFIn 1NF and every non key attribute fully dependent on the whole primary keyRedundancy caused by partial dependency on part of a composite key
3NFIn 2NF and no transitive dependency on the primary keyRedundancy caused by one non key attribute determining another

Normalization therefore moves a design from a single wide table full of repeated facts towards a set of narrow tables in which every fact is stored once and depends on the key, the whole key and nothing but the key. Stricter forms such as BCNF and 4NF handle the remaining special cases, but 3NF is normally sufficient for practical database design, and a controlled amount of denormalization is added afterwards only where read performance demands it.

Open this answer on its own page →

2076 · 10 marks

What is normal form? Explain their types. Explain about loss-less join decomposition.[10]

The normal form of a relation refers to the highest normal form condition that it meets, and hence indicates the degree to which it has been normalized. Normal forms are used to eliminate redundancy, insertion anomalies, deletion anomalies, and update anomalies from a relational database. Normal ...

Open this answer on its own page →

General Definitions of Second and Third Normal Forms

2079 · 10 marks

What is normalization? Why normalization is required? Explain 1NF, 2NF, and 3NF with example.[10]

--- Normalization is the process of organizing the attributes and tables of a relational database to reduce data redundancy and improve data integrity. It involves decomposing a large, poorly structured table into smaller, well-structured tables while preserving the relationships between the data...

Open this answer on its own page →

Functional Dependencies

2079 · 5 marks

Define functional dependencies. Explain 2nd normal form with example. [5]

Show solved answer

Functional Dependencies and Second Normal Form (2NF)


Functional Dependency (FD)

A functional dependency is a constraint between two sets of attributes in a relation. For a relation R, a functional dependency X → Y means that the value of attribute set X uniquely determines the value of attribute set Y.

  • X is called the determinant
  • Y is called the dependent

Example: In a Student table, StudentID → StudentName means that knowing the StudentID uniquely determines the StudentName.

A set of functional dependencies is given for each relation, and each relation has a designated primary key, which is then evaluated for normalization. (from notes)


Second Normal Form (2NF)

"To be in second normal form, a relation must be in First Normal Form AND the relation must not contain any partial dependency." (from notes)

Key Definitions:

  • Non-prime attribute: An attribute that is not part of any candidate key
  • Partial Dependency: When a proper subset of a candidate key determines a non-prime attribute
Partial Dependency = Proper subset of candidate key → non-prime attribute

Conditions for 2NF:

  1. The relation must be in 1NF
  2. There must be no partial dependency (every non-prime attribute must be fully functionally dependent on the whole candidate key)

Example

Table NOT in 2NF:

StudentIDCourseIDStudentNameCourseNameGrade
S01C01RamDBMSA
S01C02RamOSB
S02C01SitaDBMSA+
  • Primary Key (Composite): {StudentID, CourseID}
  • Functional Dependencies:
    • StudentID, CourseID → Grade (full dependency - OK)
    • StudentID → StudentName (partial dependency - violation)
    • CourseID → CourseName (partial dependency - violation)

Since StudentName depends only on StudentID (a proper subset of the composite key), this is a partial dependency, so the table is NOT in 2NF.


Decomposition to achieve 2NF:

Table 1: Student

StudentIDStudentName
S01Ram
S02Sita

Table 2: Course

CourseIDCourseName
C01DBMS
C02OS

Table 3: Enrollment

StudentIDCourseIDGrade
S01C01A
S01C02B
S02C01A+

Now every non-prime attribute is fully dependent on the entire primary key. All three tables are in 2NF.


Summary

ConceptDescription
1NF requiredYes
Partial dependencyNot allowed
Full dependencyEvery non-prime attribute depends on the whole candidate key
Open this answer on its own page →

2078 · 5 marks

Define Functional dependencies. Explain trivial and non trivial dependencies? [5]

A functional dependency is a constraint between two sets of attributes in a relation. For a relation R, a functional dependency is denoted as: X → Y This means attribute (or set of attributes) X functionally determines attribute (or set of attributes) Y. In other words, for any two tuples t1 and ...

Open this answer on its own page →

Boyce-Codd Normal Form

2078 · 5 marks

Define Boyce-Codd normal form with example. How it is different that 3rd Normal form. [5]

Boyce-Codd Normal Form (BCNF) is a stronger version of the Third Normal Form (3NF). A relation R is in BCNF if and only if for every non-trivial functional dependency X → Y, X must be a superkey (i.e., X must be a candidate key or a superset of a candidate key). In simple terms: For every functio...

Open this answer on its own page →

Informal Design Guidelines for Relational Schemas

2080.1 · 10 marks

What are informal design guidelines for relational schemas? why do we need functional dependencies? Explain 2NF, 3NF with suitable example.[10]

--- Informal guidelines are used as measures to determine the quality of relation schema design. The four main guidelines are: --- Design a relation schema so that it is easy to explain its meaning. Do not combine attributes from multiple entity types into a single relation. Each tuple in a relat...

Open this answer on its own page →