Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B,

Similar presentations


Presentation on theme: "+ Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B,"— Presentation transcript:

1 + Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B, the attributes in B must be included in the schema for A. Also, the result has attributes A-B. SALES(supId, prodId); PRODUCTS(prodId); Relations SALES and PRODUCTS must be built using projections. SALES/PRODUCTS: the ids of the suppliers supplying ALL products.

2 + Examples of Division A/B A B1 B2 B3 A/B1A/B2A/B3

3 + Housekeeping – Jan 25, 2013

4 + Today: Quick review of the various relational algebra concepts using small example tables Monday: work through exercises based on our running example of the transit scheme (I will finish off our first draft of the table scheme today) Goal: see if we can retrieve the information needed to support our scenarios

5 + Quiz Confusion (a.k.a. don’t try to remember how you wrote a quiz on less than 4 hours sleep) Q6. What is the role of a foreign key in a table a. Links two or more tables together b. Uniquely identifies each record in the table for which it is the foreign key c. Uniquely identifies each record in the table from which it originates d. A and C

6 + Key selection A candidate key is any set of one or more columns whose combined values are unique among all occurrences (i.e., tuples or rows). Since a null value is not guaranteed to be unique, no component of a candidate key is allowed to be null. There can be any number of candidate keys in a table The primary key of any table is any candidate key of that table which the database designer arbitrarily designates as "primary". The primary key may be selected for convenience, comprehension, performance, or any other reasons. The alternate keys of any table are simply those candidate keys which are not currently selected as the primary key. A foreign key is a set of one or more columns in any table (not necessarily a candidate key, let alone the primary key, of that table) which may hold the value(s) found in the primary key** column(s) of some other table. ** see slide 7

7 + Entity Integrity Constraint No primary key value can be null. Need to be able to identify individual tuples in the relation – if allowed to have a null value for the primary key, may not be able to distinguish between two or more tuples

8 + Referential Integrity Constraint Specified between two relations Used to maintain consistency among tuples of the two relations A tuple in one relation that refers to another relation must refer to an EXISTING TUPLE in that relation Foreign keys specify a referential integrity constraint between the two relation A set of attributes FK in relation schema R1 is a foreign key of R1 if: The attributes in FK have the same domain as the primary key attributes PK of another relation R2 (FK references R2) A value of FK in a tuple t1 of R1 either occurs as a value of PK for some tuple T=t2 in R2 or is null. E.g. if dept # is the foreign key of employee, an employee must either be assigned a value of the primary key in Dept or the value can be Null in employee if no department is currently assigned

9 + Does a foreign key have to link to a primary key? Slightly controversial. Remember that there could be several candidate keys from which a single primary key was chosen. To ensure that each record in the referencing table references exactly one record in the referenced table, the referenced column(s) in the referenced table must have a primary key constraint or have both unique and not-null constraints. Having a unique index is not sufficient. Under the hood: When you specify a primary key constraint for a table, the Database Engine enforces data uniqueness by automatically creating a unique index for the primary key columns. This index also permits fast access to data when the primary key is used in queries. If a primary key constraint is defined on more than one column, values may be duplicated within one column, but each combination of values from all the columns in the primary key constraint definition must be unique. More info on foreign key constraints (but remember, this is DB specific): http://msdn.microsoft.com/en-us/library/ms175464(v=sql.105).aspx

10 + Relational Algebra CSCI 2141 W 2013

11 + What is Relational Algebra? Relational Algebra is formal description of how relational database operates. It is a procedural query language, i.e. user must define both “ how ” and “ what ” to retrieve. It consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output.

12 + Introduction to Relational Algebra Introduced by E. F. Codd in 1970. Codd proposed such an algebra as a basis for database query languages.

13 + Relational Query Languages Formal: relational algebra, relational calculus, Datalog Actual: SQL, Quel, Query-by- Example (QBE) In ALL languages, a query is executed over a set of relations, get single relation as the result

14 + Terminology Relation - a set of tuples. Tuple - a collection of attributes which describe some real world entity. Attribute - a real world role played by a named domain. Domain - a set of atomic values. Set - a mathematical definition for a collection of objects which contains no duplicates.

