chapter 3: basic sql

Download Chapter 3: Basic SQL

Post on 23-Feb-2016

35 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Chapter 3: Basic SQL. -- Introduction to database principles Maoying Wu ( bi203.sjtu@gmail.com ) March 11, 2013. Outline. Data Definition Language (DDL) Basic Query Structure Set Operations Aggregate functions Null values Nested subqueries Complex queries Views - PowerPoint PPT Presentation

TRANSCRIPT

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

-- Introduction to database principlesMaoying Wu (bi203.sjtu@gmail.com)March 11, 2013Chapter 3: Basic SQLOutlineData Definition Language (DDL)Basic Query StructureSet OperationsAggregate functionsNull valuesNested subqueriesComplex queriesViewsModification of the DatabaseJoin OperationsData definition language (DDL)The schema for each relation ()The domain of values associated with each attribute ()Integrity constraints ()The set of indices to be maintained for each relation ()Security and authorization information for each relation ()The physical storage structure for each relation ()Data Types in SQLchar(n): Fixed length character string, with user-specified length nvarchar(n): Variable length character string with user-specified maximum length nint: Integer (a finite subset of the integers that is machine-dependent)smallint: Small integer (a machine-dependent subset of the integer domain type)numeric(p, d): Fixed point number, with user-specified precision of p digits, with d digits to the right of decimal pointreal, double precision: floating point and double-precision floating point number, with machine-dependent precisionfloat(n): Floating point number, with user-specified precision of at least n digitsTo get more details, see the help page for data types in MySQLCreate tableCREATE TABLE r(A1 D1, A2 D2, , An Dn,[integrity-constraint 1],[integrity-constraint k]);r is the name of the relationeach Ai is an attribute name in the schema for relation rDi is the data type for the domain for attribute Ai

Integrity constraintsNOT NULLPRIMARY KEY (A1, , Ak)DROP TABLEDROP TABLE r;Delete all information for relation r from the databaseALTER TABLEALTER TABLE r ADD Ak Dk;Add attributes to an existing relationAll tuples in the relation are assigned null as the values for the added attribute

ALTER TABLE r DROP A;Drop attributes of a relationnot supported by many DBMSBasic QueriesSQL is based on set and relational operations with certain modifications and enhancementsA typical SQL query has the form:SELECT A1, A2, , AnFROM r1, r2, , rmWHERE P;This query is equivalent to the relational algebra expression:

The SELECT ClauseThe SELECT clause lists the attributes for the result of a queryProjection operation in relational algebra

Keyword DISTINCT can be used to eliminate the duplicate recordsSELECT DISTINCT loan_number FROM loan;Keyword ALL can be used to preserve all the duplicate recordsSELECT ALL loan_number FROM loan;SELECT Clause (Cont.)*means all attributes:SELECT * FROM loan;

Arithmetic expressions (+,-,*,/) can be involved in the expression following SELECTSELECT loan_number, amount*100 FROM loan;The WHERE ClauseWHERE clause specifies conditions the result must satisfy

corresponding to the selection predicate of the relation algebra

Logical operations like AND, OR and NOT can be used to combine multiple conditional expressionsSELECT loan_numberFROM loanWHERE branch_name=Shanghai AND amount>1200;

WHERE ClauseBETWEEN can be used to specified the rangeSELECT loan_numberFROM loanWHERE amount BETWEEN 90000 AND 100000;

SELECT loan_numberFROM loanWHERE (amount > 90000) AND (amount > 100000);FROM ClauseFROM specifies the relations involved in the querycorresponding to the Cartesian product operations of the relational algebraSELECT * FROM borrower, loan;

Find the name, loan_number and loan amount of all customers having a loan at Shanghai branch:SELECT customer_name, borrower.loan_number, amountFROM borrower, loanWHERE borrower.loan_number = loan.loan_number and branch_name=Shanghai;AS ClauseSynopsis: old_name AS new_namecorresponding to the renaming operation in relational algebra:SELECT customer_name, borrower.loan_number AS loan_id, amountFROM borrower, loanWHERE borrower.loan_number = loan.loan_number;

SELECT customer_name, T.loan_number, S.amountFROM borrower AS T, loan AS SWHERE T.loan_number = S.loan_number;string operationsSQL includes a string-matching operator for comparisons on character string: LIKE %: matches any string_: matches any characterFind the names of all customers whose street includes the substring Main:SELECT customer_nameFROM customerWHERE customer_street LIKE %MAIN%;If a percentage sign is included in a string, escape character can be used:LIKE MAIN\%;NOTE: Many more functions can be used in string operations.ORDER BY: Ordering the resultsSELECT DISTINCT customer_nameFROM borrower, loanWHERE borrower.loan_number = loan.loan_number AND branch_name=ShanghaiORDER BY customer_name;

The default ordering rule is ascending, but we can also use DESC to specify the descending order:ORDER BY customer_name DESC;Set OperationsSet operations UNION, INTERSECT and EXCEPT correspond to the relational algebra ,,and -.

