Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 More on ER Model Detail Relationships 4

2 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 3  Multi-valued Attribute  An attribute that may take on more than one value for a given entity instance e.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 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 5 Identifier  Composite Identifier  An identifier that consists of a composite attribute e.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 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 7 Attributes Department Number Location Name Phone Manager Start date Control Projects Employees work for Number of Employees Detailed Conceptual Design

8 8 Dept Nounique identifier of a dept.Identifier Dept Namenameof a departmentUnique Locationlocation of a departmentMulti-valued Phonephone no. of a department Employeesno. of employees in a dept.Derived Dept No Dept Name Location Employees Phone Department Detailed Conceptual Design

9 9 Project Name Number Location Leader  Employee Department Control Proj Nounique identifier of a projectIdentifier Proj Namename of a projectUnique Locationlocation of a project Project Proj No Proj Name Location

10 10 Detailed Conceptual Design Employee Name National ID Address Salary Sex Birth Date Works for Department Supervise Employee Emp No

11 11 Detailed Conceptual Design Employee Emp Nounique identifier of an emp.Identifier Emp Namename of an employeeComposite First Namefirst name of an employee Mid Initialsmiddle initials of an employee Last Namelast name of an employee NIDnational id of an employeeUnique Addressaddress of an employee Salarysalary of an employee Gendersex of an employee DOBbirth date of an employee

12 12 Detailed Conceptual Design Emp No Emp Name NID Salary Address Employee Gender DOB First Name Mid Initials Last Name

13 13 Detailed Conceptual Design Dependent NameSexBirth Date Relationship Depd Namename of a dependent Part of Key Gendersex of a dependent DOBbirth date of a dependent Relationrelationship of a dependent to an employee Dependent Depd Name Gender DOB Relation

14 14 Employee works Department Project manage control works on supervise Dependent has Depd Name Gender DOB Relation Emp No Emp Name NID Salary Address Gender DOB First Name Mid Initials Last Name Proj No Proj Name Location Dept No Dept Name Location Employees Phone Hours Start d

15 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 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 depends e.g. Employee is the Owner of Dependent has

17 17 Employee works Department Project manage control works on supervise Dependent has Conceptual Design showing weak entities

18 18 Sample Entity Definitions  Name: Department Type: Regular Definition: a department of an organisation Identifier: Dept_No  Name: Dependent Type: Weak Definition: a person who is a dependent of an employee and entitle for insurance Identifier: Depd_Name (partial only)

19 19 Sample Attribute Definitions  Name: Emp_No Domain: employee identities Definition: unique identifier of an employee Null: No  Name: Emp_Name Components: First_Name, Mid_Initials, Last_Name Definition: a partial identifier of a name Null: No

20 20 Sample Relationship Definitions  Name: Works_for Type: binary 1:M Definition: associates each employee with a department Constraint: each employee must be attached to a department Attributes: none

21 21 Sample Domain Constraints  Name: Employee identities Data Type: character Length: 5 Allowable Characters: digits  Domain Constraints  A specification of the characteristics of the data values that can be associated with one or more attributes

22 22 Sample Domain Constraints Cont ’ d  Name: Last name Data Type: character Max Length: 20  Name: DOB Data Type: date Format: dd/mm/yyyy dd = day mm = month yyyy = year

23 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 24 Completes Date Comp Course CName Course Id Employee Emp Name Emp No Certificate Course CName Course Id Employee Emp Name Emp No Date Comp Cert No 4 4 30 1 many to may relationship 2 one to many relationships

25 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 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 27 Relationships  Ternary Relationship  A simultaneous relationship among the instances of three entity types S1P1W1Land10 S1P1W2Sea15 S1P2W1Air20 S2P1W1Air15 Part Warehouse Supplies Vendor Shipping Mode Unit Cost

28 28 Relationships  Ternary Relationship  can be treated as two many to many relationships Part Vendor Warehouse Supplies Shipping Mode Unit Cost supply