15 + Algebra Operations Unary Operations - operate on one relation. These include select, project and rename operators. Binary Operations - operate on pairs of relations. These include union, set difference, division, cartesian product, equality join, natural join, join and semi-join operators.

16 + Select Operator The Select operator selects tuples that satisfies a predicate; e.g. retrieve the employees whose salary is 30,000 б Salary = 30,000 (Employee) Conditions in Selection: Simple Condition: (attribute)(comparison)(attribute) (attribute)(comparison)(constant) Comparison: =,≠,≤,≥, Condition: combination of simple conditions with AND, OR, NOT

17 Select Operator Example NameAgeWeight Harry3480 Sally2864 George2970 Helena54 Peter3480 Person б Age≥34 (Person) б Age=Weight (Person)

18 Select Operator Example NameAgeWeight Harry3480 Sally2864 George2970 Helena54 Peter3480 NameAgeWeight Harry3480 Helena54 Peter3480 NameAgeWeight Helena54 Person б Age≥34 (Person) б Age=Weight (Person)

19 + Project Operator Project (∏) retrieves a column. Duplication is not permitted. e.g., name of employees: ∏ name (Employee) e.g., name of employees earning more than 80,000: ∏ name ( б Salary>80,000 (Employee))

20 Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Employee ∏ name (Employee)

21 Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Name Harry Sally George Helena Peter Employee ∏ name (Employee)

22 Project Operator Example: ∏ name ( б Salary>80,000 (Employee)) NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Employee б Salary>80,000 (Employee) ∏ name (б Salary>80,000 (Employee))

23 Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Name Sally NameAgeSalary Sally2890,000 Employee б Salary>80,000 (Employee) ∏ name (б Salary>80,000 (Employee))

24 + Cartesian Product In mathematics, it is a set of all pairs of elements (x, y) that can be constructed from given sets, X and Y, such that x belongs to X and y to Y. It defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S.

25 + Cartesian Product Note: Relation schema is the union of schemas for R and S Resulting schema may be ambiguous Use R.A or S.A to disambiguate an attribute that occurs in both schemas

26 Cartesian Product Example NameAgeWeight Harry3480 Sally2864 George2970 City San Jose Austin Person City Person X City

27 Cartesian Product Example NameAgeWeight Harry3480 Sally2864 George2970 City San Jose Austin NameAgeWeightCity Harry3480San Jose Sally2864San Jose George2970San Jose Harry3480Austin Sally2864Austin George2970Austin Person City Person X City

28 + Example AB 12 34 BC 25 47 D 6 8 91011 x RS

29 + Example AB 12 34 BC 25 47 D 6 8 91011 x AR.BS.BCD RS 12256 12478 1291011 34 34 34 256 478 91011

30 + Rename Operator In relational algebra, a rename is a unary operation written as ρ a / b (R) where: a and b are attribute names R is a relation The result is identical to R except that the b field in all tuples is renamed to an a field. Example, rename operator changes the name of its input table to its subscript, ρ EmployeeName / Name (Employee) Changes the Name field of the Employee table to EmployeeName

31 Rename Operator Example NameSalary Harry80,000 Sally90,000 George70,000 Helena54,280 Peter40,000 EmployeeNameSalary Harry80,000 Sally90,000 George70,000 Helena54,280 Peter40,000 Employee ρ EmployeeName / Name (Employee)

32 + Union Operator The union operation is denoted U as in set theory. It returns the union (set union) of two compatible relations. For a union operation r U s to be legal, we require that, r and s must have the same number of attributes. The domains of the corresponding attributes must be the same. As in all set operations, duplicates are eliminated.

33 Union Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang JohnSmith RicardoBrown FrancisJohnson Student Professor Student U Professor

34 + Intersection Operator Denoted as . For relations R and S, intersection is R  S. Defines a relation consisting of the set of all tuples that are in both R and S. R and S must be union-compatible.

35 Intersection Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah FNLN SusanYao RameshShah Student Professor Student  Professor

36 + Set Difference Operator For relations R and S, Set difference R - S, defines a relation consisting of the tuples that are in relation R, but not in S. Set difference S – R, defines a relation consisting of the tuples that are in relation S, but not in R. Note: R-S  S-R!

