1 more on er model detail relationships 4. 2 attributes attribute a property or characteristic of...
Post on 27-Dec-2015
223 Views
Preview:
TRANSCRIPT
1
More on ER Model Detail Relationships
4
2
Attributes
Attribute A property or characteristic of an entity type
that is of interest to the organisation Simple Attribute
An attribute that cannot be broken down into smaller components
e.g. Emp No
Emp No
Detail Conceptual Data Model
3
Multi-valued Attribute An attribute that may take on more than one
value for a given entity instancee.g. Employee Skills, Qualifications
Attributes Cont’d
Composite Attribute An attribute that can be broken down into
component parts
e.g. Address (Street, City, State, Postal Code) Name (First Name, Middle Initials, Last
Name)
Skills
Name
First Name
Mid Initials
Last Name
4
Attributes Cont’d
Stored Attribute An attribute whose valued is stored in the
database Derived Attribute
An attribute whose values can be calculated from related attribute values
e.g. Years Employed (using Employed Date) Age (using Date of Birth)
Age
5
Identifier
Composite Identifier An identifier that consists of a composite
attributee.g. Flight Id (Flight No, Date)
Employee Emp No
Flight Flight Id
Flight No
Date
Identifier An attribute (or combination of attributes)
that uniquely identifiers individual instances of an entity type
e.g. Emp No
6
Identifier Choose an identifier that will not change its
value over the life of each instance of the entity type
Choose an identifier such that each instance of the entity type, the attribute is guaranteed to have valid values and not be null (or unknown)
Avoid the use of so-called intelligent identifiers, whose structure indicates classifications, etc.
Consider substituting single-attribute identifiers for large composite identifiers
7
Attributes
DepartmentDepartment
NumberLocation
NamePhone
Manager
Start date
Control ProjectsEmployees work for
Number of Employees
Detailed Conceptual Design
8
Dept No unique identifier of a dept. IdentifierDept Name name of a department UniqueLocation location of a department Multi-valued Phone phone no. of a departmentEmployees no. of employees in a dept. Derived
Dept No
Dept Name
Location
Employees
Phone
Department
Detailed Conceptual Design
9
Detailed Conceptual Design
ProjectProject
NameNumberLocation Leader Employee
Department Control
Proj No unique identifier of a project IdentifierProj Name name of a project UniqueLocation location of a project
Project
Proj No
Proj Name
Location
10
Detailed Conceptual Design
EmployeeEmployee
NameNational IDAddressSalarySexBirth Date
Works for DepartmentSupervise Employee
Emp No
11
Detailed Conceptual Design
EmployeeEmployee
Emp No unique identifier of an emp. IdentifierEmp Name name of an employee Composite First Name first name of an employee Mid Initials middle initials of an employee Last Name last name of an employeeNID national id of an employee UniqueAddress address of an employeeSalary salary of an employeeGender sex of an employeeDOB birth date of an employee
12
Detailed Conceptual Design
Emp No
Emp Name
NID
Salary
Address
Employee
GenderDOB
First Name
Mid Initials
Last Name
13
Detailed Conceptual Design
DependentDependent
Name Sex Birth Date Relationship
Depd Name name of a dependent Part of KeyGender sex of a dependent DOB birth date of a dependentRelation relationship of a dependent to an employee
Dependent
Depd Name
Gender
DOB
Relation
14
Employee
works
Department
Project
manage
controlworks on
supervise
Dependent
has
Depd Name
GenderDOBRelation
Emp No
Emp NameNID
Salary
Address
Gender
DOB
First Name
Mid Initials
Last Name
Proj No
Proj NameLocation
Dept No
Dept Name
Location
Employees
Phone
Hours
Start d
15
Entity Types
Weak Entity An entity types whose existence depends on
some other entity
Dependent
Employee
Strong (Regular) Entity An entity that exists independently of other
entity types
16
Entity Types
Identifying Relationship A relationship between a weak entity type
and its owner
Identifying Owner The entity type on which the weak entity
type dependse.g. Employee is the Owner of Dependent
has
17
Employee
works
Department
Project
manage
controlworks on
supervise
Dependent
has
Conceptual Design showing weak entities
18
Sample Entity Definitions
Name: DepartmentType: RegularDefinition: a department of an organisationIdentifier: Dept_No
Name: DependentType: WeakDefinition: a person who is a dependent of an employee and entitle for insuranceIdentifier: Depd_Name (partial only)
19
Sample Attribute Definitions
Name: Emp_NoDomain: employee identitiesDefinition: unique identifier of an employeeNull: No
Name: Emp_NameComponents: First_Name, Mid_Initials,
Last_NameDefinition: a partial identifier of a nameNull: No
20
Sample Relationship Definitions
Name: Works_forType: binary 1:MDefinition: associates each employee with a
departmentConstraint: each employee must be attached to a
departmentAttributes: none
21
Sample Domain Constraints
Name: Employee identitiesData Type: characterLength: 5Allowable Characters: digits
Domain Constraints A specification of the characteristics of the
data values that can be associated with one or more attributes
22
Sample Domain Constraints Cont’d Name: Last name
Data Type: characterMax Length: 20
Name: DOBData Type: dateFormat: dd/mm/yyyy
dd = daymm = monthyyyy = year
23
Associative Entity
An entity type that associates the instances of one or more entity types and contains attributes that are peculiar to the relationship between those entity instances
Certificate
24
Completes
Date Comp
Course
CNameCourse Id
Employee
Emp NameEmp No
Certificate
Course
CNameCourse Id
Employee
Emp NameEmp No Date Comp
Cert No
4
4
30
30
11 many to may relationship many to may relationship
22 one to many relationships one to many relationships
25
Associative Entity All of the relationships for the participating
entity types are “many” relationships The resulting associative entity type has
independent meaning to end users, and preferably can be identified with a single-attribute identifier
The associative entity has one or more attributes, in addition to the identifier
The associative entity participates in one or more relationships independent of the entities related in the associated relationships
26
Relationships Unary Relationship
A relationship between the instances of a single entity type
e.g. Person is married to a Person (1:1) Employee manages Employees (1:M)
Binary Relationship A relationship between the instances of two entity
types
Employee
manages
27
Relationships Ternary Relationship
A simultaneous relationship among the instances of three entity types
S1 P1 W1 Land 10S1 P1 W2 Sea 15S1 P2 W1 Air 20S2 P1 W1 Air 15
Part
Warehouse
SuppliesVendor
Shipping Mode
Unit Cost
28
Relationships Ternary Relationship
can be treated as two many to many relationships
Part
Vendor
WarehouseSupplies
Shipping Mode
Unit Cost
supply
29
Entity Relationship Model
studno name
given family
hons
slot
labmark
exammark
STUDENT
SCHOOL
YEARENROL
YEARREG
REG
TUTOR
YEARTUTOR
STAFFCOURSE
courseno
subjectequip
name
year
faculty
appraiserappraisee
APPRAISAL
TEACHm n
1 m
1
11m
n mm
1
m
1
roomno
30
Superclasses, Subclasses; Specialisation & Generalisation Relationships
Subclasses and Superclasses a subclass entity type is a specialised type of
superclass entity type a subclass entity type represents a subset or
subgrouping of superclass entity type’s instances e.g. undergraduates and postgraduates are
subclasses of student superclass Attribute Inheritance
subclasses inherit properties (attributes) of their superclasses
31
Constraints on Specialisation & Generalisation
Specialisation the process of defining a set of more specialised entity types of
an entity type Generalisation
the process of defining a generalised entity type from a set of entity types
Predicate/Condition defined determine the entities that will become members of each
subclass by a condition on an attribute value. All member instances of the subclass must satisfy the predicate
e.g. first years and second years are subclasses of undergraduates based on their year attribute.
User defined no condition for determining subclass membership
32
Constraints on Specialisation & Generalisation
Disjointness Overlap
• the same entity instance may be a member of more than one subclass of the specialisation
Disjoint• the same entity instance may be a member of only one subclass of the
specialisation
Completeness Total
• every entity instance in the superclass must be a member of some subclass in the specialisation
Partial• an entity instance in the superclass need not be a member of any
subclass in the specialisation
33
Specialisation & Generalisation Relationships
name
given family
STUDENT
studno
d
undergraduate postgraduate
thesis title
year
STAFF
tutor
1
m
34
Superclasses, SubclassesSpecialisation & Generalisation Relationships
ACADEMIC ADMIN
STAFF
TECHNICAL
payroll no name
length of service
levelgrade
project
O
35
Superclasses, SubclassesSpecialisation & Generalisation Relationships
EMPLOYEE STUDENT
PERSON
O
name
salaryfee
O
d
RESEARCH TEACHINGPOSTGRAD
UNDERGRAD
thesis
O
LECTURING TUTORS
address
project
FINAL YEAR
year = 3
SUPERVISOR
1-2
1-2
courseno
36
Categories and Categorisation a single superclass/subclass relationship with more
than one superclass, where the superclasses represent different entity types (sometimes with different keys)
PERSONCOMPANY
U
OWNER
personid compid
duration of ownership
37
Specialisation & Generalisation Option A
1. Create a relation for superclass
2. Create a relation for each subclass such that:
{primary_key of superclass} U {attributes of subclass}
key for subclass is (primary_key of superclass)
Inclusion dependency:<key>(superclass) <key>(subclass )
Covering dependency:n (number of subclasses)
<key>(subclass ) = <key>(superclass)i=1
Disjoint dependency:n (number of subclasses)
<key>(subclass ) = i=1
name
given family
STUDENT
studno
d
undergraduate postgraduate
thesis title
year
38
Specialisation & Generalisation Option B1. Create a relation for each subclass such that:
{primary_key U {attributes U {attributes of of superclass} of superclass} subclass}
key for each relation is (primary_key of superclass)
•Works for total and disjoint constraints•Partial: lose any entity that is not in a subclass•Overlapping: redundancy•To recover the superclass can do an OUTER UNION on the subclass relations
name
given family
STUDENT
studno
d
undergraduate postgraduate
thesis title
year
39
Specialisation & Generalisation Option C1. Create one relation such that:
{primary_key U {attributes U {attributes U {type
of superclass} of superclass} of all subclasses} attribute} key for subclass is (primary_key of superclass)
•Many ‘not-applicable’ nulls•Does away with joins•Disjoint: one type which indicates which subclass the tuple represents•Overlap: set of types = number of subclasses•Partial: type is null represents superclass
name
given family
STUDENT
studno
d
undergraduate postgraduate
thesis title
year
40
Specialisation & Generalisation Overlapping
1.STAFF(payrollno,name,lengthofservice)
ACADEMIC(payrollno,level)
TECHNICAL(payrollno,project)
ADMIN(payrollno,grade)
2.ACADEMIC(payrollno,name,lengthofservice,
level)
TECHNICAL(payrollno,name,lengthofservice,
project)
ADMIN(payrollno,name,lengthofservice,grade)
3.STAFF(payrollno,name,lengthofservice,level,
project,grade,type1,type2,type3)
STAFF(payrollno,name,lengthofservice,level,
project,grade,type)
type = powerset of classes
ACADEMIC ADMIN
STAFF
TECHNICAL
payroll no name
length of service
levelgrade
project
O
41
Specialisation & Generalisation Relationships
EMPLOYEE STUDENT
PERSON
O
name
salaryfee
O
d
RESEARCH TEACHINGPOSTGRAD
UNDERGRAD
thesis
O
LECTURING TUTORS
address
project
FINAL YEAR
year = 3
SUPERVISOR
1-2
1-2
courseno
42
Specialisation Lattice with Shared Subclass
To be a shared subclass the superclasses must have the same key.
Staffpayroll no
Academic AdminTechnical
d
d
Manager
Admin Manager
Hourly Staff
Salaried Staff
43
Categories and Categorisation A category is a subclass of the union of two or more superclasses
that can have different keys because they can be of different entity types
If defining superclasses have different keys, specify a new surrogate key
PERSONCOMPANY
U
OWNER
personid compid
duration of ownership OWNER( ??? )PERSON( ??? )COMPANY( ??? )
44
a)Mapping of EER to Relational model
Create a relation for the super class. with attributes {k,a1..an}
Create a relation Li for each Si
Attributes of Li={k}U{Attributes of Si}
Constraints can be disjoint/overlapping, Total/Partial
45
b)Mapping of EER to Relational model
Create a relation Li for each Si
Attributes of Li={k a1..an}U{Attributes of Si}
Constraint must be disjoint and total
46
c)Mapping of EER to Relational model
Create a single relation L Attributes of L={k a1..an}U{Attributes of S1}U…
U{Attributes of Sm}U{t} ‘t’ is a type attribute that indicates the sub-
class to which each tuple belongs. This may create a lot of null values
Constraint is disjoint (total or partial) Not very space efficient hence can use a
previous method But no need of ’joins’ to get all attributes
47
d)Mapping of EER to Relational model
Create a single relation L Attributes of L={k a1..an}U{Attributes of S1}U…
U{Attributes of Sm}U{t1,t2 ,..tm } ‘t’ is a type attribute that indicates the sub-
class to which each tuple belongs. This may create a lot of null values
Constraint is overlapping (total or partial) Not very space efficient hence can use a
previous method But no need of ’joins’ to get all attributes
48
Mapping shared sub-classes
Option ‘a’ is usually used Options ‘c’ & ‘d’ can also be used
49
Mapping of categories
If the super-classes have different keys we have to define a new key attribute called a ‘surrogate key’. When creating a relation to correspond to the category.
Create a relation to correspond to the category,include any atributes of the category.The primary key is the surrogate key
Each super-class is also mapped into a relation with its own primary key, the surrogate key becomes a foreign key for this.
50
Eg. For mapping category
Person(PersonID,name,…,OwnerID) Company(CompID,name,…,OwnerID) Owner(OwnerID, OwnerType)
51
Hints for EER Modelling identify entity types by searching for nouns and noun phrases assume all entities are strong and check for weak ones on a later
pass need an identifier for each strong entity assume all relationships are partial participation (optional) and
check for total (mandatory) ones on a later pass expect to keep changing your mind about whether things are
entities, relationships or attributes keep level of detail relevant and consistent (for example leave out
attributes at first) approach diagram through different views and merge them
52
Review: ISA Hierarchies
Contract_Emps
namessn
Employees
lot
hourly_wages
ISA
Hourly_Emps
contractid
hours_worked
As in C++, or other PLs, attributes are inherited. If we declare A ISA B, every A entity is also considered to be a B entity.
Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed)
Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)
53
Translating ISA Hierarchies to Relations
General approach: 3 relations: Employees, Hourly_Emps and Contract_Emps.
• Hourly_Emps: Every employee is recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn); must delete Hourly_Emps tuple if referenced Employees tuple is deleted).
• Queries involving all employees easy, those involving just Hourly_Emps require a join to get some attributes.
Alternative: Just Hourly_Emps and Contract_Emps. Hourly_Emps: ssn, name, lot, hourly_wages,
hours_worked. Each employee must be in one of these two subclasses.
54
Review: Binary vs. Ternary Relationships
What are the additional constraints in the 2nd diagram?
agepname
DependentsCovers
name
Employees
ssn lot
Policies
policyid cost
Beneficiary
agepname
Dependents
policyid cost
Policies
Purchaser
name
Employees
ssn lot
Bad design
Better design
55
Binary vs. Ternary Relationships (Contd.)
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, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)
56
Views
A view is just a relation, but we store a definition, rather than a set of tuples.
CREATE VIEW YoungActiveStudents (name, grade)AS SELECT S.name, E.gradeFROM Students S, Enrolled EWHERE S.sid = E.sid and S.age<21
Views can be dropped using the DROP VIEW
command. How to handle DROP TABLE if there’s a view on the
table?• DROP TABLE command has options to let the user
specify this.
57
Views and Security
Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s). Given YoungStudents, but not Students or
Enrolled, we can find students s who are enrolled, but not the cid’s of the courses they are enrolled in.
58
Relational Model: Summary
A tabular representation of data. Simple and intuitive, currently the most widely
used. Integrity constraints can be specified by the
DBA, based on application semantics. DBMS checks for violations. Two important ICs: primary and foreign keys In addition, we always have domain constraints.
Powerful and natural query languages exist. Rules to translate ER to relational model
59
Exercise: Relationships in ER Model
Define Works_In2 table that captures all the available information in the ER diagram.
lot
dname
budgetdid
sincename
Works_In2 DepartmentsEmployees
ssn
capacity
Locations
addr
60
Review: Key Constraints
Each dept has at most one manager, according to the key constraint on Manages.
Translation to relational model?
Many-to-Many1-to-1 1-to Many Many-to-1
dname
budgetdid
since
lot
name
ssn
ManagesEmployees Departments
61
Translating ER Diagrams with Key Constraints
Map relationship to a table: Note that did is
the key now! Why not ssn?
Separate tables for Employees and Departments.
Since each department has a unique manager, we could instead combine Manages and Departments.
CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)
CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)
62
Review: Participation Constraints
Does every department have a manager? If so, this is a participation constraint: the
participation of Departments in Manages is said to be total (vs. partial).• Every did value in Departments table must
appear in a row of the Manages table (with a non-null ssn value!)lot
name dnamebudgetdid
sincename dname
budgetdid
since
Manages
since
DepartmentsEmployees
ssn
Works_In
63
Participation Constraints in SQL
We can capture participation constraints involving one entity set in a binary relationship, but little else (without resorting to CHECK constraints).
CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE NO ACTION)
64
Review: Weak Entities
A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Owner entity set and weak entity set must
participate in a one-to-many relationship set (1 owner, many weak entities).
Weak entity set must have total participation in this identifying relationship set.
lot
name
agepname
DependentsEmployees
ssn
Policy
cost
65
Translating 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)
top related