Presentation is loading. Please wait.

Presentation is loading. Please wait.

CG096 Advanced Database Technologies Lecture 6 Relational Algebra and SQL.

Similar presentations


Presentation on theme: "CG096 Advanced Database Technologies Lecture 6 Relational Algebra and SQL."— Presentation transcript:

1 CG096 Advanced Database Technologies Lecture 6 Relational Algebra and SQL

2 CG096 Advanced Database Technologies Lecture 6: Relational Languages 2 Content 1 Models, Languages and their Use in databases 2 The Relational Algebra 3 The Relational Calculus 4 Implementation of relational algebraic operations in SQL

3 CG096 Advanced Database Technologies Lecture 6: Relational Languages 3 1. Models, Languages and their Use Data model and Data Specification: studying global properties of the data and the operations for processing it Example: Consistency of the query, completeness of the answer Database and Data Processing: processing of particular piece of data stored within the database using particular operation Example: Memory storage for the result, complexity of the query Information system and Data Communication: sending prescriptions for data processing from a particular point in space to the database Example: Access to the data, Security of the operation specification

4 CG096 Advanced Database Technologies Lecture 6: Relational Languages 4 1.1 Relational and other data models Relational models (relational algebra, relational calculus) – most of the contemporary RDBMS are based on them Tree models (hierarchical, object-relational) – both legacy systems and new systems use them Object-oriented models (ODMG) – recent development, still not widely employed Note: XML native databases have some similarities with the hierarchical database systems (legacy systems), but they have more elaborated model and query languages, which are close to OQL (the standard query language of object- oriented databases)

5 CG096 Advanced Database Technologies Lecture 6: Relational Languages 5 1.2 Relational Languages and their Use Data Manipulation Language (DML) Use: Populates, updates, and queries relational DB Example: relational algebra, SQL DML Data Definition Language (DDL) Use: Specifies the data structures and defines the relational schema Example: domain calculus, SQL DDL Data Control Language (DCL) Use: Specifies operation permissions, resource access discipline and user profiles Example: SQL DCL, LDAP Note: Contemporary relational languages often incorporate some object- relational features of the model – e.g. Oracle 8i SQL has types, Oracle 9i SQL has type inheritance

6 CG096 Advanced Database Technologies Lecture 6: Relational Languages 6 1.3 Can DB live without formal model? The answer is NO for several reasons: as we will see, SQL has ambiguities, while the relational algebra is unambiguous – so it can provide semantic interpretation for SQL Moreover, because of the same reason SQL cannot be executed directly, it needs to be translated into a realistic structure of operations first, which can be interpreted then Finally, if we want to control the execution of the SQL statements, we need to know how it works

7 CG096 Advanced Database Technologies Lecture 6: Relational Languages 7 2 The Relational Algebra Proposed by Codd in 1970 as a formal data model. Describes the relations and the operations to manipulate relations Relational operations in relational algebra transform either a single relation (unary operation), or a pair (binary operation) into another relation Can be also used to specify retrieval requests (queries). Query result is also in the form of a relation. Relational Operations: RESTRICT (  ) and PROJECT (  ) operations. Set operations: UNION (  ), INTERSECTION (  ), DIFFERENCE (—), CARTESIAN PRODUCT (  ). JOIN operations ( ⋈). Other relational operations: DIVISION, OUTER JOIN, AGGREGATE FUNCTIONS.

8 CG096 Advanced Database Technologies Lecture 6: Relational Languages 8

9 9 2.1 RESTRICT  RESTRICT operation (called also SELECT - denoted by  ): Selects the tuples (rows) from a relation R that satisfy a certain selection condition c on the attributes of R :  c (R) Resulting relation includes each tuple in R whose attribute values satisfy c, i.e. it has the same attributes as R Examples:  DNO=4 (EMPLOYEE)  SALARY>30000 (EMPLOYEE)  (DNO=4 AND SALARY>25000) OR DNO=5 (EMPLOYEE)

10 CG096 Advanced Database Technologies Lecture 6: Relational Languages 10 PROJECT operation (denoted by  ): Keeps only certain attributes (columns) from a relation R specified in an attribute list L:  L (R) Resulting relation has only those attributes of R specified in L Example:  FNAME,LNAME,SALARY (EMPLOYEE) The PROJECT operation eliminates duplicate tuples in the resulting relation so that it remains a true set (no duplicate elements) Example:  SEX,SALARY (EMPLOYEE) If several male employees have salary 30000, only a single tuple is kept in the resulting relation. 2.2 PROJECT 

