lecture05sql-100311002645-phpapp02

Embed Size (px)

Citation preview

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    1/59

    2 December 2005

    Introduction to Databases

    Structured Query Language

    Prof. Beat Signer

    Department of Computer ScienceVrije Universiteit Brussel

    http://vub.academia.edu/BeatSigner

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    2/59

    Beat Signer - Department of Computer Science - [email protected] 2March 11, 2010

    Context of Today's Lecture

    AccessMethods

    SystemBuffers

    AuthorisationControl

    IntegrityChecker

    CommandProcessor

    ProgramObject Code

    DDLCompiler

    FileManager

    BufferManager

    RecoveryManager

    Scheduler

    QueryOptimiser

    TransactionManager

    QueryCompiler

    Queries

    CatalogueManager

    DMLPreprocessor

    DatabaseSchema

    ApplicationPrograms

    Database andSystem Catalog

    DatabaseManager

    DataManager

    DBMS

    Programmers Users DB Admins

    Based on 'Components of a DBMS', Database Systems,

    T. Connolly and C. Begg, Addison-Wesley 2010

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    3/59

    Beat Signer - Department of Computer Science - [email protected] 3March 11, 2010

    Structured Query Language (SQL)

    Declarative query languageto create database schemas,insert, update and query information based on a datadefinition and data manipulation language

    Data definition language (DDL) definition of database structure

    data access control

    Data manipulation language (DML)

    query language to read data as well as commands to create,update and delete tuples (CRUD operations)

    Transaction control

    Embedded SQL and dynamic SQL

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    4/59

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    5/59

    Beat Signer - Department of Computer Science - [email protected] 5March 11, 2010

    History of SQL ...

    SQL:2003 window functions, XML-related features, ...

    SQL:2006 XML Query Language (XQuery) support, ...

    SQL:2008

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    6/59

    Beat Signer - Department of Computer Science - [email protected] 6March 11, 2010

    SQL "Standard"

    Each specific SQL implementation by a database vendoris called a dialect

    The vendors implement parts of the SQL standard

    (e.g. most implement SQL-92) but add their vendorspecific extensions

    Most relational database vendors conform to a set ofCore SQL features but portability might still be limited

    due to missing or additional features We will use the Extended BackusNaur Form (EBNF) to

    describe different SQL concepts http://en.wikipedia.org/wiki/Extended_Backus-Naur_Form

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    7/59Beat Signer - Department of Computer Science - [email protected] 7March 11, 2010

    Data Definition Language (DDL)

    The data definition language (DDL) is used to specify therelation schemas as well as other information about therelations

    relation schemas attribute domain types

    integrity constraints

    relation indices

    access information

    physical storage structure of relations

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    8/59Beat Signer - Department of Computer Science - [email protected] 8March 11, 2010

    Database Creation

    The concrete process of creating a new database mightdiffer for different relational database products

    According to the SQL standard, an SQL environment

    contains one ore more catalogues

    Each catalogue manages various metadata set of schemas consisting of

    - tables

    - views- assertions

    - indexes

    users and user groups

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    9/59Beat Signer - Department of Computer Science - [email protected] 9March 11, 2010

    Database Creation ...

    The creation of catalogues is not covered by the SQLstandard and therefore implementation specific

    Schemas can be created and deleted via the CREATE and

    DROP statements

    The default parameter in the DROP SCHEMA statement isRESTRICT only empty schema can be deleted

    If CASCADE is specified, all objects associated with theschema will be dropped

    createSchema = "CREATE SCHEMA" , name , "AUTHORIZATION" , creator ,[ ddlStatements ];

    dropSchema = "DROP SCHEMA" , name , [ "RESTRICT" | "CASCADE" ];

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    10/59Beat Signer - Department of Computer Science - [email protected] 10March 11, 2010

    Table Definition

    createTable = "CREATE TABLE" , table , "(" ,( columnElement | tableConstraint ) ,{ "," , ( columnElement | tableConstraint ) } , ")";

    columnElement = column , datatype ,

    [ "DEFAULT" , ( value | "NULL" ) ] , { columnConstraint };

    columnConstraint = "NOT NULL" | "UNIQUE" | "PRIMARY KEY" |( "REFERENCES" , table , [ "(" , column , ")" ] ,{ referentialAction } ) |

    ( "CHECK (" , searchCondition , ")" );

    tableConstraint = ( ( "UNIQUE" | "PRIMARY KEY ) , "(" , column ,

    { "," , column } , ")" ) |( "FOREIGN KEY (" , column , { "," , column } , ")" ,"REFERENCES" , table , [ "(" , column , { "," , column } , ")" ] ,{ referentialAction } ) |

    ( "CHECK (" , searchCondition , ")" );

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    11/59Beat Signer - Department of Computer Science - [email protected] 11March 11, 2010

    Table Definition ...

    referentialAction = ( "ON UPDATE" | "ON DELETE" ) ,( "CASCADE" | "SET DEFAULT" | "SET NULL" | "NO ACTION" );

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    12/59Beat Signer - Department of Computer Science - [email protected] 12March 11, 2010

    Table Constraints

    We can have only one PRIMARY KEY constraint butmultiple UNIQUE constraints if no primary key is defined, duplicates are allowed (bag)

    Referential integrity a foreign key always has to have a matching value in the

    referenced table (or it can be null)

    different referential actionscan be defined for update (ON UPDATE)and delete (ON DELETE) operations on the referenced candidate

    key- CASCADE: propagate operations to the foreign keys which might lead to further

    cascaded operations

    - SET DEFAULT: set the foreign keys to their default value

    - SET NULL: set the foreign keys to NULL

    - NO ACTION: the operation on the candidate key will be rejected (default)

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    13/59Beat Signer - Department of Computer Science - [email protected] 13March 11, 2010

    SQL Datatypes

    Boolean the domain of boolean values consist of the two truth values TRUE

    and FALSE

    a thrid UNKNOWN truth value is used to represent NULL values

    Bit data

    fixed or varying sequence of binary digits (0 or 1)

    boolean = "BOOLEAN";

    bit = fixedBit | varyingBit;fixedBit = "BIT" , [ "(" , length , ")" ];varyingBit = "BIT VARYING" , [ "(" , length , ")" ];

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    14/59Beat Signer - Department of Computer Science - [email protected] 14March 11, 2010

    SQL Datatypes ...

    Character data fixed or varying sequence of characters

    CHAR always requires the fixed number of characters

    Large character data or binary data

    char = fixedChar | varyingChar;fixedChar = "CHAR" , [ "(" , length , ")" ];varyingChar = "VARCHAR" , [ "(" , length , ")" ];

    lob = clob | blob;clob = "CLOB" , [ "(" , size , ")" ];blob = "BLOB" , [ "(" , size , ")" ];

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    15/59Beat Signer - Department of Computer Science - [email protected] 15March 11, 2010

    SQL Datatypes ...

    Numeric data

    The DECIMAL datatype is sometimes used as synonym for

    the NUMERIC datatype

    numeric = decimal | int | smallInt | float | real | double;decimal = "DECIMAL" , [ "(" ,precision , [ "," , scale ] , ")" ];int = "INTEGER";

    smallInt = "SMALLINT";float = "FLOAT" , [ "(" , precision , ")" ];real = "REAL";double = "DOUBLE PRECISION";

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    16/59Beat Signer - Department of Computer Science - [email protected] 16March 11, 2010

    SQL Datatypes ...

    Datetime data

    Format of the datetime values

    date: YYYY-MM-DD time: hh:mm:ss.p hh:mm

    timestamp: YYYY-MM-DD hh:mm:ss.p hh:mm

    datetime = date | time | timestamp;date = "DATE";time = "TIME" , [ "(" ,precision , ")" ] ,

    [ "WITH TIME ZONE" , timezone ];timestamp = "TIMESTAMP" , [ "(" ,precision , ")" ] ,[ "WITH TIME ZONE" , timezone ];

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    17/59Beat Signer - Department of Computer Science - [email protected] 17March 11, 2010

    SQL Datatypes ...

    For further details about the presented datatypes as wellas information about vendor-specific datatypes one hasto consult the specific database manuals

    datatype = boolean | bit | char | numeric | datetime | lob;

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    18/59Beat Signer - Department of Computer Science - [email protected] 18March 11, 2010

    Example Database

    cdID name duration price year

    93 Falling into Place 2007 17.90 2007

    117 Moudi 3156 15.50 1996

    3 Chromatic 3012 16.50 1996

    customerID name street postcode city

    1 Urs Frei Bahnhofstrasse 7 8001 Zurich

    2 Pieter de Rover Pleinlaan 25 1050 Brussels

    5 Robert Michelin 12 Rue Louise 75008 Paris

    53 Beat Meier Bergstrasse 18 8037 Zurich

    8 Urs Frei ETH Zentrum 8092 Zurich

    customer

    cd

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    19/59Beat Signer - Department of Computer Science - [email protected] 19March 11, 2010

    Example Database

    orderID customerID cdID date amount status

    1 53 93 13.02.2010 2 open

    2 2 117 15.02.2010 1 delivered

    order

    supplierID name postcode city

    5 Urs Frei 8040 Zurich

    2 Franz Hohler 5000 Aarau

    supplier

    Customer (customerID, name, street, postcode, city)CD (cdID, name, duration, price, year)Order (orderId, customerID, cdID, date, amount, status)Supplier (supplierID, name, postcode, city)

    relational database schema

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    20/59Beat Signer - Department of Computer Science - [email protected] 20March 11, 2010

    Table Definition Example

    CREATE TABLE Customer (customerID INTEGER CHECK (customerID > 0) PRIMARY KEY,name VARCHAR(30) NOT NULL,street VARCHAR(30) NOT NULL,postcode SMALLINT CHECK (postcode > 0),

    city VARCHAR(20));

    CREATE TABLE CD (cdID INTEGER PRIMARY KEY,name VARCHAR(30) NOT NULL,duration TIME,price NUMERIC(6,2),

    year SMALLINT);

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    21/59Beat Signer - Department of Computer Science - [email protected] 21March 11, 2010

    Table Definition Example ...

    CREATE TABLE Supplier (supplierID INTEGER PRIMARY KEY,name VARCHAR(30) NOT NULL,postcode SMALLINT CHECK (postcode > 0),city VARCHAR(20)

    );

    CREATE TABLE Order (orderID INTEGER CHECK (orderID > 0) PRIMARY KEY,customerID INTEGER,cdID INTEGER ,date DATE,amount INTEGER,

    Status VARCHAR(20) NOT NULL DEFAULT 'open',UNIQUE (customerID, cdID, date),FOREIGN KEY (customerID) REFERENCES Customer(customerID)ON UPDATE CASCADE ON DELETE SET NULL,

    FOREIGN KEY (cdID) REFERENCES CD(cdID)ON UPDATE CASCADE

    );

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    22/59

    Beat Signer - Department of Computer Science - [email protected] 22March 11, 2010

    Data Manipulation

    After a table has been created, we can use the INSERTcommand to add tuples unspecified attribute values are set to the default value or NULL

    Example

    The DELETE statement can be used to delete tuples

    INSERT INTO Customer VALUES(8, 'Urs Frei', 'ETH Zentrum', 8001, 'Zurich');

    insert = "INSERT INTO" , table ,[ "(" , column , { "," , column } , ")" ] ,( "VALUES (" , expr , { "," , expr } , ")" ) | ( "(" , query , ")" );

    delete = "DELETE FROM" , table [ "WHERE" , searchCondition ];

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    23/59

    Beat Signer - Department of Computer Science - [email protected] 23March 11, 2010

    Data Manipulation ...

    Tuples can be updated via the UPDATE statement

    Example

    The DROP TABLE statement can be used to delete arelation from the database

    UPDATE Customer SET name = 'Peter Frei' WHERE customerID = 8;

    update = "UPDATE" , table , "SET" ,column , "=" , ( "NULL" | expr | "(" , query , ")" ) ,{ "," , column , "=" , ("NULL" | expr | "(" , query , ")" ) } ,

    [ "WHERE" , searchCondition ];

    dropTable = "DROP TABLE" , table;

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    24/59

    Beat Signer - Department of Computer Science - [email protected] 24March 11, 2010

    Data Manipulation ...

    A relation schema can be modified via the ALTER TABLEcommand

    Example

    alterTable = "ALTER TABLE" , table , "ADD" ,

    ( columnElement | columnConstraint );

    ALTER TABLE Customer ADD birthdate DATE;

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    25/59

    Beat Signer - Department of Computer Science - [email protected] 25March 11, 2010

    Data Manipulation ...

    The CREATE INDEX statement can be used to create anindex an index provides fast access to data without reading the whole

    relation

    the selection of the best index attributes is very challenging- tradeoff between the faster access to indexed data and the additional effort to

    maintain the indices when updating or inserting new data

    Example

    createIndex = "CREATE INDEX" , name , "ON" , table, "(" , column ,{ "," , column} , ")";

    CREATE INDEX nameIndex ON Customer (name);

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    26/59

    Beat Signer - Department of Computer Science - [email protected] 26March 11, 2010

    Expressions

    expr = exprElement { ( "+" | "-" | "*" | "/" ) , exprElement };

    exprElement = column | value |"COUNT" , "(" ( "*" | ( [ "ALL" | "DISTINCT" ] , column ) , ")" |( "MIN" | "MAX" ) , "(" , expr , ")" |( "SUM" | "AVG" ) , "(" , [ "DISTINCT" ] , expr , ")";

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    27/59

    Beat Signer - Department of Computer Science - [email protected] 27March 11, 2010

    SELECT Clause

    The SELECT statement can be used to retrieveinformation from one or multiple database tables can perform the relational algebra's selection, projectionandjoin

    operation in a single SELECT command

    query = select { ("UNION" | "INTERSECT" | "EXCEPT") , [ "ALL" ] , select};

    select = "SELECT" [ "ALL" | "DISTINCT" ] ,("*" | ( expr , [ "AS" , newName ] ,{ "," , expr , [ "AS" , newName ] } ) ,

    "FROM" , table , [ correlationVar ] ,

    { "," , table , [ correlationVar ] } ,[ "WHERE" , searchCondition ] ,[ "GROUP BY" , column , { "," , column } ,[ "HAVING" , searchCondition ] ];

    orderedQuery = query , "ORDER BY" , column , [ "ASC" | "DESC" ] ,{ "," , column , [ "ASC" | "DESC" ] };

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    28/59

    Beat Signer - Department of Computer Science - [email protected] 28March 11, 2010

    SELECT Clause ...

    searchCondition = [ "NOT" ] , search ,{ ( "AND" | "OR" ) , [ "NOT" ] , search };

    search = ( expr , [ "NOT" ] , "BETWEEN" , expr , "AND" , expr ) |( expr , [ "NOT" ] , "LIKE" , "'" , ( string | "_" | "%" ) ,{ string | "_" | "%" } , "'" ) |

    ( column | ( "(" , expr , ")" ) , "IS" , [ "NOT" ] , "NULL" ) |( expr , ( "=" | "" | ">" | ">=" | "

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    29/59

    Beat Signer - Department of Computer Science - [email protected] 29March 11, 2010

    SELECT Clause ...

    The SELECT statement contains the following maincomponents SELECT

    - specifies the columns to appear in the result (projection in relational algebra)

    FROM

    - specifies the relations to be used (cartesian product in relational algebra)

    WHERE

    - filters the tuples (selection in relational algebra)

    - join conditionsare explicitly specified in the WHERE clause

    GROUP BY

    - groups rows with the same column values

    - the HAVING construct can be used to further filter the groups

    ORDER BY

    - defines the order of the resulting tuples

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    30/59

    Beat Signer - Department of Computer Science - [email protected] 30March 11, 2010

    SELECT Clause ...

    The orderof clauses in the SELECT statement cannot bechanged

    Note that the SELECT is equivalent to a relational algebra

    projection

    In contrast to the relational algebra, SQL does noteliminate duplicates automatically the automatic elimination of duplicates is time consuming

    user has to eliminate duplicates explicitly via DISTINCT keyword

    SELECT A1, A2,..., AnFROM r1, r2,..., rmWHERE P

    pA1,A2,...,An(sP(r1 r2 ... rm)is equivalent to

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    31/59

    Beat Signer - Department of Computer Science - [email protected] 31March 11, 2010

    SELECT Clause ...

    A '*' can be used in the SELECT clause as a shortcut toget all tuple attributes

    SELECT *FROM Customer;

    customerID name street postcode city

    1 Urs Frei Bahnhofstrasse 7 8001 Zurich2 Pieter de Rover Pleinlaan 25 1050 Brussels

    5 Robert Michelin 12 Rue Louise 75008 Paris

    53 Beat Meier Bergstrasse 18 8037 Zurich

    8 Urs Frei ETH Zentrum 8092 Zurich

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    32/59

    Beat Signer - Department of Computer Science - [email protected] 32March 11, 2010

    SELECT Clause ...

    Duplicate tuplesresulting from a projection to specificattributes are not eliminated by default

    SELECT nameFROM Customer;

    name

    Urs FreiPieter de Rover

    Robert Michelin

    Beat Meier

    Urs Frei

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    33/59

    Beat Signer - Department of Computer Science - [email protected] 33March 11, 2010

    SELECT Clause ...

    The DISTINCT keyword can be used to eliminateduplicates

    SELECT DISTINCT nameFROM Customer;

    name

    Urs FreiPieter de Rover

    Robert Michelin

    Beat Meier

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    34/59

    Beat Signer - Department of Computer Science - [email protected] 34March 11, 2010

    Computed Attributes and Rename

    Computations can be performed in the SELECT clause

    multiple numeric attributescan be used in a computation The rename operation(AS) is used to rename relations

    as well as attributes computed column has no name by default

    also used when multiple relations have the same attribute names

    SELECT name,price * 1.5 AS newPriceFROM CD;

    name newPrice

    Falling into Place 26.85Moudi 23.20

    Chromatic 24.75

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    35/59

    Beat Signer - Department of Computer Science - [email protected] 35March 11, 2010

    WHERE Clause

    In the WHERE clause we can use five basic predicates(search conditions) comparison

    - compare two expressions

    range- check whether the value is within a specified range of values (BETWEEN)

    set membership- check whether the value is equal to a value of a given set (IN)

    pattern matching- test whether the expression matches a specifies string pattern (LIKE)

    check forNULL values- check whether the expression is a NULL value (IS NULL)

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    36/59

    Beat Signer - Department of Computer Science - [email protected] 36March 11, 2010

    WHERE Clause ...

    SELECT name, postcodeFROM CustomerWHERE city = 'Zurich' AND postcode >= 8040;

    name postcode

    Urs Frei 8092

    SELECT name, priceFROM CDWHERE price BETWEEN 15.0 AND 17.0;

    name price

    Moudi 15.50

    Chromatic 16.50

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    37/59

    Beat Signer - Department of Computer Science - [email protected] 37March 11, 2010

    WHERE Clause ...

    Check for set membership with the IN construct

    SELECT *FROM CustomerWHERE city IN ('Zurich', 'Brussels');

    customerID name street postcode city

    1 Urs Frei Bahnhofstrasse 7 8001 Zurich

    2 Pieter de Rover Pleinlaan 25 1050 Brussels

    53 Beat Meier Bergstrasse 18 8037 Zurich

    8 Urs Frei ETH Zentrum 8092 Zurich

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    38/59

    Beat Signer - Department of Computer Science - [email protected] 38March 11, 2010

    Pattern Matching

    Strings are enclosed in single quotes use a double single quote for escaping

    The LIKE operator is used for pattern matching

    the underscore(_) is a placeholder for a single character the percentsign (%) is a placeholder for any substring

    e.g. LIKE '_e%'

    name

    Urs Frei

    Beat Meier

    SELECT DISTINCT nameFROM CustomerWHERE name LIKE '%ei%';

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    39/59

    Beat Signer - Department of Computer Science - [email protected] 39March 11, 2010

    Null Values

    In SQL missing (unknown) information is represented byNULL values three-valued logic (3VL) based on True, False and Unknown

    True False Unknown

    True True False Unknown

    False False False False

    Unknown Unknown False Unknown

    ANDTrue False Unknown

    True True True True

    False True False Unknown

    Unknown True Unknown Unknown

    OR

    =

    True False Unknown

    True True False Unknown

    False False True Unknown

    Unknown Unknown Unknown Unknown

    NOT

    True False Unknown

    False True Unknown

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    40/59

    Beat Signer - Department of Computer Science - [email protected] 40March 11, 2010

    Null Values ...

    The NULL keyword can also be used in predicates tocheck for null values

    Note that a check for NULL is not the same as a check forthe empty String ''

    SELECT *FROM CD

    WHEREprice IS NOT NULL;

    cdID name duration price year

    1 Falling into Place 2007 17.90 2007

    2 Moudi 3156 15.50 1996

    3 Chromatic 3012 16.50 1996

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    41/59

    Beat Signer - Department of Computer Science - [email protected] 41March 11, 2010

    FROM Clause

    The FROM clause creates a cartesian product of multiplerelations and can be used to specifyjoin operations

    In a previous lecture we have seen the following

    relational algebra expression- "list the name and street of customers whose order is still open"

    - pname, street(sstatus="open"(order customer))

    - the same can be achieved in SQL by explicitly specifying the matching attributes

    SELECT name, streetFROM Customer, OrderWHERE Order.customerID = Customer.customerID AND status = 'open';

    name street

    Beat Meier Bergstrasse 18

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    42/59

    Beat Signer - Department of Computer Science - [email protected] 42March 11, 2010

    Inner and Outer Joins

    Note that there exist SQL extensions to perform joinoperations between two relations R and S in the FROM

    clause

    Inner Joins

    Outer Joins

    SELECT * FROM R NATURAL JOIN S;SELECT * FROM R CROSS JOIN S;SELECT * FROM R JOIN S ON R.A > S.B;

    SELECT * FROM R LEFT OUTER JOIN S ON R.A = S.B;SELECT * FROM R RIGHT OUTER JOIN S ON R.A = S.B;SELECT * FROM R FULL OUTER JOIN S ON R.A = S.B;

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    43/59

    Beat Signer - Department of Computer Science - [email protected] 43March 11, 2010

    Correlation Variable

    A correlation variable can be used as an alias for a table

    Example "Find all pairs of CDs that were produced in the same year"

    SELECT c1.name AS name1, c2.name AS name2FROM CD c1, CD c2WHERE c1.year = c2.year AND c1.cdID < c2.cdID;

    name1 name2

    Moudi Chromatic

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    44/59

    Beat Signer - Department of Computer Science - [email protected] 44March 11, 2010

    Sorting

    The ORDER BY clause can be used to arrange the resulttuples in acending (ASC) or descending (DESC) order multiple sort keys can be specified; highest priority first

    tuples with NULL values are either before or after non-NULL tuples

    SELECT name, street, cityFROM CustomerORDER BY city ASC, name DESC;

    name street city

    Pieter de Rover Pleinlaan 25 Brussels

    Robert Michelin 12 Rue Louise Paris

    Urs Frei ETH Zentrum Zurich

    Urs Frei Bahnhofstrasse 7 Zurich

    Beat Meier Bergstrasse 18 Zurich

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    45/59

    Beat Signer - Department of Computer Science - [email protected] 45March 11, 2010

    Set Operations

    The UNION, INTERSECT and EXCEPT operations correspondto the , and - relational algebra operations the relations have to be compatible (same attributes)

    these operations remove duplicates by default- the ALL keyword has to be used to retain duplicates

    (SELECT nameFROM Customer)INTERSECT(SELECT nameFROM Supplier);

    name

    Urs Frei

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    46/59

    Beat Signer - Department of Computer Science - [email protected] 46March 11, 2010

    Aggregate Functions and Grouping

    In SQL there are five aggregate functions (MIN, MAX, AVG,SUM and COUNT) that take a set or multiset of values asinput and return a single value

    Example "Find the number of customers in each city"

    Aggregate functions (except COUNT(*)) ignore NULLvalues in the input set input set might be empty in which case NULL is returned

    SELECT city, COUNT(customerID) AS numberFROM CustomerGROUP BY city;

    city number

    Zurich 3

    Brussels 1

    Paris 1

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    47/59

    Beat Signer - Department of Computer Science - [email protected] 47March 11, 2010

    Subqueries

    A subquery is a SELECT FROM WHERE expression that isnested within another query e.g. via check for set membership (IN or NOT IN)

    Example "Find all the suppliers who are no customers"

    SELECT DISTINCT nameFROM SupplierWHERE name NOT IN (SELECT name

    FROM Customer);

    name

    Franz Hohler

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    48/59

    Beat Signer - Department of Computer Science - [email protected] 48March 11, 2010

    Nested Subqueries ...

    Example "Find all CDs with a price smaller than average"

    SELECT *FROM CDWHEREprice < (SELECT AVG(price)

    FROM CD;

    cdID name duration price year

    2 Moudi 3156 15.50 1996

    3 Chromatic 3012 16.50 1996

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    49/59

    Beat Signer - Department of Computer Science - [email protected] 49March 11, 2010

    Set Comparison

    For nested queries with conditions like "greater than atleast one"we can use these set comparison operators > SOME, >= SOME, < SOME, ALL (SELECT postcode

    FROM Supplier);

    name postcode

    Robert Michelin 75008

    Urs Frei 8092

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    50/59

    Beat Signer - Department of Computer Science - [email protected] 50March 11, 2010

    Existence Test

    The EXISTS operator can be used to check if a tupleexists in a subquery

    Example

    SELECT nameFROM CustomerWHERE EXISTS (SELECT *

    FROM SupplierWHERE Supplier.name = Customer.name);

    name

    Urs Frei

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    51/59

    Beat Signer - Department of Computer Science - [email protected] 51March 11, 2010

    Derived Relations

    As subquery expression can also be used in the FROMclause in this case a name has to be given to the relation

    Example "Find the number of customers in the city with the most

    customers"

    SELECT MAX(noCustomers) AS maxFROM (SELECT city, COUNT(customerID)

    FROM CustomerGROUP BY city) AS CityTotal(city,noCustomers);

    max

    3

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    52/59

    Beat Signer - Department of Computer Science - [email protected] 52March 11, 2010

    WITH Clause

    The WITH clause can be used to improve the readabilityby introducing temporary new relations introduced only in SQL:1999 and not supported by all databases

    Example "Find all customers who bought one of the most expensive CDs"

    WITH Expensive(price) ASSELECT MAX(price)FROM CD

    SELECT Customer.name

    FROM Customer, CD, OrderWHERE CD.price = Expensive.price AND CD.cdID = Order.cdID ANDOrder.customerID = Customer.customerID;

    name

    Beat Meier

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    53/59

    Beat Signer - Department of Computer Science - [email protected] 53March 11, 2010

    Views

    New virtual relations (views) can be defined on top of anexisting logical model simplify queries

    provide access to only parts of the logical model (security)

    Some DBMS allow views to be stored (materialisedviews) materialised views have to be updated when its relations change

    createView = "CREATE VIEW" , table ,[ "(" , column , { "," , column } , ")" ] ,"AS" , query;

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    54/59

    Beat Signer - Department of Computer Science - [email protected] 54March 11, 2010

    Views

    Example

    Note that a view can be used like any other relation

    Views are useful for queries but they present a seriousproblem for UPDATE, INSERT and DELETE operations

    CREATE VIEW CustomerCD ASSELECT Customer.customerID, Customer.name, CD.cdID, CD.name AS cdNameFROM Customer, Order, CD

    WHERE Customer.customerID = Order.customerID ANDOrder.cdID = CD.cdID;

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    55/59

    Beat Signer - Department of Computer Science - [email protected] 55March 11, 2010

    Transactions

    A transactionconsists of a sequence of query and/orupdate statements

    A transaction explicitly starts when an SQL statement is

    executed and is ended by a COMMIT statement

    a ROLLBACK statement

    In many SQL implementations each SQL statement is a

    transaction on its own this default behaviour can be disabled

    SQL:1999 introduced BEGIN ATOMIC ... END blocks

    Transactions will be discussed in detail later

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    56/59

    Beat Signer - Department of Computer Science - [email protected] 56March 11, 2010

    Homework

    Study the following chapter of theDatabase System Conceptsbook chapter 3

    - SQL

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    57/59

    Beat Signer - Department of Computer Science - [email protected] 57March 11, 2010

    Exercise 5

    Structured Query Language (SQL)

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    58/59

    Beat Signer - Department of Computer Science - [email protected] 58March 11, 2010

    References

    A. Silberschatz, H. Korth and S. Sudarshan, DatabaseSystem Concepts(Fifth Edition), McGraw-Hill, 2005

  • 8/8/2019 lecture05sql-100311002645-phpapp02

    59/59

    Next Week

    Advanced SQL