Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Algebra. 1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN.

Similar presentations


Presentation on theme: "The Relational Algebra. 1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN."— Presentation transcript:

1 The Relational Algebra

2 1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN OPERATION 2 Binary Relational Operations *JOINJOIN *DIVISIONDIVISION

3 UNION Operation The result of this operation, denoted by R  S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. Union operation is a binary which means that it always operates on two relations. The notation for the union of relations(table) R &S is R  S Is Q We have two relations R&S, then the union of R &S as shown in fig.

4 IDNAME 501AMIT 503ANAND 504SONAL 506MADHU 507RAJNI 510KAPIL 512RANJIT IDNAME 503ANAND 504SONAL 506MADHU 510KAPIL IDNAME 501AMIT 503ANAND 504SONAL 507RAJNI 510KAPIL 512RANJIT R S R  S

5  Properties of Union Operation 1. The input relations must be union compatible. 2. Commutativity -It means that result of R  S is same as that of S  R. 3. Associativity- It means that P  Q  S)= (P  Q)  S Where P,Q and S are relations. BACK TO INDEX

6  INTERSECTION OPERATION The result of this operation, denoted by R  S, is a relation that includes all tuples that are in both R and S. The two relations used in intersection operation must be “union compatible’’ The intersection operation is a binary operation. The notation for the Intersection of relations(table) R &S is R  S Is Q We have two relations R&S, then the intersection of R &S as shown in fig.

7 IDNAME 503ANAND 504SONAL 510KAPIL IDNAME 503ANAND 504SONAL 506MADHU 510KAPIL IDNAME 501AMIT 503ANAND 504SONAL 507RAJNI 510KAPIL 512RANJIT R S Q=R  S

8  Properties of Intersection Operation 1. The input relations must be union compatible. 2. Commutativity -It means that result of R  S is same as that of S  R. 3. Associativity- It means that P  Q  S)= (P  Q)  S Where P,Q and S are relations. BACK TO INDEX

9  Set Difference (or MINUS) Operation The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two relations used in difference operation must be “union compatible’’ The Difference operation is a binary operation, which means that it always operates on two relations The notation for the Difference of relations(table) R &S is R  S Is Q

10 IDNAME 501AMIT 507RAJNI 512RANJIT IDNAME 503ANAND 504SONAL 506MADHU 510KAPIL IDNAME 501AMIT 503ANAND 504SONAL 507RAJNI 510KAPIL 512RANJIT R S Q=R  S

11  Properties of Difference Operation 1. The input relations must be union compatible. 2. They are not Commutative-It means that result of R - S is same as that of S - R. 3. They are not Associative- It means that P - ( Q - S) ≠ (P - Q) - S Where P,Q and S are relations. BACK TO INDEX

12 CARTESIAN (or cross product) Operation The Cartesian Product of two relations result in new relation that include concatenation of every tuple of first relation with every tuple of second relation. In other words, the new relation is created consisting of all possible combinations of the tuple. It is also known as the Cross Product or Cross Join. The Cartesian operation is a binary operation. Which means that it always operates on two relations. The notation for the Cartesian Product of relations(table) R &S is RXS Is Q

13 R.IDR.NAM E S.IDS.NAME 501AMIT503ANAND 501AMIT506MADHU 503ANAND503ANAND 503ANAND506MADHU 504SONAL503ANAND 504SONAL506MADHU IDNAME 503ANAND 506MADHU IDNAME 501AMIT 503ANAND 504SONAL R Q=RX  S S

14  Properties of Cartesian Product Operation 1. The relations on which Cartesian product operation is applied need not necessary to be union compatible. 2. Commutativity-It means that result of R X  S is same as that of S X  R. 3. Associativity- It means that P  X  Q  XS) = (P  X  Q)  XS Where P,Q and S are relations. 4. The resultant relation(table) may hold duplicate attributes if some attributes of two relations are defined on common domains. 5. The Degree of the resultant operation is equal to the sum of the degree of all the relations i.e. Degree of R= Degree of P + Degree of Q 6. The total number of rows in the resultant operation is equal to the product of the number of rows of the first and the second relation i.e. Total number of Tuples of R = Total number of Tuples of P X Total number of Tuples of Q. BACK TO INDEX

