Database Modeling 2

Embed Size (px)

DESCRIPTION

Course notes

Citation preview

CS480: Database Systems

CS480: Database SystemsDatabase ModelingDatabasesModeling or Data representation(of an enterprise)Management of the data Management is added value of DBMS over File Representation DDL & DMLLevels of Abstraction in a DBMSEnd-userProgram AProgram BProgram CUse DMLLevels of Abstraction in a DBMSEnd-userProgram AProgram BProgram CView 1View 1Use DMLExternal SchemaLevels of Abstraction in a DBMSEnd-userProgram AProgram BProgram CView 1View 1Use DMLExternal SchemaCONCEPTUAL

SCHEMA

Levels of Abstraction in a DBMSEnd-userProgram AProgram BProgram CView 1View 1Use DMLExternal SchemaCONCEPTUAL

SCHEMA

Database:Organized according to physical schema.External, Conceptual and Physical SchemasDefined by DDLLevels of Abstraction in a DBMSEnd-userProgram AProgram BProgram CView 1View 1Use DMLExternal SchemaCONCEPTUAL

SCHEMA

Database:Organized according to physical schema.External, Conceptual and Physical SchemasDefined by DDLDDL+DML = Data ModelInstances and SchemasSimilar to types and variables in programming languages.Conceptual Schema - the logical structure and design of the databaseRelatively FixedAnalogous to type information in a programDatabase Instance - the actual content of the database at a particular point in time Analogous to the value of variableInstances and SchemasSchema for STUDENT:

Instance for STUDENT:SS#NameAddressMajorSS#NameAddressMajor12345AliceChicagoCS23456BobJolietECE34567CharlesGaryCME45678DanChicagoCSDesign of the Conceptual Schema (Data Modeling)Building Blocks of Data ModelingEntity Sets: Set of abstract (course) or concrete (student) objects that are distinguishable.Are there objects that are not distinguishable?Relationships: Association of several entity setsUniversity System ExampleEntity SetsSTUDENT, COURSERelationshipENROLLMENTSTUDENT is enrolled in a COURSEUniversity System ExampleRelationships could include several entity setsFollowing example is a ternary relationshipEntity Sets:STUDENT, COURSE, SEMESTER (abstract?)Relationship:ENROLLMENTSTUDENT is enrolled in a COURSE during a SEMESTEREntity Sets and RelationshipsEach entity set and relationship has:NameAttributesAn attribute is a property of an entity or a relationshipEx. Attributes of a STUDENT entity setStudent-id, name, address, major, etc.Ex. Attributes of ENROLLMENT relationshipGradeEntity Sets and RelationshipsEntity Sets and Relationships are represented in the computer as a set records.Student-idNameAddressMajor12345AliceChicagoCS23456BobJolietECE34567CharlesGaryCME45678DanChicagoCSSTUDENT:Student-idCourse NameGrade12345CS 480A12345CS 401B34567CME 508DENROLLMENT:Modeling SubtletiesModeling choices will affect the usefulness of the database systemExample:Entity Sets: ITEMS, SUPPLIERSRelationship: SUPPLIESA SUPPLIER supplies an ITEMIs the attribute PRICE a property of the ITEM or of SUPPLIES?Could be modeled either wayThe choice will affect how the database will be used.Y2K?15Modeling SubtletiesModeling choices will affect the usefulness of the database systemExample:Entity Sets: ITEMS, SUPPLIERSRelationship: SUPPLIESA SUPPLIER supplies an ITEMIs the attribute PRICE a property of the ITEM or of SUPPLIES?Could be modeled either wayThe choice will affect how the database will be used.Y2K?16Modeling SubtletiesModeling choices will affect the usefulness of the database systemExample:Entity Sets: ITEMS, SUPPLIERSRelationship: SUPPLIESA SUPPLIER supplies an ITEMIs the attribute PRICE a property of the ITEM or of SUPPLIES?Could be modeled either wayThe choice will affect how the database will be used.Y2K?17Entity-Relationship (ER) DiagramVisual tool for designing the conceptual schemaProposed by Peter Chen in 1976Entity SetRelationship SetAttributeER DiagramExample:SUPPLIERITEMER DiagramExample:SUPPLIERITEMSUPPLIESER DiagramExample:SUPPLIERITEMSUPPLIESColorWeightItem NamePriceSupp. AddressSupp. NameRepresentation of Ternary RelationshipsCourseSemesterStudentEnrollmentWhats Next?Observations about relationshipsFunctionality of RelationshipsSpecial Types of RelationshipsExistenceIs-aKeysComprehensive Data Modeling ExampleObservations about RelationshipsThere can exist a relationship set between the same entity set:Observations about RelationshipsThere can exist a relationship set between the same entity set:CourseUniversityDatabasePreReqObservations about RelationshipsThere can exist a relationship set between the same entity set. CourseEmployeeUniversityDatabasePreReqManagesCompanyDatabaseDifferent roles for the entity set.Observations about RelationshipsThere can be more than one relationship set between a pair of entity sets:EmployeeProjectManagesWork-onCompany Database:Observations about RelationshipsThere can be more than one relationship set between a pair of entity sets:StudentCourseTakesTAsUniversity Database:Functionality of RelationshipsMapping Cardinality ConstraintsExpress the number of entities to which another entity can be associated via a relationship setMost useful in describing binary relationship sets.