29 29 Entity Relationship Model studno name given family hons slot labmark exammark STUDENT SCHOOL YEAR ENROL YEARREG REG TUTOR YEARTUTOR STAFF COURSE courseno subject equip name year faculty appraiser appraisee APPRAISAL TEACH m n 1 m 1 1 1 m n m m 1 m 1 roomno

30 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 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 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 33 Specialisation & Generalisation Relationships name given family STUDENT studno d undergraduate postgraduate   thesis title year STAFF tutor 1 m

34 34 Superclasses, Subclasses Specialisation & Generalisation Relationships ACADEMIC ADMIN STAFF TECHNICAL payroll no name length of service level grade project    O

35 35 Superclasses, Subclasses Specialisation & Generalisation Relationships EMPLOYEE STUDENT PERSON O name salary fee  O d RESEARCH TEACHING POST GRAD UNDER GRAD thesis     O LECTURING TUTORS   address project  FINAL YEAR year = 3 SUPERVISOR 1-2 courseno 

36 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) PERSON COMPANY U OWNER  personid compid duration of ownership

37 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:  (superclass)    (subclass ) Covering dependency: n (number of subclasses)   (subclass ) =  (superclass) i=1 Disjoint dependency: n (number of subclasses)   (subclass ) =  i=1 name given family STUDENT studno d undergraduate postgraduate   thesis title year

38 38 Specialisation & Generalisation Option B 1. 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 39 Specialisation & Generalisation Option C 1. 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 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 level grade project    O

41 41 Specialisation & Generalisation Relationships EMPLOYEE STUDENT PERSON O name salary fee  O d RESEARCH TEACHING POST GRAD UNDER GRAD thesis     O LECTURING TUTORS   address project  FINAL YEAR year = 3 SUPERVISOR 1-2 courseno 

42 42 Specialisation Lattice with Shared Subclass  To be a shared subclass the superclasses must have the same key. Staff payroll no Academic Admin Technical    d d     Manager   Admin Manager Hourly Staff Salaried Staff

43 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 PERSON COMPANY U OWNER  personid compid duration of ownership OWNER(???) PERSON(???) COMPANY( ??? )

44 44 a)Mapping of EER to Relational model  Create a relation for the super class. with attributes {k,a 1..a n }  Create a relation L i for each S i  Attributes of L i ={k}U{Attributes of S i }  Constraints can be disjoint/overlapping, Total/Partial

45 45 b)Mapping of EER to Relational model  Create a relation L i for each S i  Attributes of L i ={k a 1..a n }U{Attributes of S i }  Constraint must be disjoint and total

46 46 c)Mapping of EER to Relational model  Create a single relation L  Attributes of L={k a 1..a n }U{Attributes of S 1 }U … U{Attributes of S m }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 47 d)Mapping of EER to Relational model  Create a single relation L  Attributes of L={k a 1..a n }U{Attributes of S 1 }U … U{Attributes of S m }U{t 1,t 2,..t m }  ‘ 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 48 Mapping shared sub-classes  Option ‘ a ’ is usually used  Options ‘ c ’ & ‘ d ’ can also be used

49 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 50 Eg. For mapping category  Person(PersonID,name, …,OwnerID)  Company(CompID,name, …,OwnerID)  Owner(OwnerID, OwnerType)

51 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 52 Review: ISA Hierarchies Contract_Emps name ssn 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 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 54 Review: Binary vs. Ternary Relationships  What are the additional constraints in the 2nd diagram? age pname Dependents Covers name Employees ssn lot Policies policyid cost Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot Bad design Better design

55 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 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.grade FROM Students S, Enrolled E WHERE 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 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 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 59 Exercise: Relationships in ER Model  Define Works_In2 table that captures all the available information in the ER diagram. lot dname budget did since name Works_In2 DepartmentsEmployees ssn capacity Locations addr

60 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-11-to ManyMany-to-1 dname budget did since lot name ssn Manages Employees Departments

61 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 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 dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In

63 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 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 age pname Dependents Employees ssn Policy cost

65 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 )


Download ppt "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."

Similar presentations


Ads by Google