Module 2
Module 2
Data models are fundamental to database design. They provide a structured way
to represent the real-world entities, their attributes, and relationships, which will
be stored in the database. Think of it as a blueprint for your database.
There are several types of data models, each with its own strengths and
weaknesses. We'll focus on the Entity-Relationship (ER) model, a widely used
conceptual model.
The ER model helps us understand and organize data by breaking it down into
three key components:
• Entities: These are the "things" we want to store information about. Think
of them as nouns. Examples include:
While the core concepts of the Entity-Relationship (ER) model remain consistent,
there are different notations used to represent these concepts visually in ER
diagrams. Three popular notations are Chen, Crow's Foot, and UML.
1. Chen Notation
• Developed by: Peter Chen in 1976 (one of the pioneers of the ER model).
• Symbols:
o Cardinality: Uses 1 for one and only one, and 'm' or 'n' for many.
• Symbols:
• Symbols:
Example ER Diagram:
draw((8,2)--(12,2)--(12,0)--(8,0)--cycle); label("Course",(10,1));
draw((9,2.5)..(10,3)..(11,2.5)); label("CourseID",(10,3.2));
draw((9,1.5)..(10,1)..(11,1.5)); label("CourseName",(10,1.2));
draw((9,0.5)..(10,0)..(11,0.5)); label("Credits",(10,0.2));
This diagram shows two entities, "Student" and "Course," with their respective
attributes. The diamond "enrolls in" represents the relationship between them.
1. Tables:
• Structure: Data is organized into two-dimensional tables with rows and
columns.
Example Table:
Export to Sheets
2. Keys:
Keys are special attributes that play a crucial role in identifying and linking data
within and across tables.
• Primary Key:
• Foreign Key:
o Referential Integrity: It creates a link between two tables by
referencing the primary key of another table.
3. Relationships:
Relationships in the relational model are established through foreign keys. This
allows you to connect related data across multiple tables.
Example Relationship:
Student Table:
2 Bob Biology
3 Carol History
Export to Sheets
Enrollment Table:
2 2 BIO201 B
3 1 MATH101 A-
Export to Sheets
• Data Integrity: Mechanisms like primary and foreign keys help enforce
data consistency and accuracy.
By understanding tables, keys, and relationships in the relational model, you can
effectively design and implement databases that are efficient, scalable, and
maintain data integrity.
Normal forms are a set of rules that guide the normalization process. Each normal
form builds upon the previous one, addressing specific types of redundancy and
data anomalies.
• Atomic Values: To achieve 1NF, ensure that each column contains only
atomic values (single, indivisible units of data).
• Create Separate Tables: If you have repeating groups, create new tables
to store those repeating values and link them to the main table using foreign
keys.
Example:
Unnormalized Table:
StudentID Name PhoneNumbers
2 Bob 555-9012
Export to Sheets
1NF Table:
Student Table:
StudentID Name
1 Alice
2 Bob
Export to Sheets
PhoneNumbers Table:
1 1 555-1234
2 1 555-5678
3 2 555-9012
Export to Sheets
Example:
1 A1 Laptop 2
2 B2 Mouse 5
Export to Sheets
2NF Tables:
Order Table:
1 A1 2
2 B2 5
Export to Sheets
Product Table:
ProductID ProductName
A1 Laptop
B2 Mouse
Export to Sheets
Example:
Export to Sheets
3NF Tables:
Employee Table:
1 Alice Sales
2 Bob Marketing
Export to Sheets
Department Table:
Department DepartmentLocation
Sales London
Marketing New York
Export to Sheets
By applying these normal forms, you can create a well-structured database that is
efficient, consistent, and easier to manage. Keep in mind that while higher normal
forms generally lead to better database design, there might be situations where
denormalization (intentionally introducing some redundancy) is beneficial for
performance reasons. However, this should be done cautiously and with a clear
understanding of the trade-offs.
• Analyze the Data: Begin by examining the data you need to store. Identify
the key entities (e.g., customers, products, orders) and their associated
attributes (e.g., customer name, product price, order date).
• Create an Initial Table: Often, you'll start with a single table containing
all the data. This is usually in an unnormalized form.
• Create New Tables: For each repeating group, create a new table to store
those values.
• Establish Relationships: Use foreign keys to link the new tables to the
original table.
Step 3: Second Normal Form (2NF)
• Check for Composite Keys: If your tables have composite primary keys
(keys consisting of multiple columns), check for partial dependencies.
• Create New Tables: Create new tables to store the attributes that are
partially dependent on the primary key.
Example:
Unnormalized Table:
Export to Sheets
1NF:
• We notice that ProductID and ProductName repeat for the same OrderID.
Order Table:
Export to Sheets
Product Table:
ProductID ProductName
A1 Laptop
B2 Mouse
Export to Sheets
2NF:
1 C1 A1 2
1 C1 B2 1
2 C2 A1 1
Export to Sheets
Customer Table:
Export to Sheets
3NF:
This step-by-step process helps you systematically refine your database design to
achieve higher normal forms, leading to a more efficient and robust database.