Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”

Similar presentations


Presentation on theme: "Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”"— Presentation transcript:

1 Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure” languages: Relational algebra Tuple relational calculus Domain relational calculus

2 Relational Algebra It is a Procedural language It consist of set of operation which takes one or two relation as input and produce relation as output. Six basic operators select:  - unary project:  - unary rename:  - unary union:  - binary set difference: – - binary Cartesian product: x- binary The operators take one or two relations as inputs and produce a new relation as a result.

3 Additional Operations We define additional operations that do not add any power to the relational algebra, but that simplify common queries. Set intersection Natural join Division Assignment

4 Select Operation Notation:  p (r) p is called the selection predicate Defined as: Each term is one of: op or where op is one of: =, , >, . <.  Each term is connected by:  (and),  (or),  (not) Example of selection:  branch_name=“Perryridge” (account)

5 Select Operation – Example Relation r ABCD   1 5 12 23 7 3 10   A=B ^ D > 5 (r) ABCD   1 23 7 10

6 Project Operation Notation: where A 1, A 2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets Example: To eliminate the branch_name attribute of account  account_number, balance (account)

7 Project Operation – Example Relation r: ABC  10 20 30 40 11121112 AC  11121112 = AC  112112  A,C (r)

8 Find those customers who live in “xyz” :  customer_name (  cutomer-city = “xyz” (account)) Composition of Operations

9 Union Operation Notation: r  s Defined as: r  s = {t | t  r or t  s} For r  s to be valid. 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (example: 2 nd column of r deals with the same type of values as does the 2 nd column of s) Example: to find all customers with either an account or a loan  customer_name (depositor)   customer_name (borrower)

10 Union Operation – Example Relations r, s: r  s: AB  121121 AB  2323 r s AB  12131213

11 Set Difference Operation Notation r – s Defined as: r – s = {t | t  r and t  s} Set differences must be taken between compatible relations. r and s must have the same arity attribute domains of r and s must be compatible

12 Set Difference Operation – Example Relations r, s: r – s: AB  121121 AB  2323 r s AB  1111

13 Cartesian-Product Operation Notation r x s Defined as: r x s = {t q | t  r and q  s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R  S =  ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

14 Cartesian-Product Operation – Example Relations r, s: r x s: AB  1212 AB  1111222211112222 CD  10 20 10 20 10 E aabbaabbaabbaabb CD  20 10 E aabbaabb r s

15 Composition of Operations Can build expressions using multiple operations Example:  A=C (r x s) r x s  A=C (r x s) AB  1111222211112222 CD  10 20 10 20 10 E aabbaabbaabbaabb ABCDE  122122  20 aabaab

16 Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions. Allows us to refer to a relation by more than one name. Example:  x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A 1, A 2, …., A n.

17 Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer_name, loan_number)

18 Example Queries Find all loans of over $1200 Find the loan number for each loan of an amount greater than $1200  amount > 1200 (loan)  loan_number (  amount > 1200 (loan)) Find the names of all customers who have a loan, an account, or both, from the bank  customer_name (borrower)   customer_name (depositor)

19 Example Queries Find the names of all customers who have a loan at the Perryridge branch. Query 1  customer_name (  branch_name = “Perryridge” (  borrower.loan_number = loan.loan_number (borrower x loan)))

20 Example Queries Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.  customer-name (  branch-name = “Perryridge” (  borrower.loan-number = loan.loan-number ( borrower x loan ))) –  customer-name ( depositor )

21 Formal Definition A basic expression in the relational algebra consists of either one of the following: A relation in the database A constant relation Let E 1 and E 2 be relational-algebra expressions; the following are all relational-algebra expressions: E 1  E 2 E 1 – E 2 E 1 x E 2  p (E 1 ), P is a predicate on attributes in E 1  s (E 1 ), S is a list consisting of some of the attributes in E 1  x (E 1 ), x is the new name for the result of E 1


Download ppt "Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”"

Similar presentations


Ads by Google