Week 3 PDF
Week 3 PDF
Week 3 PDF
week 3
Database Design
You are live with Gerald Macherechedze
Learning Objectives
1. Understand the concept of relationships in a relational database.
Database Design
You are live with Gerald Macherechedze
INTRODUCTION TO Relationship Types
•Relationships help you see how different parts of a system affect each other.
•For example, the entities STUDENT and COURSE are related to each other.
•To accurately model the business, the relationships between entities are as
Database Design
You are live with Gerald Macherechedze
Understanding Relationships:
•Represent something of significance or importance to the business
•Show how entities are related to each other
•Exist only between entities (or one entity and itself)
•Are bi-directional
•Are named at both ends
•Have optionality
•Have cardinality
Database Design
You are live with Gerald Macherechedze
User Relationship Types Summary:
One-to-One Relationship:
one record in Table2, and vice versa. This is achieved by having a unique foreign key
Database Design
You are live with Gerald Macherechedze
Syntax:
CREATE TABLE Table1 (
column1 INT PRIMARY KEY,
column2 VARCHAR(255),
-- Other columns
);
CREATE TABLE Table2 (
column3 INT PRIMARY KEY,
column4 VARCHAR(255),
foreign_key_column INT UNIQUE,
FOREIGN KEY (foreign_key_column) REFERENCES Table1(column1));
Example: Users and their corresponding User Profiles.
Database Design
You are live with Gerald Macherechedze
One-to-Many Relationship:
associated with multiple records in the child table, but each record in the child
table is associated with only one record in the parent table. This is established by
Database Design
You are live with Gerald Macherechedze
Syntax
CREATE TABLE ParentTable (
parent_id INT PRIMARY KEY,
parent_name VARCHAR(255),
-- Other columns
);
CREATE TABLE ChildTable (
child_id INT PRIMARY KEY,
child_name VARCHAR(255),
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES ParentTable(parent_id)
);
Example: One User having multiple Orders.
Database Design
You are live with Gerald Macherechedze
Many-to-Many Relationship:
multiple records in Table2, and vice versa. This is implemented using a junction
Database Design
You are live with Gerald Macherechedze
Syntax
CREATE TABLE Table1 (
table1_id INT PRIMARY KEY,
column1 VARCHAR(255),
-- Other columns
);
CREATE TABLE Table2 (
table2_id INT PRIMARY KEY,
column2 VARCHAR(255),
-- Other columns
);
CREATE TABLE JunctionTable (
table1_id INT,
table2_id INT,
PRIMARY KEY (table1_id, table2_id),
FOREIGN KEY (table1_id) REFERENCES Table1(table1_id),
FOREIGN KEY (table2_id) REFERENCES Table2(table2_id) );
Database Design
You are live with Gerald Macherechedze
PRACTICALS
Database Design
You are live with Gerald Macherechedze
SQL Queries Click Here
V. Conclusion
Adapt the provided syntax and examples based on the specific database system
and requirements.
Database Design
You are live with Gerald Macherechedze