Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.

Similar presentations


Presentation on theme: "CMSC424, Spring 2005 CMSC424: Database Design Lecture 5."— Presentation transcript:

1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 5

2 CMSC424, Spring 2005 Review: Relational Algebra Relational Algebra Operators 1.Select (  ) 2.Project (  ) 3.Set Union (U) 4.Set Difference (-) 5.Cartesian Product (  ) 6.Rename (  ) These are called fundamental operations

3 CMSC424, Spring 2005 Relational Algebra Redundant Operators 4. Update (  ) 2. Division ( ) 1.Natural Join ( ) 3. Outer Joins ( )

4 CMSC424, Spring 2005 Natural Join Idea: match tuples on common attributes ABCD 12231223 αααβαααβ +--++--+ 10 20 10 EBD ‘a’ ‘b’ ‘c’ ααββααββ 10 20 10 r s ABCDE 1223312233 αααββαααββ +--+++--++ 20 10 ‘a’ ‘b’ ‘c’ = Relation 1 Relation 2 Notation:

5 CMSC424, Spring 2005 Division Query: Find customers who have accounts in all branches in Brooklyn r1  all branches in Brooklyn r2  associate customers with branches they have accounts in Now what ? Use the division operator Relation 1 Relation 2 Notation: Idea: expresses “for all” queries

6 CMSC424, Spring 2005 bnamelnoamt Downtown Redwood Perry L-170 L-230 L-260 3000 4000 1700 loan = cnamelno Jones Smith Hayes L-170 L-230 L-155 borrower = = bnamelnoamtcname Downtown Redwood L-170 L-230 3000 4000 Jones Smith Join result loses…  any record of Perry  any record of Hayes Outer Joins Motivation: loanborrower =

7 CMSC424, Spring 2005 bnamelnoamt Downtown Redwood Perry L-170 L-230 L-260 3000 4000 1700 loan = cnamelno Jones Smith Hayes L-170 L-230 L-155 borrower = bnamelnoamtcname Downtown Redwood Perry L-170 L-230 L-260 3000 4000 1700 Jones Smith ┴ preserves all tuples in left relation 1. Left Outer Join ( ) ┴ = NULL Outer Joins loan borrower =

8 CMSC424, Spring 2005 bnamelnoamtcname Downtown Redwood ┴ L-170 L-230 L-155 3000 4000 ┴ Jones Smith Hayes bnamelnoamt Downtown Redwood Perry L-170 L-230 L-260 3000 4000 1700 loan = cnamelno Jones Smith Hayes L-170 L-230 L-155 borrower = preserves all tuples in right relation 2. Right Outer Join ( ) ┴ = NULL Outer Joins loan borrower =

9 CMSC424, Spring 2005 bnamelnoamt Downtown Redwood Perry L-170 L-230 L-260 3000 4000 1700 loan = cnamelno Jones Smith Hayes L-170 L-230 L-155 borrower = preserves all tuples in both relations 3. Full Outer Join ( ) ┴ = NULL Outer Joins bnamelnoamtcname Downtown Redwood Perry ┴ L-170 L-230 L-260 L-155 3000 4000 1700 ┴ Jones Smith ┴ Hayes loan borrower =

10 CMSC424, Spring 2005 1. Deletion: r  r – s e.g., account  account – σ bname=Perry (account) (deletes all Perry accounts) 2. Insertion: r  r  s e.g., branch  branch  {(Waltham, Boston, 7M)} (inserts new branch with bname = Waltham, bcity = Boston, assets = 7M) 3. Update: r  π e1,…,en (r) e.g., account  π bname,acct_no,bal*1.05 (account) (adds 5% interest to account balances) Update Identifier  Query Notation: Common Uses:

11 CMSC424, Spring 2005 Extended Relational Algebra 1.Generalized projection 2.Aggregates

12 CMSC424, Spring 2005  e 1,…,e n (Relation) e 1,…,e n can include arithmetic expressions – not just attributes cnamelimitbalance Jones Turner 5000 3000 2000 2500 credit = π cname, limit - balance (credit) = cnamelimit-balance Jones Turner 3000 500 Generalized Projection Notation: Example Then…

13 CMSC424, Spring 2005  e 1,…,e n (Relation) e 1,…,e n can include arithmetic expressions – not just attributes cnamelimitbalance Jones Turner 5000 3000 2000 2500 credit = π cname, limit - balance as limitbalance (credit) = cnamelimitbalance Jones Turner 3000 500 Generalized Projection Notation: Example Then…

14 CMSC424, Spring 2005 Aggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

