chapter 2: relational algebra

Click here to load reader

Upload: claude

Post on 23-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

Chapter 2: Relational Algebra. -- Introduction to database principles Maoying Wu ( [email protected] ) March 6, 2013. Relation terminology. Learning strategies. Know how => know why 适当的不求甚解. After-class assignment 1. Install MySQL on your own computer - PowerPoint PPT Presentation

TRANSCRIPT

Introduction to Database Principles http://cbb.sjtu.edu.cn/course/database

-- Introduction to database principlesMaoying Wu ([email protected])March 6, 2013Chapter 2: Relational AlgebraRelation terminologyJargon ()Vulgo ()Relation name ()Table name ()Schema ()Table header (/)Relation ()2-d table ()Tuple ()Row ()Attribute ()Column ()Attribute name ()Column name ()Attribute value () Column value ()Domain ()Column type ()Learning strategiesKnow how => know why

After-class assignment 1Install MySQL on your own computer Linux: from source package or LAMPWindows: from rebuilt package or XAMP

Learn some basic knowledge on HTML/PHP and B/S development

Review of C programming skillsDML ()Data Manipulation LanguageLanguage for accessing and manipulating the data organized by appropriate data modelaka, query language ()

Two classes of languagesprocedural (): specifies what data is required and how to get these datadeclarative (): specifies what data is required without knowing how to get these data SQL: most widely used query language DDL ()Data-defintion LanguageSpecified notation for defining the database schemaExample:create table account(account_nochar(10),balanceinteger)

DDL compiler generates a set of tables stored in a data dictionary ()Data dictionary contains metadata ()Database schemaData storage and definition languagespecifies the storage structure and access methods ()Integrity constraintsdomain constraints referential integrity ()assertions ()Authorization ()SQL ()Structural Query LanguagePrincipal language to describe and manipulate relational databasesSQL-99 standard

Two aspects:Data-Definition language (DDL) for declaring database schemasData-Manipulation language (DML) for querying and modifyingSQL: 3 kinds of relationstables ()relations stored in the database allowing modification as well queries

views (): relations defined by a computation, constructed when needed

temporary tables (): constructed by SQL processor when performing queries.Relational Algebra-- Operations on relations, construct new relations from old onesFour classes of operationsset operations: union, intersection, and differenceremoving: selection eliminates some rows (tuples), and projection eliminates some columnsrelation combination: Cartesian product, joinrenaming: changing the relation schema, i.e., the names of the attributes and/or the name of relation itself.OutlinesProcedural language ()Six basic operators ()Select: ()Project: ()Union: ()Set difference: - ()Cartesian product: ()rename: ()The operators take one or two relations as inputs and produce a new relation as a resultSelect Operator ()Relation r

(A=B)(D>5) (r)

select operatorNotation: p (r)p is called the selection predicate ()Defined as

p is a formula in propositional calculus () consisting of terms connected by: (and), (or), (not)Each term can be one of op ( or )where op can be: =, , , , >, 1200(loan)

Find the loan numbers for loans with amount over 1200:loan_number(amount>1200(loan))

Find the customer names for all who have a loan, an account, or both, from the bank:customer_name(borrower) customer_name(depositor)

Example queriesFind the names of all customers who have a loan at the Shanghai branch:customer_name(branch_name=Shanghai(borrower.loan_number=loan.loan_number(borrowerloan)))

Find the names of all customers who have a loan at the Shanghai branch but do not have an account at any branch of the bank:customer_name(branch_name=Shanghai(borrower.loan_number=loan.loan_number(borrowerloan))) - customer_name(depositor)Two different strategiesfind the names of all customers who have a loan at the Shanghai branch(1)customer_name(branch_name=Shanghai(borrower.loan_number=loan.loan_number(borrowerloan)))

(2)customer_name(borrower.loan_number=loan.loan_number(branch_name=Shanghai(loan))borrower))Example queriesfind the largest account balanceStrategy:Find those balances that are not the largest(a) rename account as d so that we can compare each account balance with all othersuse set difference to find those account balances that were not found in the previous stepbalance(account) account.balance(account.balance < d.balance(account d(account)))Formal definitionA basic expression in the relational algebra consists of either one of the following:A relation in the databaseA constant relation

Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra expressions:E1E2 ()E1 E2 ()E1 x E2 ()p(E1), p is a predicate on attributes in E1 ()s(E1), s is a list of some of the attributes in E1 ()x(E1), X is the new name for the result of E1 ()29Additional operationsSet intersection ()Natural join ()Division ()Assignment ()

We must keep in mind that additional operations do NOT add anything to relational algebra, but that simplify some common queriesset intersection - exampleRelation r, s

rs:

Set intersectionNotation: rs

Defined as:

Assume:r, s have the same arityattributes of r and s are compatible

Note: rs = r (r-s)

Natural joinNotation:

Let r and s be relations on schemas R and S, respectively. Then is a relation on schema R S obtained as follows: Consider each pair of tuples tr from r and ts from s. If tr and ts have the same value on each of the attributes in RS, add a tuple t to the result, where t has the same value as tr on r and t has the same value as ts on sExample:R= (A, B, C, D)S= (E, B, D)Result schema = (A, B, C, D, E)

Natural join - exampleRelation r, s:

:

Division operator ()Notation: r sSuitable for queries that include the phrase for allLet r and s be relations on schemas R and S, whereR = (A1, , Am, B1, , Bn)S = (B1, , Bn)The result of r s is a relation on schema R S = (A1, A2, , Am)

where tu means the concatenation of tuples t and u to produce a single tuple

Division example 1r s:

Division example 2r s:

Assignment operatorThe assignment operator () provides a convenient way to expression complex querieswrite query as a sequential program consisting of a series of assignmentsfollowed by an expression whose value is displayed as a result of the queryAssignment must always be made to a temporary relation variableExample: write rs astemp1 R-S(r)temp2 R-S((temp1s) - R-S,S(r)result = temp1 temp2Ends!