INTRODUCTION QUERY LANGUAGE Lecture 9 Query Language Relational Query Language is used by the user to communicate with the database through it user requests information from the database. Types of Relational Query Language There are two types of relational query language: Procedural Query Language Non-Procedural Language PROCEDURAL The program code is written as a sequence of instructions. User has to specify “what to do” and also “how to do” (step by step procedure). Relational Algebra is a Procedural Query Language NON-PROCEDURAL LANGUAGE The user has to specify only “what to do” and not “how to do”. It only gives a single Query on one or more tables to get. SQL Lets you access or modify databases. SQL can execute queries, o retrieve data o insert records o update records o delete records o create a new database, new tables, views, and set permissions on tables, procedures, or views. RELATIONAL QUERY LANGUAGE IN DBMS SQL has its own querying methods to interact with the database. These queries work similarly to Relational Algebra and Relational Calculus. Relational Database systems are expected to have a query language that help users to query the database. RELATIONAL ALGEBRA It is a mathematical theory that uses algebraic structures to model data and define queries. It's a set of mathematical operators that can compose, modify, and combine tuples within different relations. Its operations work on one or more table to define another table without changing the original table. Both operands and results are table, so output from one operation can become input to another operation. Allows expressions to be nested, just as in arithmetic. This property is called closure. Five basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference. These perform most of the data retrieval operations needed. Also have Join, Intersection, and Division operations, which can be expressed in terms of 5 basic operations. SELECTION (OR RESTRICTION) σpredicate(R) o Works on a single relation(table) R and defines a relation that contains only those rows of R that satisfy the specified condition (predicate). Notation − σp(r) o σ stands for selection predicate o r stands for relation o p is prepositional logic (may use connectors like and, or, and not) o May use relational operators like − =, ≠, ≥, < , >, ≤. List all staff with a salary greater than £10,000. o σsalary > 1000(Staff) PROJECTION ∏col1,…,coln(R) o Works on a single relation R and defines a relation that contains a specific columns of R, and eliminating duplicates. o It projects column(s) that satisfy a given predicate. Notation − ∏A1, A2, An (r) o Where A1, A2 , An are attribute names of relation r. Produce a list of salaries for all staff, showing only staffNo, fName, lName, and salary details. o ∏staffNo, fName, lName, salary(Staff) UNION OPERATION Denoted by: r U s Defined as: r U s = {t | t ∈ r or t ∈ s} The result of r U s will include all tuples which are either in r or in s or in both. For r U s to be valid r and s must be union compatible Union operation is: o Commutative: r U s = s U r o Associative: r U (s U w) = (r U s) U w E.g. to find all the names of faculty and students in the FACULTY and STUDENT tables: o ∏name (FACULTY) (STUDENT) U ∏name When two or more tables share the same number of columns, they are said union compatible SET DIFFERENCE OPERATION Is denoted by: r – s Is defined as: r – s = {t | t ∈ r and t ∈ s} The result of r – s will include all the tuples that are in r but not in s. r and s must be union compatible Neither Commutative nor Associative. SET-INTERSECTION OPERATION Is denoted by: r ∩ s Is defined as: r ∩ s = { t | t ∈ r and t ∈ s } The result of r ∩ s will include all the tuples that are in both r and s. r and s must be union compatible. Intersection is: o Commutative: r ∩ s = s ∩ r o Associative: r ∩ (s ∩ w) = (r ∩ s) ∩ w Note: r ∩ s = r - (r - s) CARTESIAN-PRODUCT OPERATION Is denoted by: r x s Is defined as: r x s = { t | t ∈ r and t ∈ s } The result of r x s will combine tuples from both r and s in a combinatorial fashion. Assume that attributes of r(A) and s(B) are disjoint. (That is, A ∩ B = Ø). If attributes of r(A) and s(B) are not disjoint, then renaming must be used. CHARACTERISTICS OF CARTESIAN-PRODUCT OPERATION Degree r X s = degree(r) + degree(s) Cardinality of r X s = cardinality(r) * cardinality(s) Generally the result of CARTESIAN PRODUCT is meaningless unless is followed by SELECT, and is called JOIN. EXAMPLE QUERIES The following Relations are used for the coming Examples. branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number) 1. Find all loans of over $1200 T = Samount > 1200 (loan) 2. Find the loan number for each loan of an amount greater than $1200 T = ∏loan-number (Samount> 1200(loan)) 3. Find the names of all customers who have a loan, an account, or both, from the bank T = ∏customer-name (borrower) U ∏customer-name (depositor) 4. Find the names of all customers who have a loan and an account at bank T = ∏customer-name (borrower) ∩ ∏customer-name (depositor)