Relational Database Model

Download Report

Transcript Relational Database Model

The Relational Database Model
– some relations you might want to avoid!!!
Our HERO!!!
2
Logical
vs.
Physical
• Relational Database
• Designer focuses on logical representation rather
than physical
• Use of table advantageous
• Structural and data independence
• Related records stored in independent tables
• Logical simplicity
• Allows for more effective design strategies
3
Logical View of Data
• Entities and Attributes
• Entity is a person, place, event, or thing
about which data is collected
• Attributes are characteristics of the entity
• Tables
• Holds related entities or entity set
• Also called relations
• Comprised of rows and columns
4
Table Characteristics
• Two-dimensional structure with rows and
columns
• Rows (tuples) represent single entity
• Columns represent attributes
• Row/column intersection represents single
value
• Tables must have an attribute to uniquely
identify each row
• Column values all have same data format
• Each column has range of values called
attribute domain
• Order of the rows and columns is
immaterial to the DBMS
5
Keys
• One or more attributes that
determine other attributes
• Key attribute
• Composite key
• Full functional dependence
• Entity integrity
• Uniqueness
• No ‘null’ value in key
6
Keys
Superkey
Uniquely identifies each entity
Candidate key
Minimal superkey
Primary key
Candidate key to uniquely identify all other
attributes in a given row – used to uniquely
identify each record
Secondary key
Used only for data retrieval
Foreign key
Values must match primary key in another
table
7
Example Tables
Figure 2.1
8
Comparison Operators …
A and B - Intersect
A OR B –UNION: all
of A (including yellow,
gray and purple) all of
B (including aqua,
and purple and gray)
C and B
9
Simple Relational
Database
Figure 2.2
10
Integrity Rules
• Entity integrity
• Ensures all entities are unique
• Each entity has unique key
• Referential integrity
• Foreign key must have null value or
match primary key values
• Makes it impossible to delete row whose
primary key has mandatory matching
foreign key values in another table
11
Relationships within
Relational Database
• Relationship classifications
• 1:1
• 1:M
• M:N
• E-R Model
• ERD Maps E-R model
• Chen
• Crow’s Feet
12
ERD Symbols
• Rectangles represent entities
• “1” side of relationship
Number 1 in Chen Model
Bar crossing line in Crow’s Feet Model
• “Many” relationships
– Letter “M” and “N” in Chen Model
– Three pronged “Crow’s foot” in Crow’s Feet
Model
13
Define Relationship
•
•
Determine relationship using this terminology: (i.e. relationship between
student and dorm rooms)
•
1 of A is related to X (1 or many) of B
•
i.e. 1 student is assigned to 1 dorm room
•
1 of B is related to X (1 or many) of A
•
i.e. 1 dorm room is assigned to many students
The decision will be as follows:
•
1:1
•
1 of A is related to 1 of B
•
1 of B is related to 1 of A
•
1:M
•
1 of A is related to many of B
•
1 of B is related to 1 of A
•
M:N
•
1 of A is related to many of B
•
1 of B is related to many of A
14
Relationship Resolution 1 to 1 (1:1)
• Assumed that the entity is just another attribute for
that table.
• Add entity as another attribute to existing table
15
Relationship Resolution 1 to Many
(1:M)
• The primary key of the one side is duplicated as
the foreign key on the many side.
• RULE!!!! foreign key ALWAYS goes on Many side.
• Names of the primary key and the foreign key do
not need to match - only the data type needs to be
the same.
• Of course, the values of the data stored in the
field must match as well or there can not be a
join.
16
Relationship Resolution M:N
1. Resolve the M:N relationship into two 1:M relationships
• Create an associative entity (AKA composite entity or bridge
entity) with primary keys (PK) of two entities as foreign keys
(FK)
• Associative entity is many side of both 1:M relationships.
• FK ALWAYS goes on many side of relationship ->
Associative entity ALWAYS many side of the
relationship
2. If combination of 2 FKs unique, can use as PK of the associative
entity.
• In this case, since PK be composed of 2 PKs, called
composite key.
3. If combination of 2 FKs NOT unique, leave 2 FKs in associative
entity. Create new PK for associative entity.
17
Comparison of Modeling
Techniques 1:1 relationship
Chen
Car
1
Infinity
Steering
Wheel
Car
1
Crow’s
Feet
Steering
1 Wheel
1
Car
1
1
Steering
Wheel
18
Comparison of Modeling
Techniques 1:M relationship
Chen
Car
1 M
Infinity
Car
1
Crow’s
Feet
Car
∞
Tire
Tire
Tire
1
19
Example 1:M Relationship
Figure 2.20
20
Comparison of Modeling
Techniques M:N relationship (yes it
seems it should be M:M but…)
Chen
Infinity
Crow’s
Feet
Class M
Class
Class
N Student
∞ ∞
Student
Student
21
Example M:N Relationship
Figure 2.24
22
Data Redundancy
Revisited
• Foreign keys can reduce redundancy
• Some redundancy is desirable
• Called controlled redundancy
• Speed
• Information requirements
23
Indexes
• Points to location
• Makes retrieval of data faster
Figure 2.31
24