02 slides 4

Upload: nag-dhall

Post on 03-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 02 Slides 4

    1/18

    1

    Informatik II

    D-MAVT

    Database Queries withSQL

    Topic

    Database queries with SQL

    Program

    What is SQL?Creating, altering, and deleting tablesInserting, updating, and deleting data entriesQuerying the databaseQueries across multiple tablesStoring queries as data views

    Learning goals

    Be able to understand SQL statements andtranslate them to clear text.

    Be able to write SQL statements to create/definetables in a database.

    Be able to write SQL statements to insert, modify,and delete data in a table.

    Be able to write SQL statements to query thedatabase across several tables and with searchconditions.

    Database Design Steps

    Requirements Analysis (What needs to bestored, for whom, and why?)

    Conceptual Modeling (Data entities)

    Logical Modeling (Data relationships)

    Schema Refinement (Normalization) Physical Model (Mapping to tables)

    Implementation (in a RDBMS)

  • 7/28/2019 02 Slides 4

    2/18

    2

    Further reading

    Chapter 3, Simple Queries from SQL for Web Nerds,

    Philip Greenspun (http://philip.

    greenspun.com/sql/

    )

    Database Design for Mere Mortals,

    Michael J. Hernandez

    Information Modeling and Relational Databases,

    Terry Halpin

    Database Modeling and Design,Toby J. Teorey

    What is SQL?

    Non-procedural universal language for querying andmanipulating data in RDBMS

    Structured Query Language

    Many standards out there:

    ANSI SQL SQL92 (a.k.a. SQL2) SQL99 (a.k.a. SQL3) Vendors support various sub- or super-sets of these What we discuss is common to all of them

    The two parts of SQL

    Data Definition Language (DDL) Create/alter/delete tables and their attributes

    Data Manipulation Language (DML) Insert/delete/modify entities in tables

    Query one or more tables

    Data in SQL

    1. Atomic data types

    2. Table attributes are assigned a data type

    SQL is case-insensitive for all commandsand specifiers!

  • 7/28/2019 02 Slides 4

    3/18

    3

    Basic SQL Commands

    Creating tables with CREATE

    Adding data with INSERT

    Viewing data with SELECT

    Removing data with DELETE

    Modifying data with UPDATE

    Changing tables with ALTER

    Destroying tables with DROP

    Data Definition Language

    SQL commands to define and manipulaterelations in a database.

    create tables

    alter tables

    drop tables

    create views (see later)

    Creating tables with CREATE

    Generic form

    CREATE TABLE tablename (

    column_name data_type attributes,

    column_name data_type attributes,

    );

    Table and column names cannot have spaces or bereserved words like TABLE, CREATE, etc.

    Data Types in SQL

    Characters: CHAR(20) -- fixed length

    VARCHAR(40) -- variable length

    Numbers: BIGINT, INT, SMALLINT, TINYINT

    REAL, FLOAT -- differ in precision

    MONEY

    Times and dates: DATE, TIME

    TIMESTAMP

    Binary objects (such as pictures, sound, etc.) BLOB -- stands for Binary Large OBject

    Others... All are simple

  • 7/28/2019 02 Slides 4

    4/18

    4

    Column attributes in SQL

    Keys: PRIMARY KEY -- primary key of the table

    KEY -- foreign key (will be indexed) INDEX -- field to be indexed for fast search

    Null values: NOT NULL -- field must be filled in

    Default value: DEFAULT value -- value to be used if user gives none

    Automatic values: AUTO_INCREMENT -- see later

    Others... Most of them are RDBMS-specific

    Contact Table: Physical Model

    Name Character

    Address Character

    Company Character

    Phone Number Character

    URL/Web Page Character

    Age Integer

    Height Real (float)

    Birthday Date

    When we added the entry Timestamp

    Contact Table: SQL

    CREATE TABLE contacts (

    Name VARCHAR(40),

    Address VARCHAR(60),

    Company VARCHAR(60),

    Phone VARCHAR(11),

    URL VARCHAR(80),

    Age INT,

    Height FLOAT,Birthday DATE,

    WhenEntered TIMESTAMP

    );

    Plan your tables very carefully!

    Once created, they may be difficult to change!

    Contact Table: SQL

    CREATE TABLE contacts (ContactID INT PRIMARY KEY,Name VARCHAR(40),Address VARCHAR(60),Company VARCHAR(60),Phone VARCHAR(11),URL VARCHAR(80),Age INT,Height FLOAT,

    Birthday DATE,WhenEntered TIMESTAMP

    );

    If you are going to use the relational nature of a database,dont forget you need to have a unique way to access records!

    There is a way to make the keyautomatically increment,so you dont have to worry about which one is next.

  • 7/28/2019 02 Slides 4

    5/18

    5

    Automatic key generation

    AUTO_INCREMENT field attribute:

    CREATE TABLE contacts (ContactID INT PRIMARY KEYAUTO_INCREMENT,Name VARCHAR(40),

    Database automatically gives sequentialnumbers to the entities

    Makes sure each number is unique

    Numbers will never change once assigned

    Number value has no real-world meaning

    Another Create Table Example

    CREATE TABLE customer(cust_id INT,name VARCHAR(50) NOT NULL,address VARCHAR(256) NOT NULL,

    PRIMARY KEY (cust_id));

    CREATE TABLE credit_card(cust_id INT NOT NULL,credit_card_type CHAR(5) NOT NULL,credit_card_num INT NOT NULL,

    KEY (cust_id));

    Customer

    have

    N

    1

    nameaddress

    Credit card

    card numbercard type

    Modifying Tables

    Examples:

    ALTER TABLEcustomerMODIFY COLUMN nameVARCHAR(256);

    ALTER TABLE customerADD COLUMNcredit_limit REAL;

    Generic form

    ALTER TABLE tablename

    alter specifications, ;

    ALTER specifications

    | ADD [COLUMN] column_definition [FIRST | AFTER col_name ]| ADD {INDEX|KEY} [index_type] (index_col_name,...)| MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]| DROP [COLUMN] col_name| DROP PRIMARY KEY| DROP {INDEX|KEY} index_name

    | DROP FOREIGN KEY fk_symbol| RENAME [TO] new_tbl_name

  • 7/28/2019 02 Slides 4

    6/18

    6

    Destroying tables with DROP

    Generic Form

    DROP TABLE tablename;

    Example:

    DROP TABLE contacts;

    ATTN: All data will be destroyed along with the table.

    The system will not ask twice!

    Data Manipulation Language

    SQL commands to manipulate the data in adatabase or query the database.

    insert new data entries

    update existing entries

    delete entries

    query the database

    Adding data with INSERT

    Generic Form

    INSERT INTO tablename (column_name,)

    VALUES (value,)

    Inserting a record into contacts

    INSERT INTO contacts(contactid,name,address,company,phone,url,age,height,birthday,whenentered)

    VALUES

    (1,Joe,123 Any St.,ABC,

    800-555-1212,http://abc.com,30,1.9,6/14/1972,now());

  • 7/28/2019 02 Slides 4

    7/18

    7

    Inserting a partial record

    INSERT INTO contacts(contactid,name,phone)

    VALUES (2,Jane,212-555-1212);

    Modifying data with UPDATE

    Generic Form

    UPDATE table SET column=expression

    WHERE condition;

    Example:

    UPDATE contacts SET company=AOL

    WHERE company=Time Warner;

    Removing data with DELETE

    Generic Form

    DELETE FROM table WHERE condition;

    Example:

    DELETE FROM contacts WHERE age

  • 7/28/2019 02 Slides 4

    8/18

    8

    Lets start simple

    Simplified generic form:

    Multiple tables can be joined in a single query

    Multiple conditions (search criteria) can be imposed

    Results can be sorted or grouped

    SELECT attributesFROM table(s)WHERE conditions

    Simple SQL Query

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99PowergizmoGizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePName

    SELECT *

    FROM Product

    WHERE category=Gadgets

    Product

    GizmoWorksGadgets$29.99Powergizmo

    GizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePName

    selection

    Simple SQL Query

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99Powergizmo

    GizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePName

    SELECT PName, Price, Supplier

    FROM Product

    WHERE Price > 100

    Hitachi$203.99MultiTouch

    Canon$149.99SingleTouch

    SupplierPricePName

    selection and

    projection

    Product

    Eliminating Duplicates

    Compare to:

    SELECT DISTINCT category

    FROM ProductHousehold

    Photography

    Gadgets

    Category

    SELECT category

    FROM ProductHousehold

    Photography

    Gadgets

    Gadgets

    Category

  • 7/28/2019 02 Slides 4

    9/18

    9

    More SELECT examples

    SELECT * FROM contacts;

    Display all records in the contacts table

    SELECT contactid,name FROM contacts;

    Display only the record number and names

    SELECT DISTINCT url FROM contacts;

    Display only one entry for every value of URL.

    Selection Operations

    What goes in the WHERE clause:

    x = y, x < y, x

  • 7/28/2019 02 Slides 4

    10/18

    10

    The LIKE operator

    sLIKEp: pattern matching on strings

    p may contain two special symbols: % = any sequence of characters

    _ = any single character

    Product(Name,Price,Category,Supplier)

    Find all products whose name contains gizmo:

    SELECT *FROM ProductsWHERE PName LIKE %gizmo%

    More LIKE examples

    The LIKE condition

    Allows you to look at strings that are alike SELECT * FROM contacts

    WHERE name LIKE J%;

    Display records where the name starts with J

    SELECT * FROM contactsWHERE url NOT LIKE %.com;

    Display where url does not end in .com

    Ordering the Results

    SELECT pname, price, supplier

    FROM Product

    WHERE category=gadgets AND price > 50

    ORDER BY price ASC

    ASC order in ascending order DESC in descending order List of several attributes can be given for nested

    ordering (e.g. order by price first and within all productsof same price, order by supplier:

    ORDER BY price, supplierASC)

    Ordering the Results

    SELECT category

    FROM Product

    ORDER BY Pname ASC

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99Powergizmo

    GizmoWorksGadgets$19.99GizmoSupplierCategoryPricePName

    ?Photography

    Gadgets

    Household

    GadgetsCategory

  • 7/28/2019 02 Slides 4

    11/18

    11

    Ordering the Results

    SELECT DISTINCT category

    FROM Product

    ORDER BY categoryPhotography

    Household

    Gadgets

    Category

    Joins in SQL

    Connect two or more tables:

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99Powergizmo

    GizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePNameProduct

    Company

    Japan15Hitachi

    Japan65Canon

    USA25GizmoWorks

    CountryStockPriceCName

    What isthe Connection

    betweenthem ?

    Joins

    Product(pname,price,category,supplier)

    Company(cname,stockPrice,country)

    Find all products under $200 manufactured in Japan;

    return their names and prices.

    SELECT PName, Price

    FROM Product, Company

    WHERE Supplier=CName AND Country=Japan

    AND Price

  • 7/28/2019 02 Slides 4

    12/18

    12

    Joins

    Product(pname,price,category,supplier)

    Company(cname,stockPrice,country)

    Find all countries that manufacture some product in the

    Gadgets category.

    SELECT Country

    FROM Product, Company

    WHERE Supplier=CName AND Category=Gadgets

    Joins in SQL

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99PowergizmoGizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePName

    Product Company

    Japan15Hitachi

    Japan65Canon

    USA25GizmoWorks

    CountryStockPriceCname

    USA

    USA

    Country

    Why doesUSA appear

    twice ?

    SELECT Country

    FROM Product, Company

    WHERE Supplier=CName AND Category=Gadgets

    Internal joint table

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99Powergizmo

    GizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePName

    RDBMS internally builds a joint table with ALL matches:

    USA25GizmoWorks

    Japan15Hitachi

    Japan65Canon

    USA25GizmoWorks

    CountryStockPriceCname

    USA

    USA

    Country

    SELECT Country

    FROM Product, Company

    WHERE Supplier=CName AND Category=Gadgets

    First the tables are joint and entries duplicatedThen the selection is made!

    Disambiguating Attributes

    Sometimes two relations have the same attr:Person(pname,address,worksfor)

    Company(cname,address)

    SELECT DISTINCT pname, address

    FROM Person, CompanyWHERE worksfor=cname

    SELECT DISTINCTPerson.pname, Company.address

    FROM Person, CompanyWHERE Person.worksfor=Company.cname

    Which

    address ?

  • 7/28/2019 02 Slides 4

    13/18

    13

    In fact table names introduced automatically by the system:

    Company(cname,address)

    Becomes:

    It NEEDS to be done by hand if column names areambiguous!

    SELECT cname

    FROM Company

    SELECT Company.cname

    FROM Company

    Joining together multiple tables

    SELECT name,phone,zip FROMpeople, phonenumbers, addressWHEREpeople.addressid=address.addressidANDpeople.id=phonenumbers.id ;

    3Chris3

    2Jane2

    1Joe1

    AddressidNameId

    People

    14423789PDQ3

    14454456XYZ2

    12345123ABC1

    ZipAddressCompanyAddressID

    Address

    321136

    234135

    342124

    321113

    223412

    553211

    PhoneIdPhoneID

    PhoneNumbers

    Multiple Joins

    Product(pname,price,category,supplier)

    Purchase(buyer,seller,store,product)

    Person(persname,phoneNumber,city)

    Find names of people living in Seattle that bought some

    product in the Gadgets category, and the names of the

    stores they bought such product from

    SELECT DISTINCT persname, store

    FROM Person, Purchase, Product

    WHERE persname=buyer AND product=pname AND

    city=Seattle AND category=Gadgets

    Examples

    1Max

    Alice

    2John

    TutorIDName

    Fabiana3

    Greg2

    Bob1

    NameID

    Students

    Tutors

    BobMax

    NULLAlice

    GregJohn

    Tutor.NameStudent.Name

    BobMax

    GregJohn

    Tutor.NameStudent.Name

    FabianaNULLGregJohn

    BobMax

    Tutor.NameStudent.Name

    inner

    left outer

    right outer

    BobMax

    FabianaNULL

    NULLAlice

    GregJohn

    Tutor.NameStudent.Namefull outer

  • 7/28/2019 02 Slides 4

    14/18

    14

    Different types of JOINs

    Inner Join Unmatched rows in either table arent printed

    Left Outer Join All records from the left side are printed. NULLs are filled in ifno match on the right side can be found.

    Right Outer Join All records from the right side are printed. NULLs are filled in if

    not match on the left side is found.

    Full Outer Join All records are printed. NULLs are filled in for missing records on

    either side.

    Natural Join Attributes of the same name are automatically joined

    General form of SELECT/JOIN

    SELECT columns,

    FROM left_tablejoin_type JOIN right_table ON condition;

    SELECT name,phone FROM people

    JOIN phonenumbers ONpeople.id=phonenumbers.id;

    Join type examples

    SELECT name,phone FROM people

    LEFT JOIN phonenumbers ONpeople.id=phonenumbers.id;

    SELECT name,phone FROM people

    RIGHT JOIN phonenumbers ON

    people.id=phonenumbers.id;

    SELECT name,phone FROM people

    FULL JOIN phonenumbers ONpeople.id=phonenumbers.id;

    Theta style vs. ANSI

    Theta Style (only forequi-joins)

    SELECT name,phone,zip FROM people, phonenumbers, address

    WHERE people.addressid=address.addressid ANDpeople.id=phonenumbers.id ;

    ANSI Style (for all join types)

    SELECT name,phone,zip FROM people

    JOIN phonenumbers ON people.id=phonenumbers.id

    JOIN address ON people.addressid=address.addressid;

  • 7/28/2019 02 Slides 4

    15/18

    15

    Tuple Variables

    SELECT DISTINCT x.buyer, x.seller

    FROM Purchase AS x

    WHERE x.store = BestBuy

    Find buyer and seller of all purchases that were made instore BestBuy:

    Purchase(buyer,seller,store,product)

    Saves typing with long table names!

    Tuple VariablesTuple variables introduced automatically by the system:

    Product(name,price,category,supplier)

    Becomes:

    They NEED to be specified manually when the same tableappears more than once!

    SELECT name

    FROM Product

    WHERE price > 100

    SELECT Product.name

    FROM Product AS Product

    WHERE Product.price > 100

    Tuple Variables

    SELECT DISTINCT x.storeFROM Purchase AS x, Purchase AS y

    WHERE x.product = y.product AND y.store = BestBuy

    Find all stores that sold at least one product that the storeBestBuy also sold:

    Product(pname,price,category,supplier)

    Purchase(buyer,seller,store,product)

    Person(persname,phoneNumber,city)

    JOIN of a table with itself.

    Renaming Columns

    HitachiHousehold$203.99MultiTouch

    CanonPhotography$149.99SingleTouch

    GizmoWorksGadgets$29.99Powergizmo

    GizmoWorksGadgets$19.99Gizmo

    SupplierCategoryPricePName

    SELECT Pname ASprodName, Price ASaskPrice

    FROM Product

    WHERE Price > 100

    Product

    $203.99MultiTouch

    $149.99SingleTouch

    askPriceprodName

    Query with

    renaming

  • 7/28/2019 02 Slides 4

    16/18

    16

    GROUP BY/HAVING

    The GROUP BY clause allows

    you to group results together beforeapplying aggregation functions like:

    AVG(), COUNT(), MAX(), MIN(), SUM()

    COUNT DISTINCT

    HAVING allows you to searchthe grouped results

    GROUP BY/HAVING

    7100Oracle

    4500IBM

    5500Oracle

    AmountCustomer 17100Oracle

    17100IBM

    17100OracleAmountCustomer

    4500IBM

    12600Oracle

    AmountCustomer

    SELECT customer, SUM(amount)FROM Sales;

    Sales

    SELECT customer, SUM(amount)FROM Sales GROUP BY customer;

    GROUP BY/HAVING

    7100Oracle4500IBM

    5500Oracle

    AmountCustomer

    12600Oracle

    AmountCustomer

    SELECT customer, SUM(amount)FROM Sales GROUP BY customerWHERE SUM(amount)>10000;

    Sales

    SELECT customer, SUM(amount)FROM Sales GROUP BY customerHAVING SUM(amount)>10000;

    4500IBM

    12600Oracle

    AmountCustomer

    SUM(amount)=17100which is always >10000!

    ==> Cannot use WHERE

    GROUP BY Examples

    SELECT company,count(company)FROM contactsGROUP BY company; How many times does each company appear in

    the contacts table?

    SELECT company,count(company)FROM contactsGROUP BY companyHAVING count(company) > 5; Only show counts for companies that appear

    more than 5 times.

  • 7/28/2019 02 Slides 4

    17/18

    17

    Nested SELECTs

    The WHERE subclause of a SELECTstatement can contain another SELECT

    statement:

    SELECT * FROM contactsWHERE zip NOT IN (

    SELECT zip FROM addressWHERE state=NY

    );

    (Select all contacts that have a ZIP code outside NY state.)

    Nested SELECTs

    RDBMS starts with the inner-mostSELECT and executes it

    Results are pasted into query in place ofthe executed SELECT

    Progress outward recursively until outer-most SELECT has been executed

    Views in SQL

    An SQL view is a virtual table that is derivedfrom other real or virtual tables

    Real tables are defined by CREATE TABLEcommands and are permanently stored in thedatabase

    Virtual tables are defined by the CREATE VIEW

    command to avoid defining complex SQLretrieval expressions repeatedly

    Only the definition of a view is stored, but not thedata inside. They are recomputed every time theview is used (opened)

    Creating and Deleting Views

    Generic form of View definition

    CREATE VIEW viewname AS(SELECT-Statement);

    Deleting a view

    DROP VIEW viewname;

  • 7/28/2019 02 Slides 4

    18/18

    18

    Example of a View definition

    Views are stored queries and are created onthe basis of a SELECT statement:

    CREATE VIEW contactview AS

    (SELECT name,phone,zip FROM

    people,phonenumbers,address

    WHERE people.id=phonenumbers.id ANDpeople.addressid=address.addressid);

    Views can be used in other SELECT statementsas if they were tables.