37 Set Difference Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah Student Professor Student - Professor Professor - Student

38 Set Difference Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah FNLN BarbaraJones AmyFord JimmyWang Student Professor Student - Professor FNLN JohnSmith RicardoBrown FrancisJohnson Professor - Student

39 + Try R  S, R  S, R - S nameaddressgenderbirthdate Carrie Fisher Mark Hamil 123 Maple St., Hollywood 456 Oak Rd., Brentwood F M 9/9/99 8/8/88 R nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 S

40 + Sample Operations nameaddressgenderbirthdate Carrie Fisher123 Maple St., HollywoodF 9/9/99 R  S nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 R  S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88 nameaddressgenderbirthdate R - S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88

41 + Natural Join Operator Natural join is a dyadic operator that is written as R lXl S where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names. If r and s from r(R) and s(S) are successfully paired, result is called a joined tuple

42 Natural Join Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr FinanceGeorge SalesHarriet ProductionCharles NameEmpIDDeptNameMgr Harry3415FinanceGeorge Sally2241SalesHarriet George3401FinanceGeorge Harriet2202SalesHarriet Employee Dept Employee lXl Dept For an example, consider the tables Employee and Dept and their natural join:

43 + Example AB 12 34 BC 25 47 D 6 8 91011 join Resulting schema has attributes from R, either R or S (i.e., joining attribute(s)), and S Tuples that fail to pair with any tuple of the other relation are called dangling tuples RS

44 + Example AB 12 34 BC 25 47 D 6 8 91011 join ABCD RS 1256 3478

45 + Join Operations Theta Join (binary) R join C S, where C is an arbitrary join condition Step 1: take the product of R and S Step 2: Select from the product only those tuples that satisfy condition C As with the product operation, the schema for the result is the union of the schemas of R and S

46 + Example BC 23 23 D 4 5 7810 join A<D AND U.B  V.B AB 12 67 C 3 8 978 UV

47 + Example BC 23 23 D 4 5 7810 join A<D AND U.B  V.B AB 12 67 C 3 8 978 UV V.BV.CDAU.BU.C 1237810

48 + Additional slides w/ other types of joins For more information of the various relational algebra operations, see: http://en.wikipedia.org/wiki/Relational_algeb ra

49 + Semijoin Operator The semijoin is joining similar to the natural join and written as R ⋉ S where R and S are relations. The result of the semijoin is only the set of all tuples in R for which there is a tuple in S that is equal on their common attribute names.

50 Semijoin Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet ProductionCharles NameEmpIDDeptName Sally2241Sales Harriet2202Sales Employee Dept Employee ⋉ Dept For an example consider the tables Employee and Dept and their semi join:

51 + Outerjoin Operator Left outer join The left outer join is written as R =X S where R and S are relations. The result of the left outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in R that have no matching tuples in S. Right outer join The right outer join is written as R X= S where R and S are relations. The result of the right outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R.

52 Left Outerjoin Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet NameEmpIDDeptNameMgr Harry3415Financeω Sally2241SalesHarriet George3401Financeω Harriet2202SalesHarriet Employee Dept Employee =X Dept For an example consider the tables Employee and Dept and their left outer join:

53 Right Outerjoin Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet ProductionCharles NameEmpIDDeptNameMgr Sally2241SalesHarriet 2202SalesHarriet ωωProductionCharles Employee Dept Employee X= Dept For an example consider the tables Employee and Dept and their right outer join:

54 Full Outer join Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet ProductionCharles NameEmpIDDeptNameMgr Harry3415Financeω Sally2241SalesHarriet George3401Financeω Harriet2202SalesHarriet ωωProductionCharles Employee Dept Employee =X= Dept The outer join or full outer join in effect combines the results of the left and right outer joins. For an example consider the tables Employee and Dept and their full outer join:

55 + References http://en.wikipedia.org/wiki/Relational_algebra#Outer_join http://www.cs.sjsu.edu/faculty/lee/cs157/cs157alecturenote s.htm http://www.cs.sjsu.edu/faculty/lee/cs157/cs157alecturenote s.htm Database System Concepts, 5 th edition, Silberschatz, Korth, Sudarshan


Download ppt "+ Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B,"

Similar presentations


Ads by Google