170 likes | 178 Views
Learn about relational algebra, a procedural language used to query databases, and explore examples of basic relational operations. Discover the different types of query languages and their categories.
E N D
Relational algebra • Reading: 7.4, 7.6, 7.7 • Contents: • Basic relational algebra operations • Examples of queries in relational operations
Query Languages • Language in which user requests information from the database. • Categories of languages • procedural • non-procedural • “Pure” languages: • Relational Algebra • Tuple Relational Calculus • Domain Relational Calculus • Pure languages form underlying basis of query languages that people use.
Relational Algebra Relational Algebra is a Procedural language • Six basic operators • select • Project • rename • union • set difference • The join operation • Cartesian product
A B C D • Relation r 1 5 12 23 7 7 3 10 • A=B ^ D > 5(r) A B C D 1 23 7 10 Select Operation – Example
Select Operation (II) • Notation: p(r) • p is called the selection predicate p has following form: <attribute> op <attribute> or <constant> where op is one of: =, , >, . <. • Example of selection:deptname=“ITM”( Department )
A B C 10 20 30 40 1 1 1 2 A C A C 1 1 1 2 1 1 2 = Project Operation – Example • Relation r: • A,C (r)
Rename Operation • Allows us to name, and therefore to refer to, the results of relational-algebra expressions. • Example: ITM_dept deptname= “ITM” ( Department ) Family01(name01) pdependent_name(sESSN= ’01’ (Dependent)
A B A B 1 2 1 2 3 s r A B 1 2 1 3 Union Operation – Example • Relations r, s: r s:
A B A B 1 2 1 2 3 s r a 2 Intersection Operation – Example • Relations r, s: r s: A B
A B A B 1 2 1 2 3 s r A B 1 Minus Operation – Example • Relations r, s: r - s:
Constraints in union and difference operation • Set union, intersection and minus must be taken between compatible relations. • r and s must have the same arity • attribute domains of r and s must be compatible
A B C D E Relations r, s: 1 2 10 10 20 10 a a b b r s r xs: A B C D E 1 1 1 1 2 2 2 2 10 10 20 10 10 10 20 10 a a b b a a b b Cartesian-Product Operation-Example
A B C D E Relations r, s: 1 2 r c 10 10 20 10 a a b b r s rA = Cs: A B C D E 1 2 10 10 a a Join operation - Example
B D E A B C D 1 3 1 2 3 a a a b b 1 2 4 1 2 a a b a b s A B C D E 1 1 1 1 2 a a a a b Natural join operation - example • Relations r, s: r r * s
Banking Schema 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)
Examples of queries in relational operations • Find all branchs of branch_city is Lund. • Find the loan number for each loan of an amount greater than 1000000 kr. • Find the names of all customers who have a loan, an account, or both, from the bank. • Find the names of all customers who have an account, but no any loans from the bank.
Examples of queries in relational operations • Find the names of all customers who have both an account and a loan from the bank. • Find the names of all customers who have a loan at a branch in Lund.