11 CG096 Advanced Database Technologies Lecture 6: Relational Languages 11

12 CG096 Advanced Database Technologies Lecture 6: Relational Languages 12 2.3 Combining Operations Because of closure, several operations can be combined to form a relational algebra expression. For example, the names and salaries of employees in Department 4:  FNAME,LNAME,SALARY (  DNO=4 (EMPLOYEE)) Alternatively, we could specify explicit intermediate relations for each step: TEMP   DNO=4 (EMPLOYEE) R   FNAME,LNAME,SALARY (TEMP) Attributes can optionally be renamed in a left-hand-side relation (this may be required for some operations that will be presented later), e.g. R ( FIRSTNAME,LASTNAME,SALARY )   FNAME,LNAME,SALARY (TEMP)

13 CG096 Advanced Database Technologies Lecture 6: Relational Languages 13

14 CG096 Advanced Database Technologies Lecture 6: Relational Languages 14 2.4 Set Operations Binary operations from set theory: UNION: R 1  R 2,INTERSECTION: R 1  R 2, DIFFERENCE: R 1 — R 2, For , , —, the operand relations R 1 (A 1,..., A n ) and R 2 (B 1,..., B n ) must have the same number of attributes, and the domains of attributes must be compatible; that is, dom(A i )=dom(B i ) for i=1, 2,..., n. This condition is called union compatibility. The resulting relation for , , or — has the same attribute names as the first operand relation R 1 (by convention).

15 CG096 Advanced Database Technologies Lecture 6: Relational Languages 15

16 CG096 Advanced Database Technologies Lecture 6: Relational Languages 16 CARTESIAN PRODUCT R(A 1, A 2,..., A m, B 1,..., B n )  R 1 (A 1, A 2,..., A m )  R 2 (B 1,..., B n ) A tuple t exists in R for each combination of tuples t 1 from R 1 and t 2 from R 2 such that: t [A 1, A 2,..., A m ] = t 1 and t [B 1, B 2,..., B n ] = t 2 The resulting relation R has m 1 + m 2 columns If R 1 has n 1 tuples and R 2 has n 2 tuples, then R will have n 1 *n 2 tuples. More Set Operations ABCDE 123 234 345 ab bc X 123ab123bc234ab234bc345ab345bc 3 attributes + 2 attributes = 5 attributes 3 tuples * 2 tuples = 6 tuples = R1R2 ABCDER

17 CG096 Advanced Database Technologies Lecture 6: Relational Languages 17 Obviously, CARTESIAN PRODUCT is useless if alone, since it generates all possible combinations. It can combine related tuples from two relations in a more informative way if followed by the appropriate RESTRICT operation Example: Retrieve a list of the names of dependents for each female employee FEMALE_EMPS   SEX=‘F’ (EMPLOYEE) EMPNAMES   FNAME,LNAME,SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES  DEPENDENT ACTUAL_DEPENDENTS   SSN=ESSN (EMP_DEPENDENTS) RESULT   FNAME,LNAME,DEPENDENT_NAME (ACTUAL_DEPENDENTS)

18 CG096 Advanced Database Technologies Lecture 6: Relational Languages 18

19 CG096 Advanced Database Technologies Lecture 6: Relational Languages 19 2.5 JOIN Operations THETA JOIN: Similar to a CARTESIAN PRODUCT followed by a RESTRICT. The condition c is called a join condition. R(A 1, A 2,..., A m, B 1, B 2,..., B n )  R 1 (A 1, A 2,..., A m ) ⋈ c R 2 (B 1, B 2,..., B n ) EQUIJOIN: The join condition c includes equality comparisons involving attributes from R 1 and R 2. That is, c is of the form: (A i =B j ) AND... AND (A h =B k ); 1<i,h<m, 1<j,k<n In the above EQUIJOIN operation: A i,..., A h are called the join attributes of R 1 B j,..., B k are called the join attributes of R 2 Example: Retrieve each department's name and its manager's name: T  DEPARTMENT ⋈ MGRSSN=SSN EMPLOYEE RESULT   DNAME,FNAME,LNAME (T)

