Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing...

Click here to load reader

  • date post

    13-Jan-2016
  • Category

    Documents

  • view

    214
  • download

    1

Embed Size (px)

Transcript of Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing...

  • Sree Nilakanta

  • Developing Relational ModelsWhat is the relational model and what is a relational database?How are relational schemas and their keys defined?How are entity classes and entities represented in a relational model and database?How are attributes and attribute values represented in a relational model and database?How are relationship types and relational models represented in a relational model and database?How are weak entity classes represented in a relational model?How is inheritance represented in a relational model?

  • Basics of the Relational ModelThe relational model represents information in tables (called relations)Each table represents a set of entitiesEach column of a table represents the attribute values of the entitiesEach row of a table represents a single entityA database schema is a collection of table definitions (relation schemas)A relational database is a collection of tablesEach table stores objects for a single relation schema

  • Relation Schemas and KeysThe rows of a relational table are uniqueNo 2 rows have the same values for all of the attributesA key is a collection of attributes in whichNo 2 rows have the same values for all attributes in the keyEvery table must have a keyWhy?A relation schema is the specification of the structure of a tableName of the tableName and type of each attributeDeclaration of the keyA key declaration is a constraintA table is not allowed to have 2 different rows that have the same value for the keyDatabase systems enforce key constraints By blocking any attempt to modify a table that will result in a violation of the key constraint

  • Relation is not RelationshipBe careful of these two wordsRelationRelationshipA relation is a table that contains a set of entitiesA relationship is a connection between two entitiesWe must be very careful to use the correct wordListen closely to the lectures and correct me if I get it wrong!

  • Translating E-R DiagramsWe create a relational model from an E-R modelFor each component of the E-R diagram, create a representation in the relational modelThis chapter describes how to systematically translate an E-R model into a database schemaEntity classesSimple attributesComposite attributesKey attributesRelationship types of different cardinalitiesWeak entity classesMulti-valued attributesInheritance

  • Representing Entity ClassesFor each strong entity classes in your E-R model create a relation schema:Rule 1a: Define a relation schema by the same name.Rule 1b: For each single-valued attribute of the entity classcreate an attribute by the same name in the relation schema and specify a type for the attribute Rule 1c: Define the key of the new relation schema as the key of the entity class If the entity class key consists of multiple simple attributes, the key of the relation schema will be that set of attributes. Underline your selected key attribute in each schema in order to identify the key.

  • Composite AttributesRule 2. For each composite attribute of a strong entity classcreate an attribute in the relation schema for each component attributeIf appropriate, use the name of the composite attribute as a prefix for each of the component attribute names Schema: Customer (accountId string, lastName string, firstName string, street string, city string, state string, zipcode string, balance number)

  • Representing Relationship Types as AttributesA relationship type may be represented by attributes in a relation schemaAdd the key attributes of one table to the related tableConsider relationship type Video IsCopyOf MovieKey of Movie is movieIdAdd attribute movieId to table VideoSchema Video (videoId int, [other attributes omitted] movieId int references Movie)Attribute movieId of relation Video is called a foreign keyBecause its value is the value of the key of an entity in another (foreign) table. Each video is related to the movie with the same movieId

  • One-to-Many RelationshipTypesFor a one-to-many relationship typeAdd the key attributes of one entity class to the other entity class (foreign key attributes). Add the foreign key attributes to the class whose cardinality is 1Rule 3: For each one-to-many relationship type R between subject class S and target class Tadd the key attributes of class S to class T as foreign keysName the attributes using the role that S plays in relationship type RAdd the attributes of the relationship type R to target class T.

  • One-to-One Relationship TypesThe foreign key attributes may be added to either schemaEach entity class is to-one in the relationship typeChoose which class to include the foreign key attributesOne option is to try to minimize the number of null valuesRule 4: For each one-to-one relationship type between two classes, choose one class to be the subject and one to be the targetAdd the key attributes of the subject class to the target schema as foreign key attributesAdd the attributes of the relationship type to the target schema, just as in Rule 3

  • Many-to-Many Relationship TypesMany-to-Many relationship types between 2 classes cannot be represented as simple attributes in either related tableRule 5: For each many-to-many relationship type R between classes S and TCreate a new relation schema RAdd attributes to represent the key of S and the key of T as foreign key attributesThe key of schema R is the combination of those attributesAdd the relationship attributes to schema R, as in Rule 3Schema: WorksIn (ssn string references Employee, storeId number references Store)

  • Weak Entity ClassesWeak Entity classes have no keys of their ownCreate keys from Foreign keys of identifying relationship types Partial keys of the weak classRule 6: For each weak entity class WCreate a new relation schema with the same nameFor each identifying relationship, Add the key attributes of the related class to the new schema as foreign key attributesDeclare the key of the schema to be the combination ofthe foreign key attributes andThe partial key attributes of the weak entity class Add the simple and composite attributes of class W to the schema, as in Rules 2 and 3Relation Rental, shown at right, still must add foreign key for Customer

  • Multi-valued AttributesRepresent each multi-valued attribute as if it were a weak entity class with identifying relationship with owner classAll composite attributes of multi-valued attribute are partial keysRule 7: For each multi-valued attribute M of an entity class CDefine a new relation schema MAdd the components of attribute M to the new schemaAdd the key attributes of the schema that contains the other attributes of C to M as a foreign keyDefine the key of the new schema to be the combination of all of its attributes.The diagram below shows attribute otherUsers represented as a weak entity class and as a schemaDo not modify the E-R diagram. This diagram is included to illustrate the concept.

  • InheritanceFirst representation strategyRepresent each superclass and each subclass as individual tablesEach table has the attributes of the corresponding classEach subclass table has the key of the superclass as both key and foreign keyRule 8a: Create a relation schema for each superclass C using rules 1 and 2. For each subclass of C that has a defining attribute, add that attribute to the schema for C. For each subclass S, create a new relation schema. Add the simple and composite attributes of class S to the schema, as in Rules 1b and 2. Add the key of the superclass C as a foreign key referencing relation C. Declare the key of the subclass relation for S to be this foreign keys

  • Example of Rule 8a

  • Case in PointRelational Model for Video Sales for BigHit OnlineProcessEvaluate E-R model for BigHit OnlineFigure 4.25, repeated as Fig. 5.23Transform E-R diagram into relation schemasApply rules 1-8Evaluate relation schemas for clarity, accuracy and completeness

  • E-R Diagram for BigHit Online

  • Creating Relation SchemasAfter applying Rule 1, before adding relationship types and inheritanceCustomer: (accountId number, lastName string, firstName string)Sale: (saleId number, dateSold date, totalCost number)Movie: (movieId number, title string, genre string)ShoppingCart: (cartId number, dateCreated date) Adding composite attributes to SaleSale: (saleId number, dateSold date, totalCost number, creditCardType string, creditCardExpiration date, creditCardAccountNumber number) Adding relationship type Purchases to SaleSale: (saleId number, dateSold date, totalCost number, creditCardType string, creditCardExpiration date, creditCardAccountNumber number, accountId number references Customer)

  • Adding Relationship TypesAdd Selects to ShoppingCartShoppingCart: (cartId number, dateCreated date, accountId number references Customer) Add schemas for many-to-many relationship typesSaleItem: (saleId number references Sale, movieId references Movie, quantity number)CartItem: (cartId number references ShoppingCart, movieId references Movie, quantity number)Add schemas for multi-valued attributesCreditCards: (accountId number references Customer, accountNumber number, type string, expiration date)ShippingAddresses: (accountId number references Customer, street string, city string, state string, zipcode string)Add schemas for subclasses of MovieDVD: (movieId number references Movie, videoFormat string, languages string, captioning string, cost currency, quantity number)Videotape: (movieId number references Movie, format string, soundtrack string, cost currency, quantity number)

  • Resulting Database Schema for BigHit OnlineCustomer: (accountId number, lastName string, firstName string)Sale: (saleId number, dateSold date, totalCost number, creditCardType string, creditCardExpiration date, creditCardAccountNumber number, acc