Database Design I

Database Design I

Database Design • Goal: specification of database schema Database Design I: • Methodology: – Use E-R model to get a high-level graphical view of The Entity-Relationship Model essential components of enterprise and how they are related Chapter 5 – Convert E-R diagram to DDL • E-R Model: enterprise viewed as set of – Entities – Relationships among entities 1 2 Entity Type Entities · Entity type described by set of attributes · Entity: an object that is involved in the ± Student: Id, Name, Address, Hobbies enterprise · Domain: possible values of an attribute ± Ex: John, CSE305 ± Value can be a set (in contrast to relational model) • (111111, John, 123 Main St, (stamps, coins)) · Entity Type: set of similar objects • Key: minimum set of attributes that uniquely ± Ex: students, courses identifies an entity (candidate key) · Attribute: describes one aspect of an entity type • Entity Schema: entity type name, attributes (and ± Ex: name, maximum enrollment associated domain), key constraints 3 4 Representation in Relational Model • Entity type corresponds to a relation Entity Type (con' t) • Relation’s attributes = entity type’s attributes · Graphical Representation in E-R diagram: – Problem: entity type can have set valued attributes. – Solution: Use several rows to represent a single entity • (111111, John, 123 Main St, stamps) • (111111, John, 123 Main St, coins) – Problems with solution: • Redundancy • Key of entity type not key of relation • => resulting relation must be further transformed Set-valued attribute 5 6 •1 Relationship Attributes and Roles · Attribute of a relationship type describes the · Relationship: relates two or more entities · Attribute ± John majors in Computer Science relationship · Relationship Type: set of similar relationships ± e.g., John majors in CS since 2000 ± Student (entity type) related to Department (entity type) · John and CS are related by MajorsIn (relationship type). · 2000 describes relationship - value of SINCE attribute of MajorsIn relationship type · Distinction: MajorsIn ± relation (relational model) - set of tuples · Role of a relationship type names one of the ± relationship (E-R Model) ± describes relationship related entities between entities of an enterprise ± e.g., John is value of Student role, CS value of ± Both entity types and relationship types (E-R model) Department role of MajorsIn relationship type can be represented as relations (relational model) ± (John, CS, 2000) describes a relationship 7 8 Relationship Type Roles · Problem: relationship can relate elements of · Described by set of attributes and roles same entity type ± e.g., MajorsIn: Student, Department, Since ± e.g., ReportsTo relationship type relates two ± Here we have used as the role name (Student) elements of Employee entity type: the name of the entity type (Student) of the · Bob reports to Mary since 2000 participant in the relationship, but ... ± We do not have distinct names for the roles ± It is not clear who reports to whom 9 10 Schema of a Relationship Type Roles (con’ t) • Role names, Ri, and their corresponding entity sets. Roles must be single valued (number of • Solution: role name of relationship type roles = degree) need not be same as name of entity type • Attribute names, Aj, and their corresponding from which participants are drawn domains. Attributes may be set valued – ReportsTo has roles Subordinate and • Key: Minimum set of roles and attributes that Supervisor and attribute Since uniquely identify a relationship – Values of Subordinate and Supervisor both • Relationship: <e1, …en; a1, …ak> drawn from entity type Employee – ei is an entity, a value from Ri’ s entity set – aj is a set of attribute values with elements from domain of Aj 11 12 ·2 Graphical Representation Representation of Relationship Type in Relational Model · Roles are edges labeled with role names (omitted if role name = name of entity set). Most attributes have been omitted. · Attributes of corresponding relation are ± Attributes of relationship type ± For each role, the primary key of the entity type associated with that role · Example: RoomNo DeptId Name SectNo CrsCode Enroll S2000Courses Teaching Professor Id ± S2000Courses (CrsCode, SectNo, Enroll) ± Professor (Id, DeptId, Name) ± Teaching (CrsCode, SecNo, Id, RoomNo) 13 14 Representation in Relational Model Representation in SQL · Candidate key of corresponding table = candidate key of · Each role of relationship type produces a relation ± Except when there are set valued attributes foreign key in corresponding relation ± Example: Teaching (CrsCode, SectNo, Id, RoomNo, TA) ± Foreign key references table corresponding to · Key of relationship type = (CrsCode, SectNo) · Key of relation = (CrsCode, SectNo, TA) entity type from which role values are drawn CrsCode SectNo Id RoomNo TA CSE305 1 22 1234 Hum Joe CSE305 1 22 1234 Hum Mary 15 16 Example 1 Example 2 Date Price Since Status Project Sold Part Professor WorksIn Department Supplier CREATE TABLE WorksIn ( CREATE TABLE Sold ( Since DATE, -- attribute Price INTEGER, -- attribute Status CHAR (10), -- attribute Date DATE, -- attribute ProfId INTEGER, -- role (key of Professor) ProjId INTEGER, -- role DeptId CHAR (4), -- role (key of Department) SupplierId INTEGER, -- role PRIMARY KEY (ProfId), -- since a professor works in at most one department PartNumber INTEGER, -- role FOREIGN KEY (ProfId) REFERENCES Professor (Id), PRIMARY KEY (ProjId, SupplierId, PartNumber, Date), FOREIGN KEY (ProjId) REFERENCES Project, FOREIGN KEY (DeptId) REFERENCES Department ) FOREIGN KEY (SupplierId) REFERENCES Supplier (Id), FOREIGN KEY (PartNumber) REFERENCES Part (Number) ) 17 18 ·3 Key Constraint (special case) Key Constraint (special case) · Relational model representation: key of relation corresponding to entity type is key of relation · If, for a particular participant entity type, corresponding to relationship type each entity participates in at most one ± Id is primary key of Professor; ProfId is key of relationship, corresponding role is a key of WorksIn. Professor 4100 does not participate. ± Cannot use foreign key in Professor since some relationship type professors do not participate ± E.g., Professor role is unique in WorksIn · Representation in E-R diagram: arrow Id ProfId 1123 4100 1123 CSE Professor WorksIn Department 3216 3216 AMS WorksIn 19 Professor 20 Entity Type Hierarchies IsA · One entity type might be subtype of another ± Freshman is a subtype of Student Student · A relationship exists between a Freshman entity Represents four and the corresponding Student entity IsA relationship types ± e.g., Freshman John is related to Student John · This relationship is called IsA ± Freshman IsA Student Freshman Sophmore Junior Senior ± The two entities related by IsA are always descriptions of the same real-world object 21 22 Properties of IsA IsA · Inheritance - Attributes of supertype apply to subtype. · Advantage: Used to create a more concise ± E.g., GPA attribute of Student applies to Freshman and readable E-R diagram ± Subtype inherits all attributes of supertype. ± Attributes common to different entity sets need ± Key of supertype is key of subtype not be repeated · Transitivity - Hierarchy of IsA ± They can be grouped in one place as attributes ± Student is subtype of Person, Freshman is of supertype subtype of Student, so Freshman is also a ± Attributes of (sibling) subtypes can be different subtype of Student 23 24 ·4 IsA Hierarchy - Example Type Hierarchy · Might have associated constraints: ± Covering constraint: Union of subtype entities is equal to set of supertype entities · Employee is either a secretary or a technician (or both) ± Disjointness constraint: Sets of subtype entities are disjoint from one another · Freshman, Sophomore, Junior, Senior are disjoint sets · Might be related to fragmentation of data 25 26 Type Hierarchies and Relational Model Type Hierarchies and Relational Model · Supertypes and subtypes can be realized as Id attribs0 separate relations Student ± Need a way of identifying subtype entity with its (unique) related supertype entity · Choose a candidate key and make it an attribute of all entity types in hierarchy Id attribs1 Id attribs2 Id attribs3 Id attribs4 Freshman Sophmore Junior Senior 27 28 Participation Constraint Type Hierarchies and Relational Model · If every entity participates in at least one relationship, a participation constraint · Redundancy eliminated if IsA is not disjoint holds: ± For individuals who are both employees and ± A participation constraint of entity type E students, Name and DOB are stored once having role ρ in relationship type R states that for e in E there is an r in R such that ρ(r) = e. Person Employee Student ± e.g., every professor works in at least one department SSN Name DOB SSN Department Salary SSN GPA StartDate 1234 Mary 1950 1234 Accounting 35000 1234 3.5 1997 Reprsentation in E-R Professor WorksIn Department 29 30 ·5 Representing Participation Constraints Participation Constraint in Relational · Inclusion dependency: Every professor works in at least one dep' t. Model ± in relational model: (easy) · Professor (Id) references WorksIn (ProfId) ± in SQL: · Example (can' t use foreign key in Professor) · Special case: Every professor works in exactly one dep' t. (easy) ± FOREIGN KEY Id REFERENCES WorksIn (ProfId) ProfId Id · General case (not so easy): 1123 CSE 1123 1123 AMS 4100 CREATE ASSERTION ProfsInDepts 4100 ECO 3216 CHECK ( NOT EXISTS ( 3216 AMS SELECT * FROM Professor P Professor WHERE NOT EXISTS ( WorksIn SELECT * FROM WorksIn W WHERE

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us