Lecture 03 Intro SQL Relational Algebra
Lecture 03 Intro SQL Relational Algebra
T. M. Murali
What is SQL?
SQL = Structured Query Language (pronounced sequel ). Language for dening as well as querying data in an RDBMS. Primary mechanism for querying and modifying the data in an RDBMS. SQL is declarative:
Say what you want to accomplish, without specifying how. One of the main reasons for the commercial success of RDMBSs.
T. M. Murali
Relational algebra is a notation for specifying queries about the contents of relations. Relational algebra eases the task of reasoning about queries. Operations in relational algebra have counterparts in SQL. To process a query, a DBMS translates SQL into a notation similar to relational algebra.
T. M. Murali
What is an Algebra?
T. M. Murali
What is an Algebra?
T. M. Murali
What is an Algebra?
An algebra allows us to construct expressions by combining operands and expression using operators and has rules for reasoning about expressions.
a2 + 2 a b + b2 , (a + b)2 . R (R S), R S.
T. M. Murali
T. M. Murali
Projection
The projection operator produces from a relation R a new relation containing only some of Rs columns. To obtain a relation containing only the columns A1 , A2 , . . . , An of R RA A1 ,A2 ,...,An (R) SQL SELECT A1 , A2 , . . . , An FROM R;
T. M. Murali
Selection
The selection operator applied to a relation R produces a new relation with a subset of Rs tuples. The tuples in the resulting relation satisfy some condition C that involves the attributes of R. RA C (R) SQL SELECT FROM R WHERE C; The WHERE clause of an SQL command corresponds to ().
T. M. Murali
Syntax of C : similar to conditionals in programming languages. Values compared are constants and attributes of the relations mentioned in the FROM clause. We may apply usual arithmetic operators to numeric values before comparing them. RA Compare values using standard arithmetic operators. SQL Compare values using =, <>, <, >, <=, >=.
T. M. Murali
The union of two relations R and S is the set of tuples that are in R or in S or in both.
T. M. Murali
The union of two relations R and S is the set of tuples that are in R or in S or in both. R and S must have identical sets of attributes and the types of the attributes must be the same. The attributes of R and S must occur in the same order.
T. M. Murali
The union of two relations R and S is the set of tuples that are in R or in S or in both. R and S must have identical sets of attributes and the types of the attributes must be the same. The attributes of R and S must occur in the same order. RA R S SQL (SELECT * FROM R) UNION (SELECT * FROM S);
T. M. Murali
The intersection of two relations R and S is the set of tuples that are in both R and S. Same conditions hold on R and S as for the union operator. RA R S SQL (SELECT * FROM R) INTERSECT (SELECT * FROM S);
T. M. Murali
T. M. Murali
T. M. Murali
Cartesian Product
The Cartesian product (or cross-product or product) of two relations R and S is a the set of pairs that can be formed by pairing each tuple of R with each tuple of S.
The result is a relation whose schema is the schema for R followed by the schema for S. We rename attributes to avoid ambiguity or we prex attribute with the name of the relation it belongs to.
T. M. Murali
Theta-Join
The theta-join of two relations R and S is the set of tuples in the Cartesian product of R and S that satisfy some condition C . RA R S
C
T. M. Murali
Theta-Join
The theta-join of two relations R and S is the set of tuples in the Cartesian product of R and S that satisfy some condition C . RA R S
C
S=
T. M. Murali
Theta-Join
The theta-join of two relations R and S is the set of tuples in the Cartesian product of R and S that satisfy some condition C . RA R S
C
S = C (R S).
T. M. Murali
Natural Join
The natural join of two relations R and S is a set of pairs of tuples, one from R and one from S, that agree on whatever attributes are common to the schemas of R and S. The schema for the result contains the union of the attributes of R and S. Assume the schemas R(A, B, C ) and S(B, C , D). RA R S SQL SELECT R.A, R.B, R.C, S.D FROM R,S WHERE R.B = S.B AND R.C = S.C; A dangling tuple is one that fails to pair with any tuple in the other relation.
T. M. Murali
T. M. Murali
T. M. Murali
Set operations (R and S must have the same attributes, same attribute tyes, and same order of attributes):
union: R S and (R) UNION (S). intersection: R S and (R) INTERSECT (S). dierence: R S and (R) EXCEPT (S).
T. M. Murali
Set operations (R and S must have the same attributes, same attribute tyes, and same order of attributes):
union: R S and (R) UNION (S). intersection: R S and (R) INTERSECT (S). dierence: R S and (R) EXCEPT (S).
Natural join: R S; in SQL, list the conditions that the common attributes be equal in the WHERE clause.
T. M. Murali August 31, 2009 CS4604: SQL and Relational Algebra
Read Chapters 6.1.3-6.1.8 of the textbook for strings comparison, pattern matching, NULL and UNKNOWN values, dates and times, and ordering the output.
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
S = C (R S). S =??.
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
S = C (R S). S =??.
Suppose R and S share the attributes A1 , A2 , . . . An .
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
S = C (R S). S =??.
Suppose R and S share the attributes A1 , A2 , . . . An . Let L be the list of attributes in Rs schema followed by the list of attributes that are only in Ss schema.
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
S = C (R S). S =??.
Suppose R and S share the attributes A1 , A2 , . . . An . Let L be the list of attributes in Rs schema followed by the list of attributes that are only in Ss schema. Let C be the condition R.A1 = S.A1 AND R.A2 = S.A2 AND . . . AND R.An = S.An
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
S = C (R S). S =??.
Suppose R and S share the attributes A1 , A2 , . . . An . Let L be the list of attributes in Rs schema followed by the list of attributes that are only in Ss schema. Let C be the condition R.A1 = S.A1 AND R.A2 = S.A2 AND . . . AND R.An = S.An R S = L (C (R S))
T. M. Murali
Independence of Operators
The operators we have covered so far are: A,B (R), C (R), S(A1 ,A2 ) (R), R S, R S, R S, R S, R S, R S.
C
S = C (R S). S =??.
Suppose R and S share the attributes A1 , A2 , . . . An . Let L be the list of attributes in Rs schema followed by the list of attributes that are only in Ss schema. Let C be the condition R.A1 = S.A1 AND R.A2 = S.A2 AND . . . AND R.An = S.An R S = L (C (R S))
All other operators are independent, i.e., no operator can be written in terms of the others.
T. M. Murali August 31, 2009 CS4604: SQL and Relational Algebra
T. M. Murali
T. M. Murali
T. M. Murali
RA S(A1 ,A2 ,...,An ) (R): give R the name S; R has n attributes, which are called A1 , A2 , . . . , An in S. SQL Use the AS keyword in the FROM clause: Students AS Students1 renames Students to Students1. SQL Use the AS keyword in the SELECT clause to rename attributes.
T. M. Murali
Example of Renaming
Name pairs of students who live at the same address. RA S1.Name,S2.Name ( S1.Address = S2.Address (S1 (Students) S2 (Students))).
T. M. Murali
Example of Renaming
Name pairs of students who live at the same address. RA S1.Name,S2.Name ( S1.Address = S2.Address (S1 (Students) S2 (Students))). SQL SELECT S1.name, S2.name FROM Students AS S1, Students AS S2 WHERE S1.address = S2.address; Are these correct?
T. M. Murali
Example of Renaming
Name pairs of students who live at the same address. RA S1.Name,S2.Name ( S1.Address = S2.Address (S1 (Students) S2 (Students))). SQL SELECT S1.name, S2.name FROM Students AS S1, Students AS S2 WHERE S1.address = S2.address; Are these correct? No, the result includes tuples where a student is paired with himself/herself.
T. M. Murali
Example of Renaming
Name pairs of students who live at the same address. RA S1.Name,S2.Name ( S1.Address = S2.Address (S1 (Students) S2 (Students))). SQL SELECT S1.name, S2.name FROM Students AS S1, Students AS S2 WHERE S1.address = S2.address; Are these correct? No, the result includes tuples where a student is paired with himself/herself. Add the condition S1.name < S2.name.
T. M. Murali
T. M. Murali