CMP-3440 Database Systems Relational DB Languages Relational Algebra, Calculus, SQL Lecture 05 zain 1
Introduction Relational algebra & relational calculus are formal languages associated with the relational model. Informally: relational algebra is a (high-level) procedural language & relational calculus a non-procedural language. Formally both are equivalent to one another. A relationally complete language can perform all basic, meaningful operations on relations. Any relational language as powerful as relational algebra is called relationally complete. SQL is a superset of RA 2
Relational Algebra Relational algebra operations work on one or more relations to define another relation without changing the original relations. Both operands and results are relations, so output from one operation can become input to another operation. It is a set-oriented language in which all tuples, possibly from different relations, are manipulated in one statement without looping. Allows expressions to be nested, just as in arithmetic. This property is called closure. 3
Relational Algebra Operations 8 operations in relational algebra: 5 basic: Traditional Set Operations: Selection, Projection, Cartesian product, Union, & Set Difference. These perform most of the data retrieval operations needed. 3 additional: Derived Set operations: Intersection, Join & Division Derived operations can be expressed in terms of the above 5 basic operations 4
Relational Algebra Operations 5
Selection (sigma) σ predicate (R) Works on a single relation R and defines a relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate). Example List all staff with a salary greater than 10,000. σsalary > 10000 (Staff) 6
Projection (pie) Π col-1,..., col-n (R) Works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates. Example Produce a list of salaries for all staff, showing only staffno, fname, lname, and salary details. Π staffno, fname, lname, salary (Staff) 7
Union R S Union of two relations R and S defines a relation that contains all the tuples both R and S, duplicate tuples being eliminated. R and S must be union-compatible. Example List all cities where there is either a branch office or a property for rent. Π city (Branch) Π city (PropertyForRent) 8
Set Difference R - S Defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible Example List all cities where there is a branch office but no properties for rent Π city (Branch) Π city (PropertyForRent) 9
Cartesian Product R X S Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S Example List the names and comments of all clients who have viewed a property for rent. (ΠclientNo, fname, lname(client)) X (ΠclientNo,propertyNo,comment (Viewing)) 10
Intersection 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. Example List all cities where there is both a branch office and at least one property for rent. Π city (Branch) Π city (PropertyForRent) 11
Division R S Defines a relation over the attributes C that consists of set of tuples from R that match combination of every tuple in S Example Identify all clients who have viewed all properties with three rooms. (ΠclientNo,propertyNo(Viewing)) (ΠpropertyNo(σrooms = 3 (PropertyForRent))) 12
Join Operations Various forms of join operation Theta Join Equijoin Natural Join Outer Join Semi Join 13
Relational Calculus Relational Algebra provides a set of explicit operations (select, project, join, etc) that can be used to build some desired relation from the database Relational Calculus provides a notation for formulating the definition of that desired relation in terms of the relations in the database without explicitly stating the operations to be performed SQL is based on the relational calculus and algebra 14
SQL Structured Query Language Used for both Database Definition, Modification and Querying Basic language is standardized across relational DBMS s. Each system may have proprietary extensions to standard. Relational Calculus combines Restrict, Project and Join operations in a single command. SELECT. 15
Conditions used in SQL = equal to a particular value >= greater than or equal to a particular value > greater than a particular value <= less than or equal to a particular value <> not equal to a particular value LIKE *term* (may be other wild cards in other systems) IN ( opt1, opt2,, optn ) BETWEEN val1 AND val2 IS NULL 16
Aggregate Functions in SQL Count Avg SUM MAX MIN Many others are available in different systems 17