TDDB77 August 08 Overview Real world Query Model Data Modeling Using ER/EER model Database Answer Processing of queries and updates DBMS He Tan hetan@ida.liu.se Access to stored data Physical database 2 Entity-Relationship (ER) Model Main concepts High-level conceptual data model an overview of the database to discuss with non-database experts Easy to translate to data model of DBMS Easy Entity – a ”thing” in the real world with an independent existance Attributes – properties that describes an entity Entity type – a collection of entities that have the same set of attributes Model RegNumber ER diagram Year PersonalNumber Name Car Owner 3 Main concepts cont. 4 Attributes Relationship type – association among entity types Simple vs composite Single-valued vs multivalued Stored vs derived Street City Model RegNumber PersonalNumber Name Name Year Address PersonalNumber N 1 Owner owns Car Owner PhoneNumber Age 5 IDA 6 1 TDDB77 August 08 Constraints on attributes Example 1 Value sets (domains) of attributes Key attributes Street Person lives at a certain address City Are addresses important in the database? Name Address PersonalNumber Owner PhoneNumber Age 7 Constraints on relationship types Constraints on relationship types 8 Cardinality ratio – maximum number of relationship instances that an entity can participate in Participant constraint Total participation – an entity must exist related to another entity possible cardinality ratio: 1:1, 1: N, N:1, N:M ”Every car must be owned by at least one owner.” PersonalNumber 1 N N Owner N M Owner Car owns RegNumber Car owns 1 Owner Car owns N M Owner Car owns 9 Constraints on relationship types 10 Attributes of relation types Weak entity types– do not have key attibutes of their own. ”Store information on who owned which car and during which period of time” an weak entity can be identified uniquely by being related to another entity (together with its own attributes). SellDate Name Year PersonalNumber Name version N 1 Software has_version N Owner Software version 11 IDA Model RegNumber BuyDate M owns Car 12 2 TDDB77 August 08 N-ary relationships ER Notation Meaning Symbol ENTITY TYPE WEAK ENTITY TYPE Example. A person works as an engineer at one company and as a gym instructor at another company. RELATIONSHIP TYPE IDENTIFYING RELATIONSHIP TYPE ATTRIBUTE KEY ATTRIBUTE Company Employee N N works at M JobType MULTIVALUED ATTRIBUTE works as COMPOSITE ATTRIBUTE M K Employee Ternary DERIVED ATTRIBUTE N Company works as E1 R M 1 E1 JobType R N E2 TOTAL PARTICIPATION OF E2 IN R E2 CARDINALITY RATIO 1:N FOR E1:E2 IN R 13 14 Example 2 Enhanced ER Model (EER) Customers can rent video films Why more? More complex data requirements Can the same video be rented more than once? Can a video exist without customers? Is the rental history important? Example. Only some employees can use a company car, only managers have to write a monthly report, but all employees have assigned personal number, salary account and a place in the office. Subclass/superclass, specialization/generalization, union/category attribute and relationship inheritance Manager U Manager U writes ProjectLeader N MonthlyReport PID ReportID 17 IDA Engineer U RegNumber Salesman 1 1 Car Commission ProjectLeader o U uses d U U U Engineer 1 FirstName Name Employee o U d Salesman Surname PN process of defining classes Employee Commission Single vs. Multiple inheritance U FirstName Name 16 U Surname PN specialization Subclass/Superclass generalization 15 SoftwareProject N 1 manages Software ProjectLeader 18 3 TDDB77 August 08 Example 3 A taxi company needs to model their activities Union/category A subclass represents a collection of entities that is a subset of the UNION of the entities of multiple distinct superclasses CNumber PersonalNumber Person BirthDate Address “An owner of a car may be a person or a company.” Company u U 1 Owner N owns Car There are two types of employees in the company: drivers and operators. For drivers it is interesting to know the date of issue and type of the driving license, and the date of issue of the taxi driver’s certificate. For all employees it is interesting to know their personal number, address and the available phone numbers. The company owns a number of cars. For each car there is a need to know its type, year of manufacturing, number of places in the car and date of the last service. The company wants to have a record of car trips (körningar). A taxi may be picked on a street or ordered though an operator who assigns the order to a certain driver and a car. Departure and destination addresses together with times should also be recorded. 19 20 TaxiCertifDate Street PN Address Phone PostNumber A driver may have many driving licenses (types) DrivingLicenseDate Driver Town Employee DrivingLicenseType o DrivingLicenseDate U U TaxiCertifDate Driver DrivingLicenseType Date Operator 1 Type Id 1 TaxiCertifDate N assign drives 1 belongsTo DrivingLicense Körkort Driver N DepTime Destination Type N RegNumber ServiceDate DestTime ID TaxiCertifDate YearOfManuf Trip DeparturePlace Type DrivingLicense N made_by 1 N belongsTo M Driver Places Car Date 21 Summary 22 Overview Real world Entity-Relationship diagram – a graphical way to model the world Main concepts - entity, relationship and attribute Different types of constraints Enhanced ER model Model Database DBMS Query Answer Processing of queries and updates Access to stored data Physical database 23 IDA 24 4