5-rel-db-design-05-converting-slides
5-rel-db-design-05-converting-slides
Conversions
ER diagram to relational
Relational to ER diagram
Why convert from ER to relational?
Inherited database
No documentation
Outdated documentation
Ideal?
Initial model (ER)
Normalize (ER)
Convert (Relational)
Practical?
Initial model (ER)
Convert (Relational)
Normalize (Relational)
Convert changes back (ER)
What to convert?
Relational representation
Physical model
Optimized for performance
Vendor-specific tweaks
Logical model
Compact form
Benefits
Compact
Quick to create
Downside Optional? Reference?
Lack of detail
DDL
All details included
Not accessible
Representing a relational database design
Graphical
Tables
Representing a relational database design
Graphical
Tables
Relationships
Representing a relational database design
Graphical
Tables
Relationships
Keys
Optionality
Data types
Representing a relational database design
Graphical
Notation similar to ER diagrams
Easy to understand
Blurs distinction between
design and implementation
Leads to implementation
choices in design
Tables
Relationships
Keys
Optionality
Data types
ER to relational: Entity types
Entity type
Converted to a relational table
Same name or different name?
ER naming convention
Singular (e.g. Person)
Entity type
Converted to a relational table
Same name or different name?
Same name
Easier mapping between representations
Plural entity type names make the ER diagram look wrong
(And the relationship readings sound weird)
Singular table names introduce wrong mindset
Encourage one-row-at-a-time coding instead of set-based
Queries sound better with plural table names
Less chance of hitting a reserved word
ER to relational: Entity types
Entity type
Converted to a relational table
One column for every attribute
Column name is (almost always) equal to attribute name
Optional = nullable; mandatory = NOT NULL
Add data type
Implementation independent (e.g. “character” / “numeric”)
Optionally shortened
C = character data
N = numeric data
D = date/time data
Full details in appendix
Range
Precision
Maximum length
…
ER to relational: Candidate keys
Candidate key
Enforced by constraint
Primary key PRIMARY KEY constraint
Required by Codd’s 2nd rule
At most one
Alternate key UNIQUE constraint
Player
B oard position
Player
V olunteer
Player number (FK)
Primary duty
ER to relational: One-to-many relationships
Me mbership term Fina ncial transaction
Year Bank statement number
One-to-many relationship Player number (FK) is p aid with /
is p ayment for
Line number
Paid? Date
FOREIGN KEY constraint Amount
MembShipTerm_Year (FK)
Referenced table MembShipTerm_Player (FK)
MembershipTerms
YearNbr PlayerNbr Paid
N N C Referencing columns
FinancialTransactions
StatementNbr LineNbr TranDate Amount MembShipTerm_Year MembShipTerm_Player
N N D N N opt N opt
ER to relational: One-to-many relationships
One-to-many relationship
FOREIGN KEY constraint
Foreign key attributes represented in ER diagram?
Just add the FOREIGN KEY constraint
Using ER method that leaves out the foreign key attributes?
Add referencing columns first, then add FOREIGN KEY constraint
Choose one of the candidate keys in the referenced table
Default to PRIMARY KEY
ER to relational: One-to-many relationships
One-to-one relationship
FOREIGN KEY constraint
Choice of parent (referenced) and child (referencing) table:
Copy from ER diagram, if choice was made there
Choose now otherwise
CHOICE CAN BE CHANGED LATER (physical model)
Subtype relationship
FOREIGN KEY constraint
Always subtype = referencing table / supertype = referenced table
Appendix for additional information
Discriminator
Complete / incomplete subtype relationship
Mutual exclusive subtype relationships
ER to relational: One-to-many relationships
Employees Salary
Projects
ProjectCode DateStarted DateDue
N D D Opt
Employees Salary
Projects
ProjectCode DateStarted DateDue
N D D Opt
EmployeesProjects
ProjectCode EmployeeName
N C
ER to relational: Many-to-many relationships
Employees Salary
Projects
ProjectCode DateStarted DateDue
N D D Opt
EmployeesWorkOn
ProjectCode EmployeeName
N C
ER to relational: Many-to-many relationships
Employees Salary
Projects
ProjectCode DateStarted DateDue
N D D Opt
ProjectStaffing
ProjectCode EmployeeName
N C
ER to relational: Many-to-many relationships
Many-to-many relationship
Implemented as extra table
Junction table
Linking table
Cross-reference table
Join table
…
Not different from other tables!
ER to relational: Many-to-many relationships
CHECK constraint
Logical expression
May never evaluate to FALSE for any row
Can include one column, or several column
Cannot reference other tables
Cannot reference other rows in the same table
ER to relational: Other constraints
Generated column
What if only a single value is allowed?
Database can compute it for you
Generated column (aka derived column / computed column)
No access to other tables or other rows in same table
Assertion
Similar to CHECK constraint
Does allow access to other rows / other tables
Not supported by many RDBMS vendors
Can be used as a way to specify logic for database developers
ER to relational: Other constraints
DEFAULT constraint
Not really a constraint
Provides standard value to use if no value specified
Not the same as a generated column!
DEFAULT used only for new rows
DEFAULT can be overridden or changed
DEFAULT cannot reference any other columns or contain logic
Constant values
Built-in functions
ER to relational: Other constraints
CHECK constraint
Generated column
Assertion
DEFAULT constraint
Found in appendix
Not represented in ER diagram
Calendar ER to relational: demo c annot play on /
Day
Date
is blocked by
Day of week
is for /
is played on
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
League code (FK) is played by P Frames won pl 1
P
Frames won pl 2
is of /
p lays in
Leagues League
League code
Frames to play
CompetitionMatches CupMatches
Calendar ER to relational: demo c annot play on /
Day
Date
Date DayOfWeek is blocked by
Day of week
D C
is for /
is played on
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
LeagueCode FramesToPlay Frames to play
C N
Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
CompetitionMatches CupMatches
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
LeagueCode FramesToPlay Frames to play
C N
Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
CompetitionMatches CupMatches
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
LeagueCode FramesToPlay Frames to play
C N
Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
CompetitionMatches CupMatches
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
LeagueCode FramesToPlay Frames to play
C N
Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
CompetitionMatches CupMatches
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
LeagueCode FramesToPlay Frames to play
C N
Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
CompetitionMatches CupMatches
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
LeagueCode FramesToPlay Frames to play
C N
Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
CompetitionMatches CupMatches
Player Match
plays in /
Players Player number is played by P
Player 1 (FK)
Player 2 (FK)
Player name (AK1.1) plays in / Date (FK)
PlayerNbr PlayerName LeagueCode League code (FK) is played by P Frames won pl 1
N C C opt P
Frames won pl 2
is of /
p lays in
Leagues League
League code
Both empty
LeagueCode FramesToPlay or Frames to play
C N
both filled Co mp etition match Cup match
Player 1 (FK) Player 1 (FK)
c onsists of / Player 2 (FK) Player 2 (FK)
Matches is played for P Date (FK) Date (FK)
League code (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mp etitionMatch CupMatch
Date Date
Player1 Player1
Matches Player2 Player2
LeagueCode Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mp etitionMatch CupMatch
Date Date
Player1 Player1
Matches Player2 Player2
LeagueCode Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mp etitionMatch CupMatch
Date Date
Player1 Player1
Matches Player2 Player2
LeagueCode Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mp etitionMatch CupMatch
Date Date
Player1 Player1
Matches Player2 Player2
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mp etitionMatch CupMatch
Date Date
Player1 Player1
Matches Player2 Player2
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mp etitionMatch CupMatch
Date Date
Player1 Player1
Matches Player2 Player2
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mpetitionMatch CupMatch
Date Date (FK)
Player1 Player1 (FK)
Matches Player2 Player2 (FK)
LeagueCode (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mpetitionMatch CupMatch
Player1 (FK) Date (FK)
Player2 (FK) Player1 (FK)
Matches Date (FK) Player2 (FK)
LeagueCode (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mpetitionMatch CupMatch
Player1 (FK) Date (FK)
Player2 (FK) Player1 (FK)
Matches Date (FK) Player2 (FK)
LeagueCode (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches
Leagues League
LeagueCode
Both empty
LeagueCode FramesToPlay or FramesToPlay
C N
both filled Co mpetitionMatch CupMatch
Player1 (FK) Date (FK)
Player2 (FK) Player1 (FK)
Matches Date (FK) Player2 (FK)
LeagueCode (FK) Round
Player1 Player2 MatchDate Frames1 Frames2
N N D N opt N opt
R1,R2,QF,SF,Fin
CompetitionMatches CupMatches