In MySQL, NO INTERSECT and EXCEPT but we can use IN and NOT IN as an alternative.Set operationsFind all customers with a loan, an account, or both:(SELECT customer_name FROM depositor)UNION(SELECT customer_name FROM borrower);Find all customers who have both loan and account(SELECT customer_name FROM depositor)INTERSECT(SELECT customer_name FROM borrower);Find all customers who have an account but no loan(SELECT customer_name FROM depositor)INTERSECT(SELECT customer_name FROM borrower);

Aggregate functionsAVG: average valueMIN: minimum valueMAX: maximum valueSUM: sum of valuesCOUNT: number of valuesAggregate functions: examplesFind the average account balance at the Shanghai branch:SELECT avg(balance)FROM accountWHERE branch_name = Shanghai;Find the number of tuples in the customer relationSELECT count(*)FROM customer;Find the number of depositors in the bankSELECT count(distinct customer_name)FROM depositor;

Aggregate: GROUP BYFind the number of depositors for each branch:SELECT branch_name, count(distinct customer_name)FROM depositor, accountWHERE depositor.account_number=account.account_numberGROUP BY branch_name;

NOTEThe attribute names after the GROUP BY should appear after SELECT, while outside the aggregate functions.

HAVINGQualifying Results by Categories Find the names of all branches where the average account balance is more than 1200SELECT branch_name, avg(balance)FROM accountGROUP BY branch_nameHAVING avg(balance) > 1200;Distinctions between HAVING and WHEREPredicates in HAVING are applied after forming GROUPS, while predicates in WHERE are applied before forming GROUPS. NULL valuesNULL signifies an unknown value or a value that does not exist.The predicate IS NULL can be used to check for NULL values.SELECT loan_numberFROM loanWHERE amount IS NULL;The result of any arithmetic expression involving NULL is NULL1+NULL returns NULLMany aggregate functions ignore NULL valuesNULL (cont.)Any comparison with NULL returns UNKNOWNLOGIC involving UNKNOWN:OR(UNKNOWN OR true) = true,(UNKNOWN OR false) = UNKNOWN,(UNKNOWN OR UNKNOWN) = UNKNOWNAND(true AND UNKNOWN) = UNKNOWN,(false AND UNKNOWN) = false,(UNKNOWN AND UNKNOWN) = UNKNOWNNOT(NOT UNKNOWN) = UNKNOWNAny UNKNOWN in WHERE is treated as falseIS UNKNOWN is used to evaluate if the expression is UNKNOWN.NULL value in aggregateGet the total all loan amounts:SELECT SUM(amount)FROM loan;

Above statement ignores all NULL amountsResult is NULL is all is NULL.All aggregate operation except count(*) ignore tuples with NULL values on the aggregated attributes.Nested subqueriesSQL provides a mechanism for the nesting of subqueriesA subquery is a SELECT-FROM-WHERE expression that is nested within another query

A common use of subqueries is to perform tests for set membership, set comparison, and set cardinalityExample subqueriesFind all customers who have both an account and a loan at the bank.SELECT distinct customer_nameFROM borrowerWHERE customer_name in (SELECT customer_name FROM depositor);Find all customers who have a loan at the bank but do not have an account at the bankSELECT distinct customer_nameFROM borrowerWHERE customer_name NOT IN (SELECT customer_name FROM depositor);Example subqueriesFind all customers who have both an account and a loan at the Shanghai branchSELECT distinct customer_nameFROM borrower, loanWHERE borrower.loan_number = loan.loan_number AND branch_name = Shanghai' and (branch_name, customer_name ) IN (SELECT branch_name, customer_name FROM depositor, account WHERE depositor.account_number = account.account_number );

NOTE: The formulation above is simply to illustrate SQL features.Set comparisonsSOME ()Find all branches that have more assets than some branch located in BeijingSELECT distinct T.branch_name FROM branch as T, branch as SWHERE T.assets > S.assets and S.branch_city = 'Beijing' ;

Same querySELECT branch_name FROM branchWHERE assets > SOME(SELECT assets FROM branch WHERE branch_city = 'Beijing') ;Set comparisonsALL ()Find all branches that have more assets than all branch located in Beijing

SELECT branch_nameFROM branchWHERE assets > ALL(SELECT assets FROM branch WHERE branch_city = 'Beijing') ;

Test for empty resultsEXISTS returns true if the argument subquery is non-empty

EXISTS(select-clause)

NOT EXISTS(select-clause)Example queryFind all customers who have an account at all branches located in Beijing.SELECT DISTINCT S.customer_nameFROM depositor as SWHERE NOT EXISTS ((SELECT branch_name FROM branchWHERE branch_city = 'Beijing') and branch_name NOT IN(SELECT R.branch_name FROM depositor as T, account as R WHERE T.account_number = R.account_number and S.customer_name = T.customer_name ));Note that

WITH ClauseThe WITH clause provides a way of defining a temporary view whose definition is available only to the query in which the WITH clause occurs. Find all accounts with the maximum balanceWITH max_balance (value) ASSELECT max(balance)FROM accountSELECT account_numberFROM account, max_balance WHERE account