Giáo trình SQL bài 6

58 199 0
Giáo trình SQL bài 6

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lecture Relational Database Design by ER- and EER-to-Relational Mapping Objectives • Relational Model concepts • Relational Model Constraints and Relational Database Schemas • Relational Database Design Using ER-to-Relational Mapping • Mapping EER Model Constructs to Relations • Reference: Chapter - Faculty of Science and Technology Database Fundamentals Relational Model Concepts • The relational Model of Data is based on the concept of a Relation The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations • We review the essentials of the formal relational model in this chapter • In practice, there is a standard model based on SQL – this is described in Chapters and • Note: There are several important differences between the formal model and the practical model, as we shall see Faculty of Science and Technology Database Fundamentals Relational Model Concepts • A Relation is a mathematical concept based on the ideas of sets • The model was first proposed by Dr E.F Codd of IBM Research in 1970 in the following paper: "A Relational Model for Large Shared Data Banks," Communications of the ACM, June 1970 • The above paper caused a major revolution in the field of database management and earned Dr Codd the coveted ACM Turing Award Faculty of Science and Technology Database Fundamentals Informal Definitions • Informally, a relation looks like a table of values • A relation typically contains a set of rows • The data elements in each row represent certain facts that correspond to a real-world entity or relationship In the formal model, rows are called tuples Each column has a column header that gives an indication of the meaning of the data items in that column In the formal model, the column header is called an attribute name (or just attribute) Faculty of Science and Technology Database Fundamentals Informal Definitions • Key of a Relation: Each row has a value of a data item (or set of items) that uniquely identifies that row in the table • Called the key In the STUDENT table, SSN is the key Sometimes row-ids or sequential numbers are assigned as keys to identify the rows in a table • Called artificial key or surrogate key Faculty of Science and Technology Database Fundamentals Formal Definitions - Schema • The Schema (or description) of a Relation: Denoted by R(A1, A2, .An) R is the name of the relation The attributes of the relation are A1, A2, , An • Example: CUSTOMER (Cust-id, Cust-name, Address, Phone#) CUSTOMER is the relation name Defined over the four attributes: Cust-id, Cust-name, Address, Phone# • Each attribute has a domain or a set of valid values For example, the domain of Cust-id is digit numbers Faculty of Science and Technology Database Fundamentals Formal Definitions - Tuple • A tuple is an ordered set of values (enclosed in angled brackets ‘< … >’) • Each value is derived from an appropriate domain • A row in the CUSTOMER relation is a 4-tuple and would consist of four values, for example: This is called a 4-tuple as it has values A tuple (row) in the CUSTOMER relation • A relation is a set of such tuples (rows) Faculty of Science and Technology Database Fundamentals Formal Definitions - Domain • A domain has a logical definition: Example: “USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S • A domain also has a data-type or a format defined for it The USA_phone_numbers may have a format: (ddd)ddd-dddd where each d is a decimal digit Dates have various formats such as year, month, date formatted as yyyy-mm-dd, or as dd mm,yyyy etc • The attribute name designates the role played by a domain in a relation: Used to interpret the meaning of the data elements corresponding to that attribute Example: The domain Date may be used to define two attributes named “Invoice-date” and “Payment-date” with different meanings Faculty of Science and Technology Database Fundamentals Formal Definitions - State • The relation state is a subset of the Cartesian product of the domains of its attributes each domain contains the set of all possible values the attribute can take • Example: attribute Cust-name is defined over the domain of character strings of maximum length 25 dom(Cust-name) is varchar(25) • The role these strings play in the CUSTOMER relation is that of the name of a customer Faculty of Science and Technology Database Fundamentals 10 ER-to-Relational Mapping Algorithm (5) • Step 5: Mapping of Binary M:N Relationship Types For each regular binary M:N relationship type R, create a new relation S to represent R Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types; their combination will form the primary key of S Also include any simple attributes of the M:N relationship type (or simple components of composite attributes) as attributes of S • Example: The M:N relationship type WORKS_ON from the ER diagram is mapped by creating a relation WORKS_ON in the relational database schema The primary keys of the PROJECT and EMPLOYEE relations are included as foreign keys in WORKS_ON and renamed PNO and ESSN, respectively The primary key of the WORKS_ON relation is the combination of the foreign key attributes {ESSN, PNO} Faculty of Science and Technology Database Fundamentals 44 ER-to-Relational Mapping Algorithm (6) • Step 6: Mapping of Multivalued attributes For each multivalued attribute A, create a new relation R This relation R will include an attribute corresponding to A, plus the primary key attribute K-as a foreign key in R-of the relation that represents the entity type of relationship type that has A as an attribute The primary key of R is the combination of A and K If the multivalued attribute is composite, we include its simple components • Example: The relation DEPT_LOCATIONS is created The attribute DLOCATION represents the multivalued attribute LOCATIONS of DEPARTMENT, while DNUMBER-as foreign keyrepresents the primary key of the DEPARTMENT relation The primary key of R is the combination of {DNUMBER, DLOCATION} Faculty of Science and Technology Database Fundamentals 45 ER-to-Relational Mapping Algorithm (7) • Step 7: Mapping of N-ary Relationship Types For each n-ary relationship type R, where n>2, create a new relationship S to represent R Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types Also include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S • Example: The relationship type SUPPY in the ER on the next slide This can be mapped to the relation SUPPLY shown in the relational schema, whose primary key is the combination of the three foreign keys {SNAME, PARTNO, PROJNAME} Faculty of Science and Technology Database Fundamentals 46 Mapping the n-ary relationship type SUPPLY Faculty of Science and Technology Database Fundamentals 47 Summary of Mapping constructs and constraints ER Model Entity type 1:1 or 1:N relationship type M:N relationship type n-ary relationship type Simple attribute Composite attribute Multivalued attribute Value set Key attribute Relational Model “Entity” relation Foreign key (or “relationship” relation) “Relationship” relation and two foreign keys “Relationship” relation and n foreign keys Attribute Set of simple component attributes Relation and foreign key Domain Primary (or secondary) key Correspondence between ER and Relational Models Faculty of Science and Technology Database Fundamentals 48 Mapping EER Model Constructs to Relations • Step8: Options for Mapping Specialization or Generalization Convert each specialization with m subclasses {S1, S2,….,Sm} and generalized superclass C, where the attributes of C are {k,a1,…an} and k is the (primary) key, into relational schemas using one of the four following options: • • • • Option 8A: Multiple relations-Superclass and subclasses Option 8B: Multiple relations-Subclass relations only Option 8C: Single relation with one type attribute Option 8D: Single relation with multiple type attributes Faculty of Science and Technology Database Fundamentals 49 Mapping EER Model Constructs to Relations (2) • Option 8A: Multiple relationsSuperclass and subclasses Faculty of Science and Technology Database Fundamentals 50 Mapping EER Model Constructs to Relations (3) • Option 8B: Multiple relations-Subclass relations only Faculty of Science and Technology Database Fundamentals 51 Mapping EER Model Constructs to Relations (4) • Option 8C: Single relation with one type attribute Faculty of Science and Technology Database Fundamentals 52 Mapping EER Model Constructs to Relations (5) • Option 8D: Single relation with multiple type attributes Faculty of Science and Technology Database Fundamentals 53 Mapping EER Model Constructs to Relations (6) • Mapping of Shared Subclasses (Multiple Inheritance) A shared subclass, such as STUDENT_ASSISTANT, is a subclass of several classes, indicating multiple inheritance These classes must all have the same key attribute; otherwise, the shared subclass would be modeled as a category We can apply any of the options discussed in Step to a shared subclass, subject to the restriction discussed in Step of the mapping algorithm Below both 8C and 8D are used for the shared class STUDENT_ASSISTANT Faculty of Science and Technology Database Fundamentals 54 Mapping EER Model Constructs to Relations (7) Faculty of Science and Technology Database Fundamentals 55 Mapping EER Model Constructs to Relations (8) • Step 9: Mapping of Union Types (Categories) For mapping a category whose defining superclass have different keys, it is customary to specify a new key attribute, called a surrogate key, when creating a relation to correspond to the category In the example below we can create a relation OWNER to correspond to the OWNER category and include any attributes of the category in this relation The primary key of the OWNER relation is the surrogate key, which we called OwnerId Faculty of Science and Technology Database Fundamentals 56 Mapping EER Model Constructs to Relations (9) Faculty of Science and Technology Database Fundamentals 57 Faculty of Science and Technology Database Fundamentals 58 [...]... based on application semantics and cannot be expressed by the model per se Example: “the max no of hours per employee for all projects he or she works on is 56 hrs per week” • A constraint specification language may have to be used to express these • SQL- 99 allows triggers and ASSERTIONS to express for some of these Faculty of Science and Technology Database Fundamentals 31 Populated database state • Each... is the name of the whole database schema S = {R1, R2, , Rn} R1, R2, …, Rn are the names of the individual relation schemas within the database S • Following slide shows a COMPANY database schema with 6 relation schemas Faculty of Science and Technology Database Fundamentals 23 Company Database Schema Faculty of Science and Technology Database Fundamentals 24 Entity Integrity • Entity Integrity: The... involve a single relation • Used to specify a relationship among tuples in two relations: The referencing relation and the referenced relation Faculty of Science and Technology Database Fundamentals 26 Referential Integrity (2) • Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2 A tuple... • Then each vi must be a value from dom(Ai) A special null value is used to represent values that are unknown or inapplicable to certain tuples Faculty of Science and Technology Database Fundamentals 16 Characteristics Of Relations • Notation: We refer to component values of a tuple t by: • t[Ai] or t.Ai • This is the value vi of attribute Ai for tuple t Similarly, t[Au, Av, , Aw] refers to the subtuple... references a primary key value that does not exist in the referenced relation Entity integrity: • if the primary key value is null in the new tuple Faculty of Science and Technology Database Fundamentals 36 ... employee for all projects he or she works on is 56 hrs per week” • A constraint specification language may have to be used to express these • SQL- 99 allows triggers and ASSERTIONS to express... Faculty of Science and Technology Database Fundamentals 44 ER-to-Relational Mapping Algorithm (6) • Step 6: Mapping of Multivalued attributes For each multivalued attribute A, create a new relation... unknown or inapplicable to certain tuples Faculty of Science and Technology Database Fundamentals 16 Characteristics Of Relations • Notation: We refer to component values of a tuple t by: • t[Ai]

Ngày đăng: 03/12/2015, 03:58

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan