Presentation is loading. Please wait.

Presentation is loading. Please wait.

15/1/20091 Lecture 2 on Relational Algebra This lecture introduces relational data model with relational algebra as its mathematical foundation Relational.

Similar presentations


Presentation on theme: "15/1/20091 Lecture 2 on Relational Algebra This lecture introduces relational data model with relational algebra as its mathematical foundation Relational."— Presentation transcript:

1 15/1/20091 Lecture 2 on Relational Algebra This lecture introduces relational data model with relational algebra as its mathematical foundation Relational operations can be derived from the set operations of Select, Project, Join, Semi-join, Union, Intersect, Difference, Natural Join, Natural Semi-join, Outer Join and Cartesian Product.

2 15/1/20092 Relational database A relational database has been defined as a collection of tables. The major advantages of the relational approach are its simplicity and generality as follows: An interface for a high-level, non-procedural data language Efficient file structures store the database An efficient optimizer to help meet the response-time requirements User views and snapshots of the stored database. Integrity control – validation of semantic constraints on the database Concurrence control – synchronization updates to a shared database by multiple users Selective access control – authorization of access privileges to one user’s database Recovery from both soft and hard crashes. A report generator for a display of the results of interactions against the database.

3 15/1/20093 Relational model Relational systems are based on an underlying set of theoretical ideas known as the relational model. The relational model can be characterized as a way of looking at data. It is concerned with three aspects of data: data structure, data integrity, and data manipulation.

4 15/1/20094 Relational data structure Relation – may be seen as a table or record type. Attribute – All values that occur in a specific field type, or column. Tuple – A row or record occurrence of a table Domain – A set of possible values for some attributes. Primary key – Record identifier for uniquely identifying rows in a relation without null value. Candidate key – Any set of attributes that could be chosen as a key of a relation. Composite key – A primary key consisting of more than one attribute. Foreign key – A set of attributes in one relation that constitute a key in some other relation; used to indicate logical links between relations. Domain – domains are pools of values, from which the actual values appearing in attributes are drawn.

5 15/1/20095 Relations Relation may be seen as a table. A table provides a natural mechanism for conveying information in a compact form. In a table, there is a number of column, one for each attribute of the objects described. Each entry in the table is a row containing values for each attribute, i.e. a tuple.

6 15/1/20096 S#SNAMESTATUSCITY S1Smith20London S2Jones10Paris S3Blake30Paris S4Clark20London S5Adams30Athens P#PNAMECOLORWEIGHTCITY P1NutRed12London P2BoltGreen17Paris P3ScrewBlue17Rome P4ScrewRed14London P5CamBlue12Paris P6CogRed19London S#P#QTY S1P1300 S1P2200 S1P3400 S1P4200 S1P5100 S1P6100 S2P1300 S2P2400 S3P2200 S4P2200 S4P4300 S4P5400 S P SP The Suppliers-and-parts database (sample values)

7 15/1/20097 Relational Schema (Data Definition Language) Create Table – create relation as a relational schema. Create View – create virtual (read only) relation Create Index – create indexes part of relation Alter Table – change relation structure Drop Table – delete relation from relational schema Drop View – delete virtual relation Drop Index – delete indexes part of relation

8 15/1/20098 Relational Operations Select – exacts specified tuples from a specific relation. Project – exacts specified attributes from a specific relation. Join – builds a relation from 2 specified relations consisting of all possible concatenated pairs of tuples such that the two tuples satisfy some specified condition. Divide – takes two relations, one binary and one unary, and builds a relation consisting of all values of one attribute of the binary relation that match all values in the unary relation.

9 15/1/20099 Natural Join – A special case of the join operation R NJN S is an equijoin in which equalities are specified on all fields having the same name in relation R and relation S. Semi Join – A special case of the join operation R SJ S is a projection of all fields (attributes) of the first relation operand after a join operation of relation R and relation S. Natural Semi Join - A special case of the natural join operation R NSJ S is a projection of all fields (attributes) of the first relation operand after a natural join operation of relation R and relation S.

10 15/1/200910 Mathematical Set Operations Union – builds a relation consisting of all tuples appearing in either of both of two specified relations. Intersection – builds a relation consisting of all tuples appearing in both of two specified relations. Difference – builds a relation consisting of all tuples appearing in the first and not the second of two specified relations. Cartesian product – builds a relation from 2 specified relations consisting of all possible concatenated pairs of tuples, one from each of the two specified relations.

11 15/1/200911 Overview of Relational Algebra

12 26/1/200912 Operations of Relational Algebra

