Presentation is loading. Please wait.

Presentation is loading. Please wait.

ER Diagrams and Relational Model CS 174a (Winter 2015)

Similar presentations


Presentation on theme: "ER Diagrams and Relational Model CS 174a (Winter 2015)"— Presentation transcript:

1 ER Diagrams and Relational Model CS 174a (Winter 2015)

2 ER Model Notations ICS 122 A Fall 2006

3 ER Model Notations

4 Class Hierarchies (ISA Relationship)  Lots of times an entity set has members that have special properties not associated with all the members of the entity set.  E.g., the set of checking accounts and savings accounts are a subset of the set of accounts. Checking has a overdraft amount, and savings has a interest-rate.  depending upon its type, savings and checking accounts may participate in different relationships.

5 Class Hierarchies - Example savings and checking are subclasses of the account entity set account is a superclass of savings and checking entity sets An entity in a subclass has to belong to superclass as well -- that is, every savingsaccount is also an account. Similarly every checking account is also an account Attribute Inheritance: subclasses inherit all the attributes of the superclass. Similarly, subclasses inherit all relationships in which the superclass participates account ISA savings checking account# balance interest rates overdraft amount

6 Class Hierarchies - Constraints Overlap Constraints Whether two subclasses are allowed to contain the same entity In previous example, if an account can be both Checking and Savings => Overlapping Constraint (Denoted by: Savings OVERLAPS Checking ) Else (Checking acct can never be Savings acct) => No Overlapping Constraint (Default) Covering Constraints Whether the entities in the subclasses collectively include all the entities of superclass Ex. If all the accounts are either checking or savings => Covering Constraint (denoted by: Checking AND Savings COVER Account) Else (there can be an acct which is neither Checking nor Savings) => No Covering constraint (Default)

7 Aggregation - Example Used when we have to model a relationship involving (entity sets and) a relationship set. Aggregation allows us to treat a relationship set as an entity set for purposes of participation in (other) relationships. Consider a relationship works-on between Employee and Project. Suppose we want to record managers for tasks performed by an employee in a project. We need to express relationship among works-on and manages relationships. In such cases, we use aggregation shown by dashed box around works-on (and its entities).

8 Solution using aggregation:

9 Aggregation vs Ternary Relationship Can we make it a ternary relationship? - works-on and manages have distinct attributes - also, cannot express constraints like ‘Employee on a project is managed by at most one manager’ If we do not have above constraint and no attributes for works-on, we may use ternary relationship.

10 Binary vs. Ternary Relationships If each policy is owned by just one employee: Key constraint on Policies would mean policy can only cover 1 dependent! (Arrow from Policies to Covers) Use two binary relationships to incorporate the constraint Bad design Better design age pname Covers Policies cost policyid Dependents Employees lot name ssn Dependents Beneficiary Purchaser Employees lot name ssn Policies cost policyid age pname

11 Binary vs Ternary Relationships When to use ternary relationship? Example: There are generic parts in the world, such as bolts or AA batteries. There are suppliers, such as Eveready. There are projects that need parts like bolts and batteries. A project leader may buys a particular quantity of a product from a particular supplier for given price. What if we use three binary relationships instead?

12 If we use binary instead of ternary: - “ProjectOwner buys from Eveready, Eveready supplies batteries, Project needs batteries” DOES NOT IMPLY “ProjectOwner buys batteries from Eveready”! - Also can not store quantity, price etc. In this case, it is better to use ternary relationship.

13 Homework Example:

14 Solution: Alternate solutions?

15 Relational Model - Basics

16 Relational Database: Definitions Relation : made up of 2 parts: Instance : a table, with rows and columns #rows = cardinality, #columns = arity / degree Schema : specifies name of relation, plus name and type of each column E.g. Students ( sid : string, name : string, login : string, age : integer, gpa : real) Can think of a relation as a set of rows or tuples (i.e., all rows are distinct) Relational database : a set of relations

17 Example:

18 Key Constraints Superkey: a set of attributes for which no two tuples have the same value - Every relation has at least one default superkey – the set of all its attributes A superkey can have redundant attributes, so a more useful concept is that of a KEY which has no redundancy Key satisfies theconstrains: Two tuples in the relation cannot have identical values for the attributes in the key It is a minimal superkey In general, a relation schema may have more than one key, in this case, each of the key is called a candidate key If a relation has several candidate keys, one is chosen to be the primary key.

19 Key Constraints Foreign key : Set of fields in one relation that is used to ‘ refer ’ to a tuple in another relation (Must correspond to primary key of the second relation.) If all foreign key constraints are enforced, referential integrity is achieved

20 Example: Consider ER Model consisting of entities Movies and Stars How to translate ER model to Relational Model? title year length id name StarMovies Stars-in filmType Character dateOfBirth

21 Entity set to relation Map each entity set to a relation, the key to primary key How would you define relations for entity sets Movies and Star ? - Assume, No two movies with same name have same year or length. title year length Movies filmType id name Star dateOfBirth

22 Entity set to relation Map each entity set to a relation, the key to primary key - Assume, No two movies with same name have same year or length. - (title, year) and (title, length) form candidate keys. CREATE TABLE Movies ( title CHAR(30), year INTEGER, length INTEGER, PRIMARY KEY ( title, year ), UNIQUE (title, length) ) CREATE TABLE Star ( id INTEGER, dateOfBirth CHAR(20), PRIMARY KEY ( name ) ) title year length Movies filmType id name Star dateOfBirth

23 Relationships to relations In translating a relationship set to a relation, attributes of the relation must include: Keys for each participating entity set (as foreign keys-explained later) This set of attributes forms a superkey for the relation All descriptive attributes How would you translate the relation Stars-In to a relation? What are the constraints? title year length id name StarMovies Stars-in filmType Character dateOfBirth

24 Relationships to relations Recall, Foreign key : Set of fields in one relation that is used to ‘ refer ’ to a tuple in another relation. CREATE TABLE Stars-In ( id INTEGER, title CHAR(30), year INTEGER, character CHAR(30), PRIMARY KEY ( id, title, year ) FOREIGN KEY ( id ) REFERENCES Star(id), FOREIGN KEY ( title ) REFERENCES Movies(title), FOREIGN KEY ( year ) REFERENCES Movies(year) ) title year length id name StarMovies Stars-in filmType Character dateOfBirth

25 Thank You!


Download ppt "ER Diagrams and Relational Model CS 174a (Winter 2015)"

Similar presentations


Ads by Google