20 CG096 Advanced Database Technologies Lecture 6: Relational Languages 20 DEPT_MGR   DNAME,…,MGRSSN,…LNAME,…,SSN… (DEPARTMENT ⋈ MGRSSN=SSN EMPLOYEE)

21 CG096 Advanced Database Technologies Lecture 6: Relational Languages 21 In an EQUIJOIN R  R 1 ⋈ c R 2, the join attribute of R 2 appear redundantly in the result relation R. In a NATURAL JOIN, the join attributes of R 2 are eliminated from R. The equality is implied and there is no need to specify it. The form of the operator is R  R 1 ⋈ (join attributes of R 1 ),(join attributes of R 2 ) R 2 Example: Retrieve each employee's name and the name of the department he/she works for: T  EMPLOYEE ⋈ (DNO),(DNUMBER) DEPARTMENT RESULT   FNAME,LNAME,DNAME (T) If the join attributes have the same names in both relations, they need not be specified and we can write R  R 1 * R 2. Example: Retrieve each employee's name and the name of his/her supervisor: SUPERVISOR(SUPERSSN,SFN,SLN)  SN,FNAME,LNAME (EMPLOYEE) T  EMPLOYEE * SUPERVISOR RESULT   FNAME,LNAME,SFN,SLN (T) Natural Join

22 CG096 Advanced Database Technologies Lecture 6: Relational Languages 22 Example: Retrieve each employee’s name and the name of the department he/she works for: T  EMPLOYEE ⋈ DNO=DNUMBER or SSN=MGRSSN DEPARTMENT RESULT   FNAME,LNAME,DNAME (T) Multiple Join JOIN ATTRIBUTESRELATIONSHIP EMPLOYEE.SSN = DEPARTMENT.MGRSSN EMPLOYEE manages the DEPARTMENT EMPLOYEE.DNO = DEPARTMENT.DNUMBER EMPLOYEE works in the DEPARTMENT

23 CG096 Advanced Database Technologies Lecture 6: Relational Languages 23 A relation can have a set of join attributes to join it with itself : JOIN ATTRIBUTESRELATIONSHIP EMPLOYEE(1).SUPERSSN= EMPLOYEE(2) supervises EMPLOYEE(2).SSNEMPLOYEE(1) One can think of this as joining two distinct copies of the relation, although only one relation actually exists In this case, renaming can be useful Example: Retrieve each employee’s name and the name of his/her supervisor: SUPERVISOR(SSSN,SFN,SLN)   SSN,FNAME,LNAME (EMPLOYEE) T  EMPLOYEE ⋈ SUPERSSN = SSSN SUPERVISOR RESULT   FNAME,LNAME,SFN,SLN (T) Self Join

24 CG096 Advanced Database Technologies Lecture 6: Relational Languages 24

25 CG096 Advanced Database Technologies Lecture 6: Relational Languages 25 All the operations discussed so far can be described as a sequence of only the operations RESTRICT, PROJECT, UNION, SET DIFFERENCE, and CARTESIAN PRODUCT. Hence, the set { , , ,—,  } is called a complete set of relational algebra operations. Any query language equivalent to these operations is called relationally complete. For database applications, additional operations are needed that were not part of the original relational algebra. These include: 1. Aggregate functions and grouping. 2. OUTER JOIN and OUTER UNION. 2.6 Complete Set of Operations

26 CG096 Advanced Database Technologies Lecture 6: Relational Languages 26 2.7 Additional Relational Operations AGGREGATIONS Functions such as SUM, COUNT, AVERAGE, MIN, MAX are often applied to sets of tuples and aggregate the result through iterative application of the functions to certain attributes of the relation  (R) Note: The grouping attributes are optional – if they are not present, there will be only one group of values consisting of the entire relation Example: Retrieve the average salary of all employees (no grouping): R(AVGSAL)   AVERAGE SALARY (EMPLOYEE) Example: For each department, retrieve the department number, the number of employees, and the average salary (grouping by department, counting the employees and averaging the salary in each group): R(DNO,NUMEMPS,AVGSAL)  DNO  COUNT SSN, AVERAGE SALARY (EMPLOYEE) In this example DNO, NUMEMPS, AVGSAL are grouping attributes