13 15/1/200913 Formal specification of relational operations Select – Selection is called theta-selection in which theta represent any valid scalar comparison operator. For example, R where R.X theta R.Y where R is a relation. X and Y are the attributes of R, or constant values which must be on the same domain. Theta is a valid scalar comparison operator such as =, , >, or < etc. Relational algebra Form:SL F R where SL means select operation, F means formula and R is the operand relation. For instance, SL City=‘London’ S Result S#SNAMESTATUSCITY S1Smith20London S4Clark20London

14 15/1/200914 Project – the projection operator yields a “vertical” subset of a given relation. For example, R[X,Y…Z] where R is a relation and X Y..Z are attributes of R. Relational algebra form:PJ Attr R where PJ is project operation, attr is the attributes to be projects and Relation R is the operand For instance, PJ S#, Sname, Status, City S Result S#SNAMESTATUSCITY S1Smith20London S2Jones10Paris S3Blake30Paris S4Clark20London S5Adams30Athens

15 26/1/200915 Join – the Join operation is called theta-join which is not a primitive operation. If theta is a formula, then it is a join operation based on the selection criteria of the formula and then perform the cartesian product. Relational algebra form:RJN F S where JN is a join operation, F is the join formula and relations R and S are operands For instance, S JN S=S1 or S=S2 or S=S3 SP Result

16 15/1/200916 Semi Join – the Join operation is called theta-join which is not a primitive operation. If theta is a formula, then it is a join operation based on the selection criteria of the formula and then perform the cartesian product with the result including the attributes of first operand only. Relational algebra form:RSJ F S where SJ F is semi join operation, F is the join formula and relations R and S are operands For instance, S SJ S=S1 or S=S2 or S=S3 SP Result S#SNAMESTATUSCITY S1Smith20London S2Jones10Paris S3Blake30Paris

17 15/1/200917 Natural Join – the Join operation is called theta-join which is not a primitive operation. If theta is equality, the theta-join is an equijoin. The result of an equijoin must include 2 identical attributes. If one of those two attributes is eliminated, it is a natural join. Relational algebra form:RNJNS where NJN is natural join operation, and relations R and S are operands For instance, S NJN SP Result S.S#S.SnameS.StatusS.CitySP.P#SP.QTY S1Smith20LondonP1300 S1Smith20LondonP2200 S1Smith20LondonP3400 S1Smith20LondonP4200 S1Smith20LondonP5100 S1Smith20LondonP6100 S2Jones10ParisP1300 S2Jones10ParisP2400 S3Blake30ParisP2200 S4Clark20LondonP2200 S4Clark20LondonP4300 S4Clark20LondonP5400

18 19/1/200918 Natural Semi Join – the Join operation is called theta-join which is not a primitive operation. If theta is equality, the theta-join is an equijoin. The result of an equijoin must include 2 identical attributes. If one of those two attributes is eliminated, and result are attributes of first operand only, then it is a natural semi join. Relational algebra form:RNSJS where NSJ is natural semi join operation, and relations R and S are operands For instance, S NSJ SP Result S#SNAMESTATUSCITY S1Smith20London S2Jones10Paris S3Blake30Paris S4Clark20London

19 19/1/200919 Divide – the division operator divides a dividend relation A of degree m by a divisor relation B of degree n and produces a quotient relation of degree m. (Dividend ÷ Divisor = Quotient) Relational algebra form:ADI B Attr where DI is divide operation, attr are attributes of divisor and relations A and B are operands. For instance:SP DI DOR S# Given SP DI DOR => Result S# S1 S2 P# P1 P2 S#P#QTY S1P1300 S1P2200 S1P3400 S1P4200 S1P5100 S1P6100 S2P1300 S2P2400 S3P2200 S4P2200 S4P4300 S4P5400

20 15/1/200920 Union –For all set operations, the 2 relations must be union compatible of the same degree and domain. Union of two (union-compatible) relations is the set of all tuples belonging to either one or both. Relational algebra form:AUNB where UN is union operation, relations A and B are operands For instance:J UNHS Result S#NAMEMAJOR 123JONESHISTORY 158PARKSMATH 271SMITHHISTORY NUMNAMEINTEREST 105ANDERSONMANAGEMENT 123JONESHISTORY S#/NumNameMajor/Interest 123JonesHistory 158ParksMath 271SmithHistory 105AndersonManagement J HS S#CNAMEPOS# 123H3501 105BA4903 123BA4907 E