15  JOIN Operation ◦ The sequence of Cartesian product followed by select is used quite commonly to identify and select related tuples from two relations, a special operation, called JOIN. It is denoted by ∞ ◦ This operation is very important for any relational database with more than a single relation, because it allows us to process relationships among relations. ◦ The general form of a join operation on two relations R(A 1, A 2,..., A n ) and S(B 1, B 2,..., B m ) is: R∞ S where R and S can be any relations that result from general relational algebra expressions. Consider the example of EMP & DEPT table

16 EMP IDENAMESALARYDEPT ID 101ANU1200001 103PRIYA1500001 106ANKUR1300002 DEPT IDDNAME 01FINANCE 01MARKETING 02PACKING EMP IDENAMESALARYDEPT ID DNAME 101ANU1200001 FINANCE 103PRIYA1500001 MARKETING 106ANKUR1300002 PACKING EMP∞ dept_id=dept_no DEPT EMP DEPT

17 In the above example, the DEPT ID attribute of EMP table is matched with the DEPTNO attribute of DEPT table and if both are equal then the resultant tuples from both the tables are fetched as a single row The common attribute in both the tables should not have the same name so as to preserve the uniqueness of the column names in the resultant relation. If they have the same name then they should be renamed to preserve uniqueness. There is another join known as Natural Join in which there is no need of explicitly naming the columns. The join is Performed by joining all those col from the first table to any col in the second table with the same name The result of Natural Join in the above example is as shown

18 EMP IDENAMESALARYDEPT IDDNAME 101ANU1200001FINANCE 103PRIYA1500001MARKETING 106ANKUR1300002PACKING Natural Join on EMP & DEPT table The natural Join is also referred as Inner Join

19 The Another type of join in which relation is joined to it self by comparing the values with a col of a single relation is known as Self Join EMP IDENAMEMANG ID 101REKHA------- 103KAPIL101 104AMAR106 ATUL------- 107PUNEET1-1 ENAME KAPILREKHA AMARATUL PUNEETREKHA EMP THE RESULTANT RELATION THE RESULT OF SELF JOIN

20  Properties of Join Operation 1. Commutativity-It means that result of R ⋈  S is same as that of S ⋈  R. 2. Associativity- Using this the same rows appear in the final resuIt regardless of the order in which relations are joined. P  ⋈  Q  ⋈ S) = (P  ⋈ Q)  ⋈ S Where P,Q and S are relations. 3. We can combine a selection with a cross product to form a join. R ⋈  S=  (R X S) 4. We can commute a selection with a join. If the selection condition only involves attributes of one of the arguments to the join.  R ⋈  S )=  R) ⋈  S. The attributes must appear only in R and not in S. 5. Rows whose join attributes are null,don’t appear in the resultants relation(table). 6. More than two tables can also be joined but it increases the complexity. 7. Joins are most commonly used when there exists a relationship between tables i.e. where a join condition is based on a Primary key and Foreign key columns. BACK TO INDEX

21  DIVISION Operation The division operation results in a new relation such that every tuple appearing in the resultant relation must exist in the dividend relation in combination with the every tuple in divisor relation. It is binary operation which operates in two relations. The division is denoted by a symbol “÷” The notation for the division of two relations P & Q is P÷Q. where P is the dividend relation and Q is the divisor relation. Consider an example, in which we take two tables P and Q

22 N n1 n2 N n1 MN m1n1 m1n2 m2n1 m3n3 m4n1 m4n2 m1n3 N n1 n2 n3 Q - PQQQQ iii iiiiv M m1 m4 M m1 m2 m3 M m1 M m2 m3 m4 RRRR The resultant relation R obtained by P÷Q in each case when tuples of Q are different

23  Properties of Divide Operation 1. It is a binary operation as it operates on two relations(tables). 2. In the Operation P÷Q, the relation P is known as dividend relation and relation Q is known as a divisor relation. If the dividend relation has degree m+n and the divisor relation has a degree of n, then the resultant relation will have a degree m. 3. The division operation is suited to queries that include the phrase “for all”. 4. It is very rarely used in database applications. BACK TO INDEX

24 BACK TO INDEX


Download ppt "The Relational Algebra. 1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN."

Similar presentations


Ads by Google