27 CG096 Advanced Database Technologies Lecture 6: Relational Languages 27

28 CG096 Advanced Database Technologies Lecture 6: Relational Languages 28 2.8. More Relational Operations OUTER JOIN In a regular EQUIJOIN or NATURAL JOIN operation, tuples in R 1 or R 2 that do not have matching tuples in the other relation do not appear in the result. Some queries require all tuples in R 1 (or R 2 or both) to appear in the result When no matching tuples are found, nulls are placed for the missing attributes LEFT OUTER JOIN: R 1 R 2 lets every tuple in R 1 appear in the result RIGHT OUTER JOIN : R 1 R 2 lets every tuple in R 2 appear in the result FULL OUTER JOIN: R 1 R 2 lets every tuple in both R 1 and R 2 appear in the result

29 CG096 Advanced Database Technologies Lecture 6: Relational Languages 29 TEMP  EMPLOYEE SSN=MGRSSN DEPARTMENT RESULT   FNAME,MINIT,LNAME,DNAME (TEMP)

30 CG096 Advanced Database Technologies Lecture 6: Relational Languages 30 3 Relational Calculus Declarative language based on predicate logic - checks what is true in the database rather then looking to get it The main difference in comparison with the relational algebra is the introduction of variables which range over attributes or tuples Two different variations of the relational calculus: domain calculus - specification of formal properties for different data types, used for describing data tuple calculus - querying and checking formal properties of stored relational data

31 CG096 Advanced Database Technologies Lecture 6: Relational Languages 31 3.1 Predicate Calculus Vocabulary of constant names, functional attributes and predicate properties, used for describing the information Phrases for specification of constant, functionally dependant or predicated properties build using proper vocabulary terms Statements about the world, formulated as meaningful phrases, connected through logical connectives in logical sentences conjunction (  ) disjunction (  ) negation (  ) implication (  ) equivalence (  ) Possible quantification of the sentences using existential (  ) or universal (  ) quantifiers, ranging over variables Example: All units registered in the database have unit leaders among the lecturers (  x).(Unit (x)  (  y).(Lecturer(y)  Leader(y,x)))

32 CG096 Advanced Database Technologies Lecture 6: Relational Languages 32 3.2 Relational DB as a Predicate Calculus model Semantic interpretation of the calculus is given in a set, so either all type domains of the relational schema (domain calculus), or the set of all relations in the database (tuple calculus) can serve a model for it All meaningful sentences in the model are true or false, so the relation tuples can be interpreted as truthful facts describing the world The constraints describe logical regularities among the attribute values, so they can be expressed as logical sentences Example: All records of unit leaders have primary keys Relation names are predicates with a place for each attribute Leader(Lno:INTEGER,Lname:CHAR,Uname:CHAR) - Lno, Lname and Uname are attributes of Leader Data values in the relation tuples are constants from the domains Leader(2,‘Johnson’,’CS234’) - 2,Johnson and CS234 are constants forming one Leader tuple Constraints can be stated using quantified variables over domains (  x)(  y,z).(Leader(x,y,z)) - x stands for the primary key of Leader

33 CG096 Advanced Database Technologies Lecture 6: Relational Languages 33 Notes: 1. In predicate calculus sentences contain quantified variables only 2. The sub-expressions following the quantified variables form the scope of quantification; it is usually closed in round parentheses Example: The table for unit leaders has foreign keys to both the lecturers and units tables Relation names are predicates with place for each attribute Unit(Uno:INTEGER, Uname:CHAR) - Uno and Uname are attributes of relation Unit Lecturer(Lno:INTEGER, Lname:CHAR) - Lno and Lname are attributes of relation Lecturer Leader(LUno:INTEGER,Lno:INTEGER,Uno:INTEGER) - LUno, Lno and Uno are attributes of relation Leader All foreign key constraints can be stated logically using quantified sentences, in which variables range over the respective values (  LUno,Uno,Lno).(Leader(LUno,Uno,Lno)  (  Lname).(Lecturer(Lno,Lname))  (  Uname).(Unit(Uno,Uname)) )

