软件学院软件学院
Yunsheng Liu
2008.10
Yunsheng Liu
2008.10
Yubsheng-DBS 2
3.1 Introduction3.1 Introduction
1. Concepts
- A DM( data model) is an abstract representation to describe (the data of ) a DB in mathematics
2. The elements of a DM: a set of notions with their notations for static
structure a set of operations for dynamic behavior a set of constraints for the structures and behaviors
3. The spectrum of DMs
Yubsheng-DBS 3
Relational DM Logical DMLogical DM
Value-Oriented Model
Ex-RDMEx-RDM
Functional DM
E-R Model
Semantic DMSemantic DM
Permanent-Language OMPermanent-Language OM Query Language OMQuery Language OM
Object Model
Object-Oriented Model
Hierarchical DM
Network DM
Record-Oriented Model
Yubsheng-DBS 4
3.2 The Entity-Relationship Model
3.2 The Entity-Relationship Model
3.2.1 Entity and Entity Set3.2.1 Entity and Entity Set1. Entity concept - An entity is a thing/object in the RW which exists and is
distinguishable from all others — can be identified
2. Entity properties/characteristics - Describing an entity — called Attributes3. Entity type vs. instances - An abstract representation of entities with the
same name and the same set of attributes e.g.: Student(S#, Sname, Sex, Birthday, Dept)
Yubsheng-DBS 5
4. Entity set - A group consisting of all “similar” entities of the
same type that possesses of the same name and attributes
- Entity sets need not be disjoint.5. Examples:
3.2.1 Entity and Entity Set3.2.1 Entity and Entity Set
Entity Entity set A student — All students An airline — All airlines A course — All courses
Yubsheng-DBS 6
……………
Math19931226MWang Z.200010123
CST19970206FLi Y.200515006
SW19981020MZhang X.200610001
DeptBirthdaySexS-nameS#Student
Entity type
An Entity
Entity set
C# C-name Term Dept
SW402 DBS. Fall SW
CS501 ADBT Spring CST
SW302 OS. Fall SW
… … … …
Course
3.2.1 Entity and Entity Set3.2.1 Entity and Entity Set
Yubsheng-DBS 7
3.2.2 Attributes and Domains3.2.2 Attributes and Domains
1. ConceptsProperties or characteristics used to represent
an entityType—(attr-name, attri-type)Value—data value
2. Purposes DescriptionIdentification—Key
Single- and multi-valus Null
— elementary units to form an entity
Yubsheng-DBS 8
3. Simple vs. composite attributes
4. Derived attributes
5. Key — a set of attributes to uniquely identify an entity — key attributes
6. Domain—a set of all the values that an attribute may take
3.2.2 Attributes and Domains3.2.2 Attributes and Domains
Any value in a domain must be elementary
Yubsheng-DBS 9
3.2.3 Relationship and Relationship Set3.2.3 Relationship and Relationship Set
1. Relationship concepts An relationship is an association among two or
more entities. 2-ary relationships vs. n-ary relationships—degree An entity may participate in different kinds of
relationships and also in different relationships of the same type
2. Relationship characteristics - Attributes 3. Relationship type vs. instances - It is similar to that of entities
Yubsheng-DBS 10
4. Relationship set
- A set of relationships of the same type
5. Multiple relationships and multi-value relationships
There exist more than one relationship type/set between two entity sets
There exist more than one relationships between two entities in the same set
e.g.
3.2.3 Relationship and Relationship Set3.2.3 Relationship and Relationship Set
Multiple relationships
Multi-value relationship
Yubsheng-DBS 11
6. Degree—the number of entity set associated in a Relationship type. e.g. 2-ary, 3-ary, n-ary
n-ary A set of 2-ary relationships
1 : 1 1 : MN : M
College : DeanDept : StudentStudent : Course
e.g.
3.2.3 Relationship and Relationship Set3.2.3 Relationship and Relationship Set
7. Cardinality—the numbers of entities in variant entity sets appeared in the same relationship
Yubsheng-DBS 12
3.2.4 Identification3.2.4 Identification
1. Identifying entities
Let X is a key of entity set E, then any attribute set X’X can also uniquely identify E. X’ is called a superkey—an attribute set covered a key
Key—the smallest supper key
Supperkey
There may exist more than one key for an entity set. All of them is referred as candidate keys
The chosen one of candidate keys is a primary key
Candidate versus Primary key
Key— an attribute set uniquely identifying single Es
Yubsheng-DBS 13
Whether KA ?? where K= a key attribute set and A= the set of all
the attributes for an entity set
(customer#, restaurant#, time, food) Non-key— K∩A K - Weak entity
Total key — K=A Please give an example. Could you ?
3.2.4 Identification3.2.4 Identification
Primary attributes vs. Non-primary attributes -Attribute in a key vs. attributes not in any key
Yubsheng-DBS 14
2. Identifying relationships
- Let R be a relationship set among entity sets E1, …,En
and PKi be the primary key of Ei (i=1,2,..,n). Rule: If R is a single-value relationship, then PK = PKi
If R is a multi-value relationship, R must has its own attributes, saying Ai (i=1,…,m), then
PK = PKi Ai
i =1
n
i =1
m
i =1
n
Examples: G-record( Student, Course, Grade) C-Schedule( Course, Teacher, Time, Room)
3.2.4 Identification3.2.4 Identification
Yubsheng-DBS 15
3.2.5 E-R Diagram3.2.5 E-R Diagram
1. Graphic elements
E-name Entity type/set
MN Cardinality of a relationship
Relationship type/setR-name
Attribute typeA-name
xy Key attributes—plus underscore “ ___”
Yubsheng-DBS 16
2. Examples
Grade
Student Hold DeptS#
Sname
Sex Dept
D#
DnamePhoneLoca
1M
Student Enroll CourseS#
Sname
Sex Dept
C#
CnameDeptDate
N M
Locus
3.2.5 E-R Diagram3.2.5 E-R Diagram
Yubsheng-DBS 17
D-name
E-R Diagram of a University DB
ProjectLab
Course
Teacher Student
L-P
LecturingChairing
Advising
Holding
Dividing
Studying
Working
Managing Researching
Holding
M N
N1
M N
N1
M
N
N
1
1
N
1
1
P
N
Main-P Sub-P
1
C-name
C#
S-name
S#
Sex
Bir-dayN
1
Address
Phone
Position
Time
T-name
Phone
Interests
M N
L-name
Address
Phone
P-name
Pro#
S-E time
Budget
T#
Dept
Dependents
family
3.2.6 Extented Features of The E-R Model
Yubsheng-DBS 18
3.2.6 Extended Features of The E-R Model3.2.6 Extended Features of The E-R Model
1. Weak entities/Identifying relationship
Example
Employee Dependentsfamily
They cann’t identify an entity by itselfes. And the identifications have to depend also on the primary attributes of another entity set, called owner entity set.
Identifier relationship ◆ Those between a weak entity and its owner entity ◆ represented by point to the weak entity
Yubsheng-DBS 19
Class hierarchies of entities in an entity set
2. Generalization/Specification relationships
StudentUndergre
GreduateMS_Stud
PhD_Cand
Generalization
Specification
Attributes inheritance
ISA realationship— that between classes at adjacent hierarchies, notated by in E-R model
ISA
ISAIS
A
3.2.6 Extended Features of The E-R Model
3.2.6 Extended Features of The E-R Model
Yubsheng-DBS 20
?
Monitoring
3. Aggregation
Relationship among relationships ?
Student CourseTesting
Monitor
Aggregation is an abstraction that considers an association of several objects as an object at a higher level
3.2.6 Extended Features of The E-R Model
3.2.6 Extended Features of The E-R Model
Yubsheng-DBS 21
3.3 Relational Data Model3.3 Relational Data Model
3.3.1 Introduction3.3.1 Introduction
E.F.Codd, 1970, inaugurating a new age of DB Special notions and terminology for structures,
operations and constraints Powerful mathematical fundamental, Criteria for DB implementation A DB is a collection of relations
Yubsheng-DBS 22
Relation head vs. bodyrelation schema vs.relation type vs. occurrence
A relation schema is a formal description of the logical structure of a relation, like Rname(A1, A2, …, An)
Specifying the rel. name, att. names and domains
Invariable over time
n is called the degree or arity of the relation A relation, called also a table, is a named set of tuples
3.3.2 Terminology— Basic Concepts3.3.2 Terminology— Basic Concepts
1. Relation Entity set
Yubsheng-DBS 23
As entities, an A is a named item, referred to a D A D is a named set of values in the same type Type—pair (Ai : Di) vs.values— aiDi
An A is an atomic element of a relation structureEach is named uniquely in a relation: ij (Ai Aj )
Each Ai takes values from a single domain Di
Multiple attributes may refer to the same domain, i.e. there may exist ij and Di = Dj
2. Attributes(A) and Domains(D)
3.3.2 Terminology— Basic Concepts3.3.2 Terminology— Basic Concepts
Yubsheng-DBS 24
4. Graphical show of a relation—Table
3. Tuple Entity
A tuple, called also a row, is an aggregation of the attribute values( one from each attribute).
A list of the attribute values, like (a1, a2,…,an) where
aiDi (i=1,2,…,n), called a component of the tuple
(a1, a2,…,an) is called an n-tuple
Any tuple in a relation is unique
3.3.2 Terminology— Basic Concepts3.3.2 Terminology— Basic Concepts
Yubsheng-DBS 25
Student
……………Math19931226MWang Z.200010123
CST19970206FLi Y.200515006
SW19981020MZhang X.200610001
DeptBirthdaySexS-nameS#
Relation schema
Attributes/Collumns
Tuples/Rows
Relation
Attribute values / tuple components
dateM,FM,F String of
digits
D1 String of Chars.
D2 String of Chars.
Dn
Domains
A relation’s tabular show A relation’s tabular show
Attribute names
Relation name
Yubsheng-DBS 26
3.3.3 The General Characteristics of RDB3.3.3 The General Characteristics of RDB
1. All the values in each domain are atomic2. Every attribute is referred to a single domain, but3. A domain may be concerned in different attributes4. Each tuple is uniquely identified ? Key
uniqueness
A DB consists of just relations. How about relationships ?
5. There is no constraint on the number and sequence of tuples in a relation
6. There is no constraint on the number and sequence of attributes in a relation. But ?
7. The schema/head of a relation is fixed8. The body of a relation is variable
Yubsheng-DBS 27
3.3.4 Integrity Constraints3.3.4 Integrity Constraints
1. Keys of a relation A smallest non-empty set of attributes that can
uniquely identify a tuple UniquenessSmallest
Why ?
2. Foreign key of a relation Why ?The relation’s attribute(s) which forms the primary
key of another relation Example: Student(S#, Sname, Sex, Birthday, Dept#)
for relationships between entities
Yubsheng-DBS 28
Reference Integrity:
If R1 holds a foreign key fk which is the primary key of R2 , then any value of fk in R1 is either the primary key value of some tuple in R2 or all Null
3. The Integrity Rules
Why? Could you give an explanation ?
3.3.4 Integrity Constraints3.3.4 Integrity Constraints
Entity Integrity: Any primary(key) attribute value cann’t be Null ?
Yubsheng-DBS 29
3.3.5 Relational Algebra3.3.5 Relational Algebra
Two kinds of notations used for expressing operations on relations:
Algebra notations— Relational algebra
Logical notations— Relational calculusTwo kinds of operations used to form relational
algebra
Conventional set operations
Native relational operations
Yubsheng-DBS 30
Let R1 and R2 be k- and s-arity relations, respectively. Then R3 is a relation consisting of all possible (k+s)-tuples whose first k components form a tupe in R1 and whose last s components form a tuple in R2
1. Basic Relational Algebra Operations - 5 basic operations serving to define relational algebra
(1). Union(U): R1∪R2= R3={t |tR1∨tR2}
(2). Difference(–): R1–R2= R3={t |tR1∧tR2} (3). Cartesian product():
R1R2 =R3 ={t1 t2 | t1R1∧t2R2} R3 is of ?-tuple
3.3.5 Relational Algebra3.3.5 Relational Algebra
Yubsheng-DBS 31
Examples:
R: A B C S: D E F a b d c e b a d b d b e
3.3.5 Relational Algebra3.3.5 Relational Algebra
What is the R×S ?
R×S = T: A B C D E F a b d c e b a b d d b e a d b c e b a d b d b e
Yubsheng-DBS 32
- Native relational operations
(4). Selection(): c(R)={t |tR∧c(t) =.T.}
Where c is a condition expression formed by Operands: constant, attribute names or position
numbers ($-) The arithmetic comparison operators: , ,=,≠.≤,≥, The logical operators: , ,
e.g. Dept =’ CS’ Age≤18∧ ( Students)$2 >5 $3≤$1∨ (R)
3.3.5 Relational Algebra3.3.5 Relational Algebra
Yubsheng-DBS 33
A is a list of names or position numbers of the retained attributes, like Ai1
,Ai2,…,Aim
or i1, i2 , …,im
t[A] is a tuple formed by the components of t on AOpposite to ‘SELECT’ being horizontally extracting
tuples, ‘PROJECTION’ is vertically extracting attributes
The duplicate tuples must be removed from the result
(5). Projection(): A(R)={t[A] |tR}
e.g. s-name, sex (Student), 2, 4 (Student)
3.3.5 Relational Algebra3.3.5 Relational Algebra
Yubsheng-DBS 34
Exp is a relational algebra expression. returns the result of Exp, a relation, and assigns
the name R to the result relation and renames the attributes according to the renaming list NList
NList is either a list of items as the following, or a null
oldname→newname or position number→newname
(6). Renaming(): (R(NList), Exp )
3.3.5 Relational Algebra3.3.5 Relational Algebra
2. Additional Relational Algebra Operations
Yubsheng-DBS 35
Examples:
⑴. (S, Student)
⑵. (R3, R1R2 )
⑶. ( Slist(S-name→Name, Dept→Dname),
s-name, dept (Student) )
3.3.5 Relational Algebra3.3.5 Relational Algebra
Yubsheng-DBS 36
(7). Intersection(∩): R1∩R2 = R3={t |tR1∧tR2} R1∩R2 = R1 – (R1 – R2)
(8). Join(⋈) -join (Condition join, ⋈c): R1 ⋈c R2={<t1t2>| t1R1∧t2R2∧c[t1t2]=.T.} c is a condition expression in the form of A B A and B are names or position numbers of some
comparable attributes of R1 and R2, respectively. is a arithmetic comparison operator, such as
< ,≤… R1 ⋈c R2 = c (R1R2 )
3.3.5 Relational Algebra3.3.5 Relational Algebra
Yubsheng-DBS 37
Equijoin — the special case of -join where is = R1 ⋈A=B R2 = A=B (R1R2 ) Natural join — the further special case of the equijoin
on common attributes. R ⋈ S =R.A∪S.A - Ac(R.Acom = S.Acom
(RS ))
R.A, S.A are the attribute sets of R and S, respectively, and Acom is their common attribute set
There must exist a common attribute set between the two relations
The duplicate attributes are removed in the result
3.3.5 Relational Algebra3.3.5 Relational Algebra
Yubsheng-DBS 38
3. Composite Operations e.g.:s-name,sex(Dept = CS(Student)) 2,4(3>5(R S)) ∪ 3>5 (2,4 (R S)) ∪ ?
3.3.5 Relational Algebra3.3.5 Relational Algebra
(9). Division(÷)
Let R.A=(X,Y), i.e. the attribute set of R, S.A=(Y), Y and Y are two comparable attribute sets.
R÷S={t|tR[X] (∧ tsS (∃trR ( tr[X]=t t∧ r[Y]=ts[Y’])))}
Where we can get that S.A R.A
Yubsheng-DBS 39
3.4 Network Data Model3.4 Network Data Model
3.4.1 Network Data Objects 3.4.1 Network Data Objects 1. Record type—a named ordered set of data items/fields
Record type Relation types/Entity type
2. Field—a basic named data unit forming a record type
Field (data item) attributes, but
it can be a composite of other fields or a repeated item
3. Link—a relation between two(only two) record types
Link 2-ary relationship in E-R
1:M(all), Ring(comp.), Cycle(all), N:M(comp.),
No n-ary rela.
Yubsheng-DBS 40
3.4.2 Bachman Diagram3.4.2 Bachman Diagram
F-name1…F-namen
Record type LinkL-nameParent
recordChild record
Dept# Dname Add Phone
Dept# Dname Add Phone
OnameAddPhone
OnameAddPhone
LnameAddPhone
LnameAddPhone
C#C#nameTerm
C#C#nameTerm
T#TnamePhone
T#TnamePhone
S#SnameSex
S#SnameSex
Dept
Office Lab Course
Student Teacher
L1
Li
4. Data structure diagram
Yubsheng-DBS 41
3.4.3 Network Structure3.4.3 Network Structure
1. Multiple 1:M relationships all
2. M:N relationshipcomplex net only
3. Ring relationships complex net only
4. Circle relationships not in restricted net
5. N-ary relationships No, creating a relationship- record type and multiple 1:M rel. to each rec. type
6. Class relationship not in restricted net
Yubsheng-DBS 42
3.4.4 DBTG Model3.4.4 DBTG Model
1. CODASYL (Conference on Data System Language ), DBTG (Data Base Task Group) Reports (1971,…,1984…)
2. Simple Network— only 1:M links3. Set ( 系 ) structure A two-level structure consisting of owner record type
and members record types— a link between records Each set must be named, Each set has one and only one owner, and may have
zero, one or more members Any record type may be the owner of a set type and
simultaneously a member of another set type ?
Yubsheng-DBS 43
4. Set type vs. Set occurrences Similar to a relation A set type: SetName(Owner-spec, Member-spec) A set occurrence: a set of an owner and the members
associated with the owner under the set type
3.4.4 DBTG Model3.4.4 DBTG Model
Dept DeptPeople
TeacherTeacherTeacherTeacherStudentStudent
5. Examples:
CourseCourseLecture
Yubsheng-DBS 44
3.5 Hierarchical Data Model3.5 Hierarchical Data Model
IBM,1968, used in they first DBMS IMSThe earliest data model, a special case of the NM
1. Basic Concepts Record type, Field/item, Link— the same as NM DB—forest, a set of trees 2. Hierarchical Data Structures—tree There must be one node without a parent, called a root Any node (except root)must have one and only one
parent. A node may have 0 (leaf), 1 or more children,
Yubsheng-DBS 45
Combined record— virtual record with regular fields, such as ‘Grade’ in the above example
3. Representing M:N relationships with virtual record
Students
Courses
Enroll
N
M
Students
Courses Students
Courses
?
!
3.5 Hierarchical Data Model3.5 Hierarchical Data Model
Students
•
Courses
V-Courses V-Students• Grade Grade
Yubsheng-DBS 46
1. The HM and NM are uniformly referred as Record-based Model
2. The operations in record-based models are GET, INSERT, DELET, MODIFY, etc. and all a-record-at-a-time,
3. There are also integrity constraints, such as - A child record cann’t exist without the parent record For inserting a child record, the parent record must
have already existed, and when deleting a parent record, all the children records
have to be deleted.
3.6 Operations and Integrity in Record-based DM
3.6 Operations and Integrity in Record-based DM