15 CMSC424, Spring 2005 Aggregate Operation – Example Relation r: AB   C 7 3 10 g sum(c) as sumC (r) sum-C 27

16 CMSC424, Spring 2005 Aggregate Functions and Operations General form: G1, G2, …, Gn g F1( A1 ), F2( A2 ),…, Fn( An ) (E) E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each A i is an attribute name

17 CMSC424, Spring 2005 Aggregate Operation – Example Relation account grouped by branch-name: branch-name g sum(balance) (account) branch-nameaccount-numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-namebalance Perryridge Brighton Redwood 1300 1500 700

18 CMSC424, Spring 2005 Other Theoretical Languages Relational Calculus Non-procedural Tuple relational calculus Examples Safety Domain relational calculus

19 CMSC424, Spring 2005 Review: Query Languages Theoretical UsePractical Use Relational Algebra Formal semantics of practical QL’s Language Expressivity Internal query representation for query optimizers TRC Foundation for SQL SQL-92 -Standard for Relational DB Query Languages SQL-99 -Standard for Object-Relational DB Query Languages OQL -Standard for Object-Oriented DB Query Languages XQuery -Standard for XML-based DB Query Languages

20 CMSC424, Spring 2005 SQL - Introduction Standard DML/DDL for relational DB’s DML = Data Manipulation Language (queries, updates) DDL = Data Definition Language (create tables, indexes, …) View definition Security (Authorization) Integrity constraints Transactions Early 70’s, IBM system R project (SEQUEL) Later, become standard (Structured Query Language) Also includes History

21 CMSC424, Spring 2005 SQL: Basic Structure SELECTA 1, ….., A n FROMr 1, ….., r m WHEREP Equivalent to:  A 1,A2,…,An ( σ P (r 1  …  r n ))

22 CMSC424, Spring 2005 A Simple SELECT-FROM-WHERE Query Similar to SELECTbname FROMloan WHEREamt > 1000 Can instead write : SELECT DISTINCT bname FROMloan WHEREamt > 1000 (removes duplicates from result) We will discuss bag algebra a bit later  bname (  amt > 1000 ( loan ) ) bname Redwood Perry Downtown Perry Why preserve duplicates? Duplicates are retained (i.e., result not a set) But not quite

23 CMSC424, Spring 2005 Another SELECT-FROM-WHERE Query Similar to SELECTcname, balance FROMdepositor, account WHEREdepositor.acct_no = account.acct_no Can also write SELECTd.cname, a.balance FROMdepositor as d, account as a WHEREd.acct_no = a.acct_no (neccessary for self-joins) cnamebalance Johnson Smith Hayes Turner Johnson Jones Lindsay 500 700 400 350 900 750 700 Note:  cname, balance (depositor account ) Returns:

24 CMSC424, Spring 2005 The SELECT Clause Equivalent to (generalized) projection, despite name e.g: SELECT* FROMloan e.g: SELECTbname, acct_no, balance*1.05 FROMaccount Can use ‘*’ to get all attributes Can write SELECT DISTINCT to eliminate duplicates Can write SELECT ALL to preserve duplicates (default) Can include arithmetic expressions

25 CMSC424, Spring 2005 (or, depending on WHERE clause) The FROM Clause Equivalent to cartesian product (  ) e.g: FROM borrower, loan Computes borrower    loan Identifies borrower, loan columns in result, allowing one to write WHERE borrower.lno = loan.lno e.g: FROM borrower as b, loan as l allows one to write WHERE b.lno = l.lno Binds tuples in relations to variable names

26 CMSC424, Spring 2005 The WHERE Clause Equivalent to Selection, despite name 1.Simple attribute relop attribute (or constant) (relop: =, <>,, =) 2. Complex (using AND, OR, NOT, BETWEEN) e.g: SELECT lno FROM loan WHERE amt BETWEEN 90000 AND 100000 is the same as… SELECT lno FROM loan WHERE amt >= 90000 AND amt <= 100000 WHERE predicate can be:

27 CMSC424, Spring 2005 Data Definition Language Allows specification of relation schema as well as: Attribute domains Integrity constraints Security and authorization information Creation of Indexes …

28 CMSC424, Spring 2005 DDL CREATE TABLE branch (branch-name char(15) not null, branch-city char(30), assets integer, primary key (branch-name), check (assets >= 0)) DROP TABLE branch ALTER TABLE branch ADD zipcode integer


Download ppt "CMSC424, Spring 2005 CMSC424: Database Design Lecture 5."

Similar presentations


Ads by Google