34 CG096 Advanced Database Technologies Lecture 6: Relational Languages 34 3.3 Relational Calculus as database query language Queries are logical expressions, which contain non-quantified (free) variables for tuples The free variables are placeholders for the information, we are looking for from the database relations The logical expressions, used in the query, are its conditions which need to be met during answering the query Example: Who are the employees with salary greater than 40 000? {e.FNAME,e.LNAME | Employee(e)  e.SALARY > 40 000} An answer is a replacement of the free variables in the query with tuple attributes from the relations used to formulate the query conditions; they should make the statement of the query true in database (pattern matching) Answer: James Borg and Jennifer Wallace Free variablesMatching values e.FNAMEJamesJennifer e.LNAMEBorgWallace

35 CG096 Advanced Database Technologies Lecture 6: Relational Languages 35 Notes: 1. The question variables are always listed in front of the expression, separated by | from the question condition; 2. Question condition can contain free variables only from the list in the beginning; all other variables should be quantified Querying related tables requires check of the corresponding keys for equality Example: Retrieve the name and address of all employees who work for the ‘Research’ department {e.FNAME,e.LNAME,e.ADDRESS | Employee(e)  (  d).(Department(d)  d.DNAME = ‘Research’  d.DNUMBER = e.DNO ) }

36 CG096 Advanced Database Technologies Lecture 6: Relational Languages 36 3.4 Equvalence between relational algebra and relational calculus Relational algebra and relational calculus are equivalent with respect to their ability to formulate queries against relational databases (Codd 1972) Relational algebra concentrates on the procedural (“how-to”) aspects and because of this it is used as an intermediate language for optimization of database queries Relational calculus is more appropriate to specify declaratively the model properties (“what is true”), without worrying about the way it is achieved and as such it can be used as a specification or querying language Note: The relational language Query-By-Example (QBE), which is developed by IBM during 70ties and is implemented in Paradox and Access desktop database systems is based on the relational calculus

37 CG096 Advanced Database Technologies Lecture 6: Relational Languages 37 4 Structured Query Language – SQL A standard language for working with relational databases; mixture of DDL, DML and DCL constructs It is a specific database language, not general-purpose programming language; all its constructs are primarily for manipulating tables, rows, columns, database schemes and users - not for direct processing of the data There are several SQL standards; among them SQL-92 (SQL2) and SQL-99 (SQL3) are the most widely used, and SQL-92 is still regarded up-to-date; Different vendors implement them to certain levels of compliance – for example, Oracle 8i is SQL2 compliant, while Oracle 9i is SQL3 compliant SQL is purely declarative; procedural extensions of SQL exist, but they are vendor-specific (e.g. Oracle PL/SQL, Microsoft Transact SQL, Informix 4GL, etc.)

38 CG096 Advanced Database Technologies Lecture 6: Relational Languages 38 4.1 SQL DML For querying and manipulating relational databases; its constructs are recognised by the first keyword - SELECT, INSERT, UPDATE or DELETE The SELECT statements implement the relational algebra operations; All relational operations can be expressed in a standard SQL implementation using SELECT statements only INSERT statements are used to add tuples to the relations, defined by the relational schema DELETE statements exclude tuples from the relations UPDATE change some of the attributes of the existing tuples

39 CG096 Advanced Database Technologies Lecture 6: Relational Languages 39 4.2 SELECT- statement The general form of this statement in SQL is SELECT FROM WHERE The result from executing the statement is directed to the system output by default. It corresponds to the following form  Example: The following statement SELECT e.name, a.address FROM EMP e, ADDR a WHERE a.no = e.no corresponds to the algebraic expression   name,address (EMP * ADDR)

40 CG096 Advanced Database Technologies Lecture 6: Relational Languages 40 In some cases (e.g. in Oracle extended SQL), the query result can be stored SELECT INTO FROM WHERE In this case, the result is stored into and the statement corresponds to the following algebraic expression  where is the relation calculated as a result of the expression

41 CG096 Advanced Database Technologies Lecture 6: Relational Languages 41 There are variations of these templates, which support the implementation of relational operations through varying of different clauses in the statement: SELECT clause can explicitly point at the required columns; alternatively, it can contain special symbol *, which projects full rows without skip of any columns in them FROM clause can contain more then one table, which allows to implement both unary and binary operations WHERE clause in the statement can be used to restrict the rows to be selected using additional conditions on the columns INTO clause, when present, points to a data storage which should store the result from the query; it should have the same structure, as the expected result

