R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to...
-
Upload
devin-aswell -
Category
Documents
-
view
220 -
download
1
Transcript of R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to...
![Page 1: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/1.jpg)
RELATIONAL MODEL TOSQLData Model
![Page 2: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/2.jpg)
22
CONCEPTUAL DESIGN:ER TO RELATIONAL TO SQL
How to represent Entity sets, Relationship sets, Attributes, Key and participation constraints, Subclasses, Weak entity sets. . . ?
![Page 3: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/3.jpg)
33
PROBLEM SOLVING STEPS Understand the business rules/requirements Draw the ER diagram Draw the Relational Model Write the SQL and create the database
![Page 4: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/4.jpg)
44
NOTATIONS
![Page 5: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/5.jpg)
55
CROW’S FEET
Entities Relationships
1-N 1-1 N-N
![Page 6: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/6.jpg)
66
ENTITY SETS Entity sets are translated to tables.
CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn));
Employees
ssnname
age
ER Diagram Relational
SQL
![Page 7: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/7.jpg)
77
RELATIONSHIP SETS Relationship sets are also translated to tables.
Keys for each participating entity set (as foreign keys).
The combination of these keys forms a superkey for the table.
All descriptive attributes of the relationship set.
ER Diagram
Relational
![Page 8: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/8.jpg)
88
RELATIONSHIP SETS
ER Diagram
Relational
CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments);
SQL
![Page 9: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/9.jpg)
99
KEY CONSTRAINTS Each dept has
at most one manager, according to the key constraint on Manages.
Translation to relational model?
many-to-manyone-to-one one-to-many many-to-one
dname
budgetdid
since
lot
name
ssn
ManagesEmployees Departments
![Page 10: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/10.jpg)
1010
KEY CONSTRAINTS
2 choices Map relationship set to a table
Separate tables for Employees and Departments. Note that did is the key now!
Since each department has a unique manager, we could instead combine Manages and Departments.
![Page 11: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/11.jpg)
1111
KEY CONSTRAINTS Choice 1
Map relationship set to a table Separate tables for Employees and
Departments. Note that did is the key now!
CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)
ER Diagram
Relational
SQL
![Page 12: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/12.jpg)
1212
KEY CONSTRAINTS Choice 2
Since each department has a unique manager
Combine Manages and Departments!!ER Diagram
SQL
since
CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)Relational
![Page 13: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/13.jpg)
1313
PARTICIPATION CONSTRAINTS
We can capture participation constraints involving one entity set in a binary relationship, using NOT NULL.
In other cases, we need CHECK constraints.
CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, manager CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (manager) REFERENCES Employees, ON DELETE NO ACTION)
![Page 14: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/14.jpg)
1414
WEAK ENTITY SETS A weak entity set can be identified uniquely only by
considering the primary key of another (owner) entity set. Owner entity set and weak entity set must participate in
a one-to-many relationship set (one owner, many weak entities). Weak entity has partial key. It’s primary key is made of
Its own partial key Primary key of Strong Entity
Weak entity set must have total participation in this identifying relationship set.
lot
name
agepname
DependentsEmployees
ssn
Policy
cost
Partial Key
![Page 15: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/15.jpg)
1515
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.
CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)
Employees
PK SSN
Name Age
Dep_Policy
PK,FK1 SSNPK PName
Age Cost
![Page 16: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/16.jpg)
1616
SUBCLASSES declare A ISA B
every A entity is also considered to be a B entity A is a specialization of B
Attributes of B are inherited to A. Overlap constraints
Can Joe be an Hourly_Emps as well as a Contract_Emps entity? depends
Covering constraints Does every Employees
entity either have to be an Hourly_Emps or a Contract_Emps entity? depends
![Page 17: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/17.jpg)
1717
SUBCLASSES One table for each of the
entity sets (superclass and subclasses).
ISA relationship does not require additional table.
All tables have the same key, i.e. the key of the superclass.
E.g.: One table each for Employees, Hourly_Emps and Contract_Emps. General employee
attributes are recorded in Employees
For hourly emps and contract emps, extra info recorded in the respective relations
Employees
PK ssn
name lot
Hourly_Emps
PK,FK1 ssn
hourly_wages hours_worked
Contract_Emps
PK,FK1 ssn
contractID
![Page 18: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/18.jpg)
1818
SUBCLASSES
Queries involving all employees easy, those involving just Hourly_Emps require a join to get their special attributes.
CREATE TABLE Hourly_Emps( ssn CHAR(11), hourly_wages REAL, hours_worked INTEGER, PRIMARY KEY (ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)
CREATE TABLE Employees( ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))
Employees
PK ssn
name lot
Hourly_Emps
PK,FK1 ssn
hourly_wages hours_worked
Contract_Emps
PK,FK1 ssn
contractID
![Page 19: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/19.jpg)
1919
SUBCLASSES Alternative translation
Create tables for the subclasses only. These tables have all attributes of the superclass(es) and the subclass.
This approach is applicable only if the subclasses cover the superclass.
Queries involving all employees difficult, those on Hourly_Emps and Contract_Emps alone are easy.
Only applicable, if Hourly_Emps AND Contract_Emps COVER Employees
![Page 20: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/20.jpg)
2020
BINARY VS. TERNARY RELATIONSHIPS
The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents.
Participation constraints lead to NOT NULL constraints.
CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)
CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER NOT NULL, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)
![Page 21: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/21.jpg)
2121
SUMMARY High-level design follows requirements
analysis and yields a high-level description of data to be stored.
ER model popular for high-level design. Constructs are expressive, close to the way
people think about their applications. Basic constructs: entities, relationships, and
attributes (of entities and relationships). Some additional constructs: weak entities,
subclasses, and constraints. ER design is subjective. There are often many
ways to model a given scenario! Analyzing alternatives can be tricky, especially for a large enterprise.
![Page 22: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/22.jpg)
2222
SUMMARY There are guidelines to translate ER diagrams
to a relational database schema. However, there are often alternatives that
need to be carefully considered. Entity sets and relationship sets are all
represented by relations. Some constructs of the ER model cannot be
easily translated, e.g. multiple participation constraints.
![Page 23: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/23.jpg)
2323
WALKTHROUGH 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 A Course must have at least one Assessment
An Assessment is for only one Course
![Page 24: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/24.jpg)
2424
WALKTHROUGH Want to track information about students
Student {StudentId, LastName, FirstName, Sex, Email, HTel, WTel}
Course {Code, ShortName, FullName, Description}
Assessment {AssessmentNo, Description, Weighting}
![Page 25: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/25.jpg)
2525
WALKTHROUGH 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 A Course must have at least one Assessment An Assessment is for only one Course
0:N 0:N
0:N
1:N
1:N
1:1
![Page 26: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/26.jpg)
2626
WALKTHROUGH
0:N 0:N
0:N
1:N
1:N
1:1
ER Diagram
Relational
![Page 27: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/27.jpg)
2727
WALKTHROUGH
Group together tables (formerly entities) and their relationships that have a cardinality of 0:1 or 1:1
![Page 28: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/28.jpg)
2828
WALKTHROUGH
The remaining relationships whose cardinalities are N (1 :N or 0:N) on both sides become new tables in the new relational model.
![Page 29: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/29.jpg)
2929
WALKTHROUGH
remaining relationships whose cardinalities are 1:N or 0:N 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
![Page 30: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/30.jpg)
3030
WALKTHROUGH
Relational
ER Diagram
Final tables Create in specific
order?
![Page 31: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/31.jpg)
3131
WALKTHROUGH Final tables Create entities with
no dependencies first
Relational
SQL
CREATE TABLE Student ( StudentID BIGINT, LastName VARCHAR(100), FirstName VARCHAR(100), Sex CHAR(1), EMail VARCHAR(100), HTel VARCHAR(20), WTel VARCHAR(20), PRIMARY KEY (StudentID) );
![Page 32: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/32.jpg)
3232
WALKTHROUGH Final tables Create entities with
no dependencies first
Relational
SQL
CREATE TABLE Course( Code VARCHAR(20), ShortName VARCHAR(100), FullName VARCHAR(100), Description VARCHAR(8000), PRIMARY KEY (Code) );
![Page 33: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/33.jpg)
3333
WALKTHROUGH Final tables Create tables
dependent on entities. Can we create
StudentsAssessments?
Relational
![Page 34: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/34.jpg)
3434
WALKTHROUGH Final tables
Relational
SQL
CREATE TABLE StudentsCourses( Code VARCHAR(20), StudentID BIGINT,PRIMARY KEY (Code, StudentID), FOREIGN KEY (Code) REFERENCES Course, FOREIGN KEY (StudentID) REFERENCES Student);
Data types must be identical in all tables referencing the same field!
![Page 35: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/35.jpg)
3535
WALKTHROUGH Final tables
Relational
SQL
CREATE TABLE Assessment( AssessmentNo INTEGER, Code VARCHAR(20), Weighting DECIMAL(4,2), Description VARCHAR(100), PRIMARY KEY (AssessmentNo), FOREIGN KEY (AssessmentNo) REFERENCES Assessment);
![Page 36: R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.](https://reader036.fdocuments.in/reader036/viewer/2022062307/5519b3365503466f578b46a6/html5/thumbnails/36.jpg)
3636
WALKTHROUGH Final tables
Relational
SQL
CREATE TABLE StudentsAssessments( AssessmentNo INTEGER, StudentID BIGINT, DateGive DATE, Grade DECIMAL(4,2), PRIMARY KEY (AssessmentNo , StudentID), FOREIGN KEY (AssessmentNo) REFERENCES Assessment, FOREIGN KEY (StudentID) REFERENCES Student);