Professional Documents
Culture Documents
SQL
CREATE TABLE Employee
(RUT CHAR(11),
name CHAR(20),
age INTEGER,
PRIMARY KEY (RUT));
Relationship Sets
⚫ Relationship sets are also translated to tables.
Keys for each participating entity set (as foreign keys).
All descriptive attributes of the relationship set.
ER Diagram Relational
Employee Department
RUT name age did dname budget
works_in
RUT did since
Relationship Sets
ER Diagram
SQL
Employee Department
RUT name age did dname budget
works_in manages
RUT did since did RUT since
Relational v2
Employee
⚫ 2 choices RUT name age
Map relationship set to a table
⚫ Separate tables for Employees and Departments. Department
⚫ Note that did is the key now! did dname budget Manager MangerSince
⚫ Choice 1
Map relationship set to a table
⚫ Separate tables for Employees and
Departments.
⚫ Note that did is the key now!
Key Constraints
Relational v2
Employee SQL
RUT name age
CREATE TABLE Department(
did INTEGER,
Department
dname CHAR(20),
did dname budget manager mangerSince budget REAL,
manager CHAR(11),
works_in mangerSince DATE,
RUT did since PRIMARY KEY (did),
FOREIGN KEY (manager) REFERENCES Employee)
⚫ Choice 2
Since each department has a unique
manager
Combine Manages and Departments!!
Participation Constraints
⚫ We can capture participation constraints (at least one relationships) involving one
entity set in a binary relationship, using NOT NULL.
⚫ In other cases, we need CHECK constraints.
Partial Key
Weak Entity Sets
⚫ Weak entity set and identifying relationship set
are translated into a single table.
When the owner entity is deleted, all owned
weak entities must also be deleted.
name CHAR(20),
age REAL,
Employee
PRIMARY KEY (employee, name),
RUT name email
FOREIGN KEY (employee) REFERENCES Employee
ON DELETE CASCADE);
Example
hourlyEmp contractEmp
RUT hourly_wages hours_worked RUT contractID
⚫ Business Rules
A Student can take many Courses
A Course can be taken by many
Students
A Student can complete many
Assessments
An Assessment must be completed
by at least one Student. He takes the
assesment on a given date and gets
a grade
A Course must have at least one
Assessment
An Assessment is for only one
Course
Walkthrough: From ER to RM
ER Diagram
Relational
Student
studentID firstName lastName Htel Wtel sex email
Assesment Course
AssNumber Description Weighting Code Fullname ShortName Description
Walkthrough: From ER to RM
⚫ Group together tables (formerly entities) and their relationships that have a
cardinality of “at most one” (thick lines)
Assesment
AssNumber Description Weighting Course
Course
0 Code Fullname ShortName Description
Student
studentID firstName lastName Htel Wtel sex email
Walkthrough: From ER to RM
⚫ Relationships whose cardinalities can be of more than one on both sides become new tables in
the new relational model.
primary keys from the two tables
involved in the relationship
become a composite primary key
in the new table
New table usually has a name
that is a combined form of the two
original table names
StudentCourse
studentID Code
Walkthrough: From ER to RM
⚫ Relationships whose cardinalities can be of more than one on both sides become new tables in
the new relational model.
StudentAssesment
studentID AssNumber DateGiven Grade
0
ER Diagram
Walkthrough
⚫ Final tables
⚫ Are they create in a specific
order?
Relational
StudentAssesment Assesment
studentID AssNumber DateGiven Grade AssNumber Description Weighting Course
StudentCourse Course
studentID Code Code Fullname ShortName Description
Student
studentID firstName lastName Htel Wtel sex email
Walkthrough: From RM to SQL
⚫ Create tables with no dependencies first
Relational
Student Course
studentID firstName lastName Htel Wtel sex email Code Fullname ShortName Description
SQL
SQL
Assesment
AssNumber Description Weighting Course
SQL
SQL
⚫ Can I have a Student that takes an Assesment of a Course in which he is not registered?