42 CG096 Advanced Database Technologies Lecture 6: Relational Languages 42 4.3 Implementation of relational operations All relational algebra operations are implemented in SQL using SELECT statements only. For this purpose the following variations of different parameters in the SELECT clauses can be used: The list of attributes to be selected in the SELECT clause The number of tables to be looked into in the FROM clause The conditions specified in the WHERE clause The resulting relation given INTO clause is present There is no structural correspondence between the expressions of relational algebra and the SQL expressions; in a single SQL statement the following combinations are possible: single unary operator applied to one single relation single binary operator applied to pair of relations sequence of unary operators applied inner side out to one relation and all the intermediate results combinations of binary and unary operators applied to several relations and the intermediate results according nesting rules

43 CG096 Advanced Database Technologies Lecture 6: Relational Languages 43 The SELECT statements of SQL when applied to one table only correspond to three possible combinations of the relational algebra operators  (RESTRICT) and  (PROJECT) Examples: List of all student ids, names and addresses SELECT StudentId, Name, Address FROM Student  StudentId, Name, Address   STUDENT  Full information about the students in Business Computing SELECT * FROM Student WHERE CourseName = ‘Business Computing’  CourseName = ‘Business Computing’  STUDENT  List of ids and names for students in Business Computing SELECT StudentId, Name FROM Student WHERE CourseName = ‘Business Computing’  StudentId, Name  CourseName = ‘Business Computing’  STUDENT  Restriction and Projection

44 CG096 Advanced Database Technologies Lecture 6: Relational Languages 44 When applied to two tables simultaneously without WHERE clause, the SELECT statements correspond to combinations of the unary relational algebra operator  with the binary operator  Examples: Full information about students and courses SELECT * FROM Student, Course STUDENT  COURSE List of ids for both students and lecturers SELECT StudentId, LecturerId, FROM Student, Lecturer  StudentId, LecturerId  STUDENT  LECTURER  Note: When more than one tables are in a single SELECT statement, to avoid ambiguity when referring to columns with the same names we precede them with the names of respective tables. Names of all students and lecturers SELECT STUDENT.Name, LECTURER.Name FROM STUDENT, LECTURER Cartesian Product

45 CG096 Advanced Database Technologies Lecture 6: Relational Languages 45 When applied to two tables simultaneously, the SELECT statements with WHERE clause correspond to combination of the two unary relational algebra operators  (RESTRICT) and  (PROJECT) with proper binary operators Examples: Full information about the students and their courses SELECT * FROM Student, Course WHERE Student.Course = Course.Name OR SELECT * FROM Student INNER JOIN Course ON Student.Course = Course.Name STUDENT ⋈ Course = Name COURSE List of student names with their course leaders SELECT StudentName, CourseLeader FROM Student NATURAL JOIN Course  StudentName, CourseLeader  STUDENT * COURSE  Inner Joins

46 CG096 Advanced Database Technologies Lecture 6: Relational Languages 46 Full information about students from Business computing with details about the course SELECT * FROM Student INNER JOIN Course ON Student.Course = Course.Name WHERE Course.CourseName = ‘Business Computing’  CourseName = ‘Business Computing’ (STUDENT ⋈ Course = Name COURSE) Note: In order to avoid ambiguity when referring to columns from different tables which have the same names, we should qualify them explicitly in the WHERE clause. This can be done through preceding their names with aliases of the respective tables. The names of all unit leaders of current units together with the names of the units themselves SELECT l.Name, u.Name FROM LECTURER l INNER JOIN UNIT u ON u.Leader = l.Name WHERE u.Status = ‘Current’

47 CG096 Advanced Database Technologies Lecture 6: Relational Languages 47 The tuples returned as answers of two independent queries can be combined in a single relation using the set operators  (UNION) - for the union of two relations and  (DIFFERENCE or MINUS) - for the difference between them Examples: (using Oracle SQL Syntax) Full details for students in both Mathematics and Computing SELECT * FROM Student WHERE Course = ‘Computing’ UNION SELECT * FROM Student WHERE Course = ‘Mathematics’  Course = ‘Computing’  STUDENT    Course = ‘Mathematics’  STUDENT  Full details for students in Computing after first year SELECT * FROM Student WHERE Course = ‘Computing’ MINUS SELECT * FROM Student WHERE Level = 1  Course = ‘Computing’  STUDENT    Level = 1  STUDENT  Set Operations