One to OneFor each member of entity set A there is at most one related member of B.Ex:MenWomenMarriageArrow points toward the singular entitiesOne to ManyFor each member of entity set A, there are zero or more related members of B.For each member of B, there is at most one member of A.One to ManyFor each member of entity set A, there are zero or more related members of B.For each member of B, there is at most one member of A.MenChildrenFathersArrow points toward the singular entitiesMany to ManyFor each member of entity set A, there are zero or more related members of B.For each member of entity set B, there are zero or more related member of A.SUPPLIERITEMSUPPLIESMany to ManyFor each member of entity set A, there are zero or more related members of B.For each member of entity set B, there are zero or more related member of A.SUPPLIERITEMSUPPLIESCourseStudentEnrollmentMapping Cardinality Constraints

One to oneOne to manyNote: Some elements in A and B may not be mapped to any elements in the other setMapping Cardinality ConstraintsMany to oneMany to manyNote: Some elements in A and B may not be mapped to any elements in the other set

Relationship InstanceABRRelationship InstanceABR

Database Instance of RSCHEMA IS DEFINED PRIOR TO DATABASE INSTANCE

THE DATABASE INSTANCE DOES NOT DETERMINE WHAT THE SCHEMA IS.38Relationship InstanceABR

Database Instance of RIs it a possible instance of R?SCHEMA IS DEFINED PRIOR TO DATABASE INSTANCE

THE DATABASE INSTANCE DOES NOT DETERMINE WHAT THE SCHEMA IS.39Relationship InstanceABR

Database Instance of RIs R then One to One?Is it a possible instance of R?SCHEMA IS DEFINED PRIOR TO DATABASE INSTANCE

THE DATABASE INSTANCE DOES NOT DETERMINE WHAT THE SCHEMA IS.40Relationship InstanceABR

Database Instance of RIs R then One to One?Separation of Database Schema and Database InstanceIs it a possible instance of R?SCHEMA IS DEFINED PRIOR TO DATABASE INSTANCE

THE DATABASE INSTANCE DOES NOT DETERMINE WHAT THE SCHEMA IS.41Existence RelationshipsA one to many relationship set R, between entity sets A and B indicates existence-dependence if an entity set of type B can exist in the database only if it is related to an entity of type A.B is called a dominated entity set.

R is represented by:B represented by:Existence RelationshipsA company wants to store information about the children of employees.Existence RelationshipsA company wants to store information about the children of employees.EmployeeFathersChildSalaryLastNameFirstNameSS#nameBirthDateExistence RelationshipsIn a university system: modeling courses and their possible sections.CourseCourse-secSectionCreditsCourseidTitleSection idSemesterYearExistence RelationshipsShould Works-on be an existence relationship?EmployeeWorks onProjectExistence RelationshipsShould Works-on be an existence relationship?EmployeeWorks onProjectWill depend on company policy. If project can be worked on by at most one employee AND every project must have at least one employee working it.Existence RelationshipsA company wants to store information about the children of employees.EmployeeFathersChildSalaryLastNameFirstNameSS#nameBirthDateIMPLICATIONS?Existence RelationshipSS#First NameLast NameSalary12345AliceAllen100K23456BobBarry50K34567CharlesCole20K45678DanDole200KDads SS#Child NameBirthdate12345Amy1/1/1023456Bill2/2/0912345Andy3/3/1145678Dave4/4/0745678Dylan5/5/05Existence RelationshipSS#First NameLast NameSalary12345AliceAllen100K23456BobBarry50K34567CharlesCole20K45678DanDole200KDads SS#Child NameBirthdate12345Amy1/1/1023456Bill2/2/0912345Andy3/3/1145678Dave4/4/0745678Dylan5/5/05Existence RelationshipSS#First NameLast NameSalary23456BobBarry50K34567CharlesCole20K45678DanDole200KDads SS#Child NameBirthdate12345Amy1/1/1023456Bill2/2/0912345Andy3/3/1145678Dave4/4/0745678Dylan5/5/05Existence RelationshipSS#First NameLast NameSalary23456BobBarry50K34567CharlesCole20K45678DanDole200KDads SS#Child NameBirthdate12345Amy1/1/1023456Bill2/2/0912345Andy3/3/1145678Dave4/4/0745678Dylan5/5/05Existence RelationshipSS#First NameLast NameSalary23456BobBarry50K34567CharlesCole20K45678DanDole200KDads SS#Child NameBirthdate23456Bill2/2/0945678Dave4/4/0745678Dylan5/5/05If an element of the dominating set is deleted, then its related elements in the weak set should also be deleted.ISA RelationshipIf each entity in the entity set B is a particular type of entity in entity set A then we say that B isa A.Namely, there is an ISA relationship between A and B.SpecializationISA Relationship ExampleHospital EmployeeSalarySS#NameAddressISAISAPhysicianLabTechnicianSpecializationWork HoursObservations about ISA relationshipsIs always One to OneInduces a specialization hierarchy in the ER-diagramEg. A ISA B ISA C If A ISA B then all the attributes of A are inherited by B.The Lab Technician has a salary and ss#, etc.Lower level classes dont share attributesA physician doesnt have work hours and a lab technician doesnt have a specialization.

