1 / 35

Chapter 6 The Relational Data Model and the Relational Algebra

Chapter 6 The Relational Data Model and the Relational Algebra. 6.1. The Relational Data Model Concepts. The relational model of data is based on the concept of a relation A relation is a mathematical concept based on the ideas of sets

ludlow
Download Presentation

Chapter 6 The Relational Data Model and the Relational Algebra

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6The Relational Data Model and the Relational Algebra

  2. 6.1. The Relational Data Model Concepts • The relational model of data is based on the concept of a relation • A relation is a mathematical concept based on the ideas of sets • The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations • The model was first proposed by Dr. E.F. Codd of IBM in 1970

  3. RELATION: A table of values • A relation may be thought of as a set of rows • A relation may alternately be though of as a set of columns • Each row represents a fact that corresponds to a real-world entity or relationship • Each row has a value of an item or set of items that uniquely identifies that row in the table • Sometimes row-ids or sequential numbers are assigned to identify the rows in the table • Each column typically is called by its column name or column header or attribute name

  4. FORMAL DEFINITIONS • A relation may be defined in multiple ways • The schema of a relation: R (A1, A2, .....An) Relation schema R is defined over attributes A1, A2, .....An For Example - CUSTOMER (Cust-id, Cust-name, Address, Phone#) Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values. For example, the domain of Cust-id is 6 digit numbers

  5. A tuple is an ordered set of values • Each value is derived from an appropriate domain. • Each row in the CUSTOMER table may be referred to as a tuple in the table and would consist of four values. • <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000">is a tuple belonging to the CUSTOMER relation. • A relation may be regarded as a set of tuples (rows). • Columns in a table are also called attributes of the relation • A domain has a logical definition: e.g.,“USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S.

  6. A domain may have 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. E.g., Dates have various formats such as monthname, date, year or yyyy-mm-dd, or dd mm,yyyy etc • An attribute designates the role played by the domain. E.g., the domain Date may be used to define attributes “Invoice-date” and “Payment-date” • The relation is formed over the cartesian product of the sets; each set has values from a domain; that domain is used in a specific role which is conveyed by the attribute name

  7. For example, attribute Cust-name is defined over the domain of strings of 25 characters. The role these strings play in the CUSTOMER relation is that of the name of customers. • Formally, Given R(A1, A2, .........., An) r(R)  dom (A1) X dom (A2) X ....X dom(An) • R: schema of the relation • r of R: a specific "value" or population of R. • R is also called the intension of a relation • r is also called the extension of a relation

  8. Let S1 = {0,1} Let S2 = {a,b,c} Let R  S1 X S2 • Then for example: r(R) = {<0,a> , <0,b> , <1,c> } is one possible “state” or “population” or “extension” r of the relation R, defined over domains S1 and S2. It has three tuples

  9. DEFINITION SUMMARY Informal TermsFormal Terms TableRelation Column Attribute/Domain Row Tuple Values in a column Domain Table Definition Schema of a Relation Populated Table Extension

  10. CHARACTERISTICS OF RELATIONS • Ordering of tuples in a relation r(R): The tuples are not considered to be ordered, even though they appear to be in the tabular form. • Ordering of attributes in a relation schema R (and of values within each tuple): We will consider the attributes in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn> to be ordered . (However, a more general alternative definition of relation does not require this ordering). • Values in a tuple: All values are considered atomic (indivisible). A special null value is used to represent values that are unknown or inapplicable to certain tuples

  11. Notation: - We refer to component values of a tuple t by t[Ai] = vi (the value of attribute Ai for tuple t). Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of attributes Au, Av, ..., Aw, respectively

  12. 6.2. The Relational Constraints and Relational Database Schemas • Constraints are conditions that must hold on all valid relation instances. There are three main types of constraints: • Key constraints • Entity integrity constraints • Referential integrity constraints Key Constraints • Superkey of R: A set of attributes SK of R such that no two tuples in any valid relation instance r(R) will have the same value for SK. That is, for any distinct tuples t1 and t2 in r(R), t1[SK]  t2[SK]

  13. Key of R: A "minimal" superkey; that is, a superkey K such that removal of any attribute from K results in a set of attributes that is not a superkey. Example: The CAR relation schema: CAR(State, Reg#, SerialNo, Make, Model, Year) has two keys Key1 = {State, Reg#}, Key2 = {SerialNo}, which are also superkeys. {SerialNo, Make} is a superkey but not a key • If a relation has severalcandidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined

  14. Entity Integrity • Relational Database Schema: A set S of relation schemas that belong to the same database. S is the name of the database S = {R1, R2, ..., Rn} • Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R). This is because primary key values are used to identify the individual tuples t[PK]  null for any tuple t in r(R) • Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key

  15. Referential Integrity • A constraint involving two relations (the previous constraints involve a single relation) • Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation • 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 t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK] • A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2

  16. Referential Integrity Constraint Statement of the constraint The value in the foreign key column (or columns) FK of the the referencing relation R1 can be either: (1) a value of an existing primary key value of the corresponding primary key PK in the referenced relation R2,, or.. (2) a null. In case (2), the FK in R1 should not be a part of its own primary key.

  17. Other Types of Constraints • based on application semantics and cannot be expressed by the model per se • E.g., “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 allow for some of these

  18. 6.3.The Relational Operations • The basic set of operations for the relational model is known as the relational algebra • These operations enable a user to specify basic retrieval requests • The result of the retrieval is a new relation, which may have been formed from one or more relations • The algebra operations thus produce new relations, which can be further manipulated using operations of the same algebra • A sequence of relational algebra operations forms a relational algebraexpression, whose result will also be a relation that represents the result of adatabase query (or retrieval request)

  19. Relational algebra is a theoretical language with operations that work on one or more relations to define another relation without changing the original relation • The output from one operation can become the input to another operation (nesting is possible) • There are different basic operations that could be applied on relations on a database based on the requirement

  20. Selection (σ) Selects a subset of rows from a relation. • Projection (π) Deletes unwanted columns from a relation. • Cross-Product ( X ) Allows us to combine two relations. • Set-Difference ( - ) Tuples in relation1, but not in relation2. • Union (∪) Tuples in relation1 or in relation2. • Intersection (∩) Tuples in relation1 and in relation2 • Join Tuples joined from two relations • Using these we can build up sophisticated database queries

  21. The following is sample table used to illustrate different kinds of relational operations. The relation contains information about employees, IT skills they have and the school where they attend each skill. The primary key for this table is EmpId and Skill ID since a single employee can have multiple skills and a single skill be acquired by many employees. • School address is the address of a school for which the address of the main office will be considered in cases where a single school has many branches at different locations.

  22. Employee

  23. Selection • Selects subset of tuples/rows in a relation that satisfy selection condition. • Selection operation is a unary operator (it is applied to a single relation) • The Selection operation is applied to each tuple individually • The degree of the resulting relation is the same as the original relation but the cardinality (no. of tuples) is less than or equal to the original relation. • The Selection operator is commutative. • Set of conditions can be combined using Boolean operations (∩(AND), Ú(OR), and ~(NOT))

  24. No duplicates in result! • Schema of result identical to schema of (only) input relation. • Result relation can be the input for another relational algebra operation! (Operator composition.) • It is a filter that keeps only those tuples that satisfy a qualifying condition (those satisfying the condition are selected while others are discarded.) Notation: σ<selection condition>(relation name)

  25. Example: Find all Employees with skill type of Database σ< SkillType =”Database”> (Employee) This query will extract every tuple from a relation called Employee with all the attributes where the SkillType attribute with a value of “Database” The resulting relation will be the following

  26. If the query is all employees with a SkillType Database and School Unity the relational algebra operation and the resulting relation will be as follows σ<SkillType=”Database”AND School=”Unity”> (Employee)

  27. Projection • Selects certain attributes while discarding the other from the base relation • The PROJECT creates a vertical partitioning – one with the needed columns (attributes) containing results of the operation and other containing the discarded Columns • Deletes attributes that are not in projection list • Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation • Projection operator has to eliminate duplicates! • Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it • If the Primary Key is in the projection list, then duplication will not occur • Duplication removal is necessary to insure that the resulting table is also a relation

  28. Notation: π<Selected Attributes> (Relation Name) Example: To display Name, Skill, and Skill Level of an employee, the query andthe resulting relation will be: π<FName, LName, Skill, Skill_Level> (Employee)

  29. If we want to have the Name, Skill, and Skill Level of an employee with Skill SQL and SkillLevel greater than 5 the query will be: π<FName, LName, Skill, Skill_Level>(σ<Skill=”SQL” SkillLevel>5>(Employee))

  30. Cartesian Product (Cross Product) • This operation is used to combine tuples from two relations in a combinatorial fashion • That means, every tuple in Relation1(R) one will be related with every other tuple in Relation2 (S) • In general, the result of R(A1, A2, . . ., An) X S(B1,B2, . . ., Bm) is a relation Q with degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . .,Bm), in that order; Where R has n attributes and S has m attributes • The resulting relation Q has one tuple for each combination of tuples—one from R and one from S

  31. Hence, if R has n tuples, and S has m tuples, then | R x S | will have n* m tuples Example: Employee Dept

  32. Then the Cartesian product between Employee and Dept relations will be of the form: Employee X Dept:

  33. Basically, even though it is very important in query processing, the Cartesian Product is not useful by itself since it relates every tuple in the First Relation with every other tuple in the Second Relation • Thus, to make use of the Cartesian Product, one has to use it with the Selection Operation, which discriminate tuples of a relation by testing whether each will satisfy the selection condition • In our example, to extract employee information about managers of the departments (Managers of each department), the algebra query and the resulting relation will be

  34. π<ID, FName, LName, DeptName > (σ<ID=MangID>(Employee X Dept)) • sxd

More Related