48 CG096 Advanced Database Technologies Lecture 6: Relational Languages 48 Outer joins are not always directly expressible in the commercial SQL implementations. But they can be modelled if needed Examples: (using Oracle SQL syntax) List of unit leaders with their units, including leaders still without assigned units SELECT LecturerName, UnitName FROM Lecturer, Unit WHERE Lecturer.UnitName = Unit.UnitName (+) LECTURER UnitName = UnitName UNIT List of units with their leaders, including units still without appointed leaders SELECT UnitName, LecturerName FROM Lecturer, Unit WHERE Lecturer.UnitName (+) = Unit.UnitName LECTURER UnitName = UnitName UNIT Outer Joins (Oracle 8i)

49 CG096 Advanced Database Technologies Lecture 6: Relational Languages 49 Combined list of units and leaders, including units without leaders and leaders without units (full outer join) SELECT Lecturer.UnitName, Unit.UnitName FROM Lecturer, Unit WHERE Lecturer.UnitName = Unit.UnitName(+) UNION SELECT Lecturer.UnitName, Unit.UnitName FROM Lecturer, Unit WHERE Lecturer.UnitName (+) = Unit.UnitName LECTURER UnitName = UnitName UNIT

50 CG096 Advanced Database Technologies Lecture 6: Relational Languages 50 Outer joins are not always directly expressible in the commercial SQL implementations. But they can be modelled if needed Examples: (using Oracle SQL syntax) List of unit leaders with their units, including leaders still without assigned units SELECT LecturerName, UnitName FROM Lecturer LEFT OUTER JOIN Unit ON Lecturer.UnitName = Unit.UnitName LECTURER UnitName = UnitName UNIT List of units with their leaders, including units still without appointed leaders SELECT UnitName, LecturerName FROM Lecturer RIGHT OUTER JOIN Unit WHERE Lecturer.UnitName = Unit.UnitName LECTURER UnitName = UnitName UNIT Outer Joins (Oracle 9i)

51 CG096 Advanced Database Technologies Lecture 6: Relational Languages 51 Combined list of units and leaders, including units without leaders and leaders without units (full outer join) SELECT Lecturer.UnitName, Unit.UnitName FROM Lecturer FULL OUTER JOIN Unit ON Lecturer.UnitName = Unit.UnitName When more than two tables are queried, the SQL operator corresponds to combination of unary and binary operations Examples: (using Oracle SQL syntax) List of students in Computing after first year SELECT StudentId, StudentName FROM Student WHERE CourseName= ‘Computing’MINUS SELECT StudentId, StudentName FROM Student WHERE CourseLevel = 1  CourseName = ‘Computing’   StudentId, StudentName  STUDENT    CourseLevel = 1   StudentId, StudentName  STUDENT  LECTURER UnitName = UnitName UNIT

52 CG096 Advanced Database Technologies Lecture 6: Relational Languages 52 Note: According to relational algebra theory, the relations do not contain duplicated tuples. However, the result from an SQL query could contain several identical rows. If we wish to have a true relation as a result instead, the keyword DISTINCT should be specified after SELECT to indicate eliminating of possible duplications. All units which have been subscribed by the students without duplicates SELECT DISTINCT e.Unit FROM STUDENT s, ENROLMENT e WHERE e.Student = s.Name

53 CG096 Advanced Database Technologies Lecture 6: Relational Languages 53 Summary Relational Algebra Formal languages have unambiguous syntax and clear semantics, which makes them good for specifications Formal languages hide the details of implementation and can’t give very deep insight into the real DBMS Formal languages formalize adequately only part of the database operations, which have semantics inherited from the relational model; they do not cover DCL at al. SQL SQL syntax is ambiguous about the order of operations, which requires knowledge of the way it is interpreted SQL is not a programming language, but language for communication with DBMS and still can’t give very deep insight into the real data processing SQL as a mixture of DDL, DML and DCL is the ultimate choice for practical relational databases


Download ppt "CG096 Advanced Database Technologies Lecture 6 Relational Algebra and SQL."

Similar presentations


Ads by Google