KeysDef: The key of an entity set E is a minimal subset of the attributes of E that uniquely indentifies an entity in the set.This subset of attributes adequately distinguishes between elements of the entity set.Ask if they know what minimal means

Fingerprint, DNA sequence, SS#57KeysStudentaddressnameStudent-idKeysStudentaddressnameStudent-idIs {name} a key?KeysStudentaddressnameStudent-idIs {name} a key?Is {address} a key?KeysStudentaddressnameStudent-idIs {name} a key?Is {address} a key?Is {student-id, address} a key?NOT MINIMAL61KeysStudentaddressnameStudent-id{student-id}

Minimal Uniquely identifies a studentKeysStudentaddressnameStudent-id{student-id}

Minimal Uniquely identifies a studentKeys in the ER-diagram areunderlined.What is key for actors?ActorsMoviesStarred-inTitleYearLengthSalaryLast NameFirst NameAgeMiddleInitialFilm TypeWhat is key for Movies?ActorsMoviesStarred-inTitleYearLengthSalaryLast NameFirst NameAgeMiddleInitialFilm TypeKeysActorsMoviesStarred-inTitleYearLengthSalaryLast NameFirst NameAgeMiddleInitialFilm TypeKeys for ISA RelationshipsHospital EmployeeSalarySS#NameAddressISAISAPhysicianLabTechnicianSpecializationWork HoursWhat is the keyfor Physician andLab Technician?Keys for ISA RelationshipsIf A is a B then the key of B are attributes inherited from A.Key for a Physician or for a Lab Technician is inherited from Hospital Employee entity set. Their keys are the SS#.Keys for Dominated Entity SetsEmployeeFathersChildSalaryLastNameFirstNameSS#nameBirthDateWhat is the key for Child?Are any of these keys: {name}, {Birth Date}, {name,BirthDate}? Keys for Dominated Entity SetsEmployeeFathersChildSalaryLastNameFirstNameSS#nameBirthDateKey for Child will include key attributes for Employee.

{SS#, name} Keys for Dominated Entity SetsEmployeeFathersChildSalaryLastNameFirstNameSS#nameBirthDateIn general, a dominated entity sets key may include attributes in the dominating set.Situation in which one would not include an attribute from the dominating set????Possibly section one for the courses system. If there is a section-id attribute then its enough.71Keys of a Relationship SetThe key of a relationship set R is a minimal set of attributes (not necessarily of R) that uniquely identifies a relationship in the set.Keys of a Relationship SetSUPPLIERPARTSUPPLIESColorWeightItem NamePriceSupp. AddressSupp. NameSupplierIdPart IdWhat is key for SUPPLIES?Keys of a Relationship SetSUPPLIERPARTSUPPLIESColorWeightItem NamePriceSupp. AddressSupp. NameSupplierIdPart IdUsually we take keys of participating entities: {SuppId, PartId}Keys of a Relationship SetSUPPLIERPARTSHIPMENTColorWeightItem NamePriceSupp. AddressSupp. NameSupplierIdPart IdDateWhat is key for SHIPMENT?Keys of a Relationship SetSUPPLIERPARTSUPPLIESColorWeightItem NamePriceSupp. AddressSupp. NameSupplierIdPart IdDateThe key may also involve a relationship attribute.Exercise ER-DiagramDatabase System for an Airline to be used for:Booking FlightsManaging Flights, Crew Members, and their PlanesConstruct an ER-diagram for this Airline Enterprise.