21 15/1/200921 Intersection – intersection is a set operation. Intersection of 2 compatible relations is the set of all tuples belonging to both relations. The result has all the tuples that occur in both relations. Relational algebra form:A INB where IN is intersection operation, relations A and B are operands For instance:J IN HS Result: S#/NumNameMajor/Interest 123JonesHistory

22 15/1/200922 Difference – difference is a set operation. Difference between 2 compatible relations is the set of all tuples belonging to one relation and not to another. Relational algebra form:ADFB where DF is difference operation, relations A and B are operands. For instance,J DF HS Result: S#/NumNameMajor/Interest 158ParksMath 271SmithHistory

23 15/1/200923 Extended Cartesian Product – it is a set operation. The extended Cartesian Product of 2 relations is the set of all tuples such that the set is the concatenation of a tuple belonging to one relation and a tuple belonging to another. Relational algebra form:A CP B where CP is Cartesian product and relations A and B are operands. For instance: J CP E Result: s1NameMajorSnumCnamePnum 123JonesHistory123H3501 123JonesHistory105BA4903 123JonesHistory123BA4907 158ParksMath123H3501 158ParksMath105BA4903 158ParksMath123BA4907 271SmithHistory123H3501 271SmithHistory105BA4903 271SmithHistory123BA4907

24 15/1/200924 TF TFD FD Outer Join Some tuples of two tables may not match their values according to join condition, which results in null value and is called Outer Join. In the following example: TF outer Join FD Into TFD Teaching AssistantFaculty Member JohnVincent MaryVincent TimTom Faculty memberDepartment VincentCS TomMath ChowMath Teaching AssistantFaculty MemberDepartment JohnVincentCS MaryVincentCS TimTomMath Null ChowMath

25 15/1/200925 Outer Union The outer union operation is to take the union of two relations if the relations are not union compatible, then the tuples that have no values for these attributes are padded with null values.

26 15/1/200926 Example on Outer Union

27 15/1/200927 Referential integrity A child relation must have a foreign key referring to the primary key of its parent table. In creation, a parent relation must be created first before child relation. In deletion, a child relation must be deleted first before parent relation. For example, a department has many employees. Relation department is a parent relation and relation employee is a child relation which has a foreign key referring to its parent relation.

28 15/1/200928 Parent Relation Department (Dept_name,….) Child Relation Employee (EMP_id, ….*Dept_name) Insert parent relation Department tuple before inserting correspondent child relation Employee tuple. Delete child relation Employee tuple before deleting correspondent parent relation Department tuple. Referential integrity

29 15/1/200929 Entity integrity When creating a relation, the primary key value must be unique and cannot be null value. For example, when creating a relation student, Relation Student (Student_id, address) Where the student id is used as a primary key and cannot be null value.

30 15/1/200930 Relational algebra expression Relational algebra execution sequence is from right to left with parenthesis: For example, given a relation S (supplier) and relation SP (supplier parts), where are the cities of the supplier who supply more than 300 parts? Perform the following relational algebra: PJ CITY (S NJN (SL QTY > 300 SP) ) Processing sequence 1. SL QTY > 300 SP 2. S NJN SP 3. PJ CITY S The result isCITY London Paris

31 15/1/200931 Operator tree

32 15/1/200932 Lecture summary All relational database operations can be expressed in relational algebra. By using set theory, we can optimize these operations. In general, reducing the operands size as early as possible is a way for efficient processing of relational operations. For example, with same operand relations, the size of the resultant semi-join operation tuples is smaller of the size of the resultant join operation tuples.

33 15/1/200933 Review Question What are the major differences among Join, Semi Join, Natural Join and Natural Semi Join operations in terms of resultant attributes? What is the ranking in sequence of efficiency of these four operations and why?

34 15/1/200934 Tutorial Question Given the following relations: and the nine elementary operations of relational algebra: Selection (SL), projection (PJ), union (UN), cartesian product (CP), join (JN), natural join (NJN), semi-join (SJ), natural semi-join (NSJ) and difference (DI). Use no more than Three of the nine elementary operations to implement the following query operation: “Find the employee#, responsibility and duration of all employees who are assigned to work on job under project Database Development”. (a) Show the three elementary operations in a relational algebra (b) Show the three elementary operations in an operator tree

35 15/1/200935 Reading Assignment Pages 167-195 of Chapter 6 Relational Algebra and Calculus of “Fundamentals of Database Systems” 5 th editon, by Elmasri & Navathe, Pearson, 2007.


Download ppt "15/1/20091 Lecture 2 on Relational Algebra This lecture introduces relational data model with relational algebra as its mathematical foundation Relational."

Similar presentations


Ads by Google