Lecture SQL

Embed Size (px)

Citation preview

  • 8/13/2019 Lecture SQL

    1/78

    CSE544: SQL

    Monday 3/27 and Wednesday 3/29, 2006

  • 8/13/2019 Lecture SQL

    2/78

  • 8/13/2019 Lecture SQL

    3/78

    SQL

    Data Definition Language (DDL)

    Create/alter/delete tables and their attributes

    Following lectures...

    Data Manipulation Language (DML)

    Query one or more tablesdiscussed next !

    Insert/delete/modify tuples in tables

  • 8/13/2019 Lecture SQL

    4/78

    Tables in SQL

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography Canon

    MultiTouch $203.99 Household Hitachi

    Product

    Attribute namesTable name

    Tuples or rows

  • 8/13/2019 Lecture SQL

    5/78

  • 8/13/2019 Lecture SQL

    6/78

    Data Types in SQL

    Atomic types:

    Characters: CHAR(20), VARCHAR(50)

    Numbers: INT, BIGINT, SMALLINT, FLOAT Others: MONEY, DATETIME,

    Every attribute must have an atomic typeHence tables are flatWhy ?

  • 8/13/2019 Lecture SQL

    7/78

    Tables Explained

    A tuple = a record

    Restriction: all attributes are of atomic type

    A table = a set of tuples

    Like a list

    but it is unorderd:

    no first(), no next(), no last().

  • 8/13/2019 Lecture SQL

    8/78

    SQL Query

    Basic form: (plus many many more bells and whistles)

    SELECT

    FROM

    WHERE

  • 8/13/2019 Lecture SQL

    9/78

    Simple SQL Query

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography CanonMultiTouch $203.99 Household Hitachi

    SELECT *

    FROM Product

    WHERE category=Gadgets

    Product

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorksselection

  • 8/13/2019 Lecture SQL

    10/78

    Simple SQL Query

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography CanonMultiTouch $203.99 Household Hitachi

    SELECT PName, Price, Manufacturer

    FROM Product

    WHERE Price > 100

    Product

    PName Price Manufacturer

    SingleTouch $149.99 Canon

    MultiTouch $203.99 Hitachi

    selection and

    projection

  • 8/13/2019 Lecture SQL

    11/78

    Notation

    Product(PName, Price, Category, Manfacturer)

    Answer(PName, Price, Manfacturer)

    Input Schema

    Output Schema

    SELECT PName, Price, Manufacturer

    FROM Product

    WHERE Price > 100

  • 8/13/2019 Lecture SQL

    12/78

    Details

    Case insensitive:

    Same: SELECT Select select

    Same: Product product Different: Seattle seattle

    Constants: abc - yes

    abc - no

  • 8/13/2019 Lecture SQL

    13/78

    The LIKEoperator

    s LIKEp: pattern matching on strings

    p may contain two special symbols:

    % = any sequence of characters

    _ = any single character

    SELECT *FROM ProductsWHERE PName LIKE%gizmo%

  • 8/13/2019 Lecture SQL

    14/78

    Eliminating Duplicates

    SELECT DISTINCTcategory

    FROM Product

    Compare to:

    SELECT category

    FROM Product

    Category

    Gadgets

    Gadgets

    Photography

    Household

    Category

    Gadgets

    Photography

    Household

  • 8/13/2019 Lecture SQL

    15/78

    Ordering the Results

    SELECT pname, price, manufacturer

    FROM Product

    WHERE category=gizmo AND price > 50ORDER BY price, pname

    Ties are broken by the second attribute on the ORDER BY list, etc.

    Ordering is ascending, unless you specify the DESC keyword.

  • 8/13/2019 Lecture SQL

    16/78

    SELECT Category

    FROM Product

    ORDER BY PName

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography Canon

    MultiTouch $203.99 Household Hitachi

    ?

    SELECT DISTINCTcategory

    FROM Product

    ORDER BYcategory

    SELECT DISTINCTcategory

    FROM Product

    ORDER BYPName

    ?

    ?

  • 8/13/2019 Lecture SQL

    17/78

    Keys and Foreign Keys

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography Canon

    MultiTouch $203.99 Household Hitachi

    Product

    Company

    CName StockPrice Country

    GizmoWorks 25 USA

    Canon 65 Japan

    Hitachi 15 Japan

    Key

    Foreign

    key

  • 8/13/2019 Lecture SQL

    18/78

    Joins

    Product (pname, price, category, manufacturer)

    Company (cname, stockPrice, country)

    Find all products under $200 manufactured in Japan;return their names and prices.

    SELECT PName, Price

    FROM Product, CompanyWHERE Manufacturer=CName AND Country=Japan

    AND Price

  • 8/13/2019 Lecture SQL

    19/78

    Joins

    PName Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography Canon

    MultiTouch $203.99 Household Hitachi

    ProductCompany

    Cname StockPrice Country

    GizmoWorks 25 USA

    Canon 65 Japan

    Hitachi 15 Japan

    PName Price

    SingleTouch $149.99

    SELECT PName, Price

    FROM Product, CompanyWHERE Manufacturer=CName AND Country=Japan

    AND Price

  • 8/13/2019 Lecture SQL

    20/78

    More Joins

    Product (pname, price, category, manufacturer)

    Company (cname, stockPrice, country)

    Find all Chinese companies that manufacture productsboth in the electronic and toy categories

    SELECT cname

    FROM

    WHERE

  • 8/13/2019 Lecture SQL

    21/78

    A Subtlety about Joins

    Product (pname, price, category, manufacturer)

    Company (cname, stockPrice, country)

    Find all countries that manufacture some product in theGadgets category.

    SELECT Country

    FROM Product, CompanyWHERE Manufacturer=CName AND Category=Gadgets

    Unexpected duplicates

  • 8/13/2019 Lecture SQL

    22/78

    A Subtlety about Joins

    Name Price Category Manufacturer

    Gizmo $19.99 Gadgets GizmoWorks

    Powergizmo $29.99 Gadgets GizmoWorks

    SingleTouch $149.99 Photography Canon

    MultiTouch $203.99 Household Hitachi

    ProductCompany

    Cname StockPrice Country

    GizmoWorks 25 USA

    Canon 65 Japan

    Hitachi 15 Japan

    Country

    ??

    ??

    What isthe problem ?

    Whats the

    solution ?

    SELECT Country

    FROM Product, Company

    WHERE Manufacturer=CName AND Category=Gadgets

  • 8/13/2019 Lecture SQL

    23/78

    Tuple Variables

    SELECT DISTINCTpname, address

    FROM Person, Company

    WHERE worksfor = cname

    Which

    address ?

    Person(pname, address, worksfor)

    Company(cname, address)

    SELECT DISTINCTPerson.pname, Company.address

    FROM Person, CompanyWHERE Person.worksfor = Company.cname

    SELECT DISTINCTx.pname, y.address

    FROM Person AS x, Company AS y

    WHERE x.worksfor = y.cname

  • 8/13/2019 Lecture SQL

    24/78

    Meaning (Semantics) of SQL

    QueriesSELECTa1, a2, , akFROM R1AS x1, R2AS x2, , RnAS xnWHERE Conditions

    Answer = {}forx1inR1do

    forx2inR2do

    ..forxninRndoifConditions

    thenAnswer = Answer {(a1,,ak)}returnAnswer

  • 8/13/2019 Lecture SQL

    25/78

    SELECT DISTINCTR.A

    FROM R, S, TWHERE R.A=S.A OR R.A=T.A

    An Unintuitive Query

    Computes R (S T) But what if S = f?

    What does it compute ?

  • 8/13/2019 Lecture SQL

    26/78

    Subqueries Returning Relations

    SELECT Company.city

    FROM Company

    WHERE Company.name IN(SELECTProduct.maker

    FROM Purchase , Product

    WHEREProduct.pname=Purchase.product

    AND Purchase .buyer = Joe Blow);

    Return cities where one can find companies that manufactureproducts bought by Joe Blow

    Company(name, city)

    Product(pname, maker)

    Purchase(id, product, buyer)

  • 8/13/2019 Lecture SQL

    27/78

    Subqueries Returning Relations

    SELECT Company.cityFROM Company, Product, Purchase

    WHERE Company.name= Product.maker

    AND Product.pname = Purchase.product

    AND Purchase.buyer = Joe Blow

    Is it equivalent to this ?

    Beware of duplicates !

  • 8/13/2019 Lecture SQL

    28/78

    Removing Duplicates

    Now

    they are

    equivalent

    SELECTDISTINCTCompany.city

    FROM Company

    WHERE Company.name IN

    (SELECTProduct.maker

    FROM Purchase , Product

    WHEREProduct.pname=Purchase.product

    AND Purchase .buyer = Joe Blow);

    SELECTDISTINCTCompany.cityFROM Company, Product, Purchase

    WHERE Company.name= Product.maker

    AND Product.pname = Purchase.product

    AND Purchase.buyer = Joe Blow

  • 8/13/2019 Lecture SQL

    29/78

    Subqueries Returning Relations

    SELECT nameFROM Product

    WHERE price > ALL(SELECTprice

    FROM Purchase

    WHERE maker=Gizmo-Works)

    Product ( pname, price, category, maker)Find products that are more expensive than all those produced

    By Gizmo-Works

    You can also use: s > ALL R

    s > ANY R

    EXISTS R

  • 8/13/2019 Lecture SQL

    30/78

    Question for Database Fans

    and their Friends Can we express this query as a single

    SELECT-FROM-WHERE query, without

    subqueries ?

  • 8/13/2019 Lecture SQL

    31/78

    Question for Database Fans

    and their Friends Answer: all SFW queries are

    monotone(figure out what this means).

    A query with ALLis not monotone

  • 8/13/2019 Lecture SQL

    32/78

    Correlated Queries

    SELECTDISTINCTtitle

    FROM Movie AS x

    WHERE year ANY

    (SELECT year

    FROM MovieWHERE title = x.title);

    Movie (title, year, director, length)

    Find movies whose title appears more than once.

    Note (1) scope of variables (2) this can still be expressed as single SFW

    correlation

  • 8/13/2019 Lecture SQL

    33/78

    Complex Correlated Query

    Product ( pname, price, category, maker, year)

    Find products (and their manufacturers) that are more expensivethan all products made by the same manufacturer before 1972

    Very powerful ! Also much harder to optimize.

    SELECT DISTINCT pname, makerFROM Product AS xWHERE price > ALL (SELECT price

    FROM Product AS y

    WHERE x.maker = y.maker AND y.year < 1972);

  • 8/13/2019 Lecture SQL

    34/78

    Aggregation

    SELECT count(*)

    FROM Product

    WHERE year > 1995

    Except count, all aggregations apply to a single attribute

    SELECT avg(price)

    FROM Product

    WHERE maker=Toyota

    SQL supports several aggregation operations:

    sum, count, min, max, avg

  • 8/13/2019 Lecture SQL

    35/78

    COUNT applies to duplicates, unless otherwise stated:

    SELECT Count(category)

    FROM Product

    WHERE year > 1995

    same as Count(*)

    We probably want:

    SELECT Count(DISTINCTcategory)

    FROM Product

    WHERE year > 1995

    Aggregation: Count

  • 8/13/2019 Lecture SQL

    36/78

    Purchase(product, date, price, quantity)

    More Examples

    SELECT Sum(price * quantity)

    FROM Purchase

    SELECT Sum(price * quantity)

    FROM Purchase

    WHERE product = bagel

    What do

    they mean ?

  • 8/13/2019 Lecture SQL

    37/78

    Simple AggregationsPurchase

    Product Date Price Quantity

    Bagel 10/21 1 20

    Banana 10/3 0.5 10

    Banana 10/10 1 10

    Bagel 10/25 1.50 20

    SELECT Sum(price * quantity)

    FROM Purchase

    WHERE product = bagel50 (= 20+30)

  • 8/13/2019 Lecture SQL

    38/78

    Grouping and AggregationPurchase(product, date, price, quantity)

    SELECT product, Sum(price*quantity) AS TotalSales

    FROM Purchase

    WHERE date > 10/1/2005

    GROUP BY product

    Lets see what this means

    Find total sales after 10/1/2005 per product.

  • 8/13/2019 Lecture SQL

    39/78

    Grouping and Aggregation

    1. Compute the FROMand WHEREclauses.

    2. Group by the attributes in the GROUPBY

    3. Compute the SELECTclause: grouped attributes and aggregates.

  • 8/13/2019 Lecture SQL

    40/78

    1&2. FROM-WHERE-GROUPBY

    Product Date Price Quantity

    Bagel 10/21 1 20

    Bagel 10/25 1.50 20

    Banana 10/3 0.5 10

    Banana 10/10 1 10

  • 8/13/2019 Lecture SQL

    41/78

    3. SELECT

    SELECT product, Sum(price*quantity) AS TotalSalesFROM Purchase

    WHERE date > 10/1/2005

    GROUP BY product

    Product Date Price Quantity

    Bagel 10/21 1 20

    Bagel 10/25 1.50 20

    Banana 10/3 0.5 10Banana 10/10 1 10

    Product TotalSales

    Bagel 50

    Banana 15

  • 8/13/2019 Lecture SQL

    42/78

    GROUP BY v.s. Nested Quereis

    SELECT product, Sum(price*quantity) ASTotalSalesFROM Purchase

    WHERE date > 10/1/2005

    GROUP BY product

    SELECT DISTINCT x.product, (SELECTSum(y.price*y.quantity)

    FROM Purchase y

    WHEREx.product = y.product

    AND y.date > 10/1/2005)ASTotalSales

    FROM Purchase x

    WHERE x.date > 10/1/2005

  • 8/13/2019 Lecture SQL

    43/78

    Another Example

    SELECT product,sum(price * quantity) AS SumSales

    max(quantity) AS MaxQuantity

    FROM Purchase

    GROUP BYproduct

    What does

    it mean ?

  • 8/13/2019 Lecture SQL

    44/78

    HAVING Clause

    SELECT product, Sum(price * quantity)

    FROM Purchase

    WHERE date > 10/1/2005

    GROUPBYproduct

    HAVING Sum(quantity) > 30

    Same query, except that we consider only products that had

    at least 100 buyers.

    HAVING clause contains conditions on aggregates.

  • 8/13/2019 Lecture SQL

    45/78

    General form of Grouping and

    AggregationSELECT S

    FROM R1,,Rn

    WHERE C1GROUP BYa1,,akHAVING C2

    S = may contain attributes a1,,akand/or any aggregates but NO OTHERATTRIBUTES

    C1 = is any condition on the attributes in R1,,RnC2 = is any condition on aggregate expressions

    Why ?

  • 8/13/2019 Lecture SQL

    46/78

    General form of Grouping and

    Aggregation

    Evaluation steps:

    1. Evaluate FROM-WHERE, apply condition C12. Group by the attributes a1,,ak

    3. Apply condition C2 to each group (may have aggregates)

    4. Compute aggregates in S and return the result

    SELECT S

    FROM R1,,RnWHERE C1

    GROUP BYa1,,akHAVING C2

  • 8/13/2019 Lecture SQL

    47/78

    Advanced SQLizing

    1. Getting around INTERSECT and EXCEPT

    2. Quantifiers

    3. Aggregation v.s. subqueries

    INTERSECT and EXCEPT: not in SQL Server

  • 8/13/2019 Lecture SQL

    48/78

    1. INTERSECT and EXCEPT:

    (SELECTR.A, R.BFROM R)

    INTERSECT

    (SELECTS.A, S.BFROM S)

    SELECTR.A, R.BFROM RWHERE

    EXISTS(SELECT*FROMSWHERER.A=S.A and R.B=S.B)

    (SELECTR.A, R.B

    FROM R)EXCEPT

    (SELECTS.A, S.BFROM S)

    SELECTR.A, R.B

    FROM RWHERENOT EXISTS(SELECT*

    FROMSWHERER.A=S.A and R.B=S.B)

    If R, S have no

    duplicates, then can

    write without

    subqueries

    (HOW ?)

    INTERSECT and EXCEPT: not in SQL Server

  • 8/13/2019 Lecture SQL

    49/78

    2. Quantifiers

    Product ( pname, price, company)

    Company( cname, city)

    Find all companies that make some products with price < 100

    SELECT DISTINCT Company.cnameFROM Company, ProductWHERE Company.cname = Product.company and Product.price < 100

    Existential: easy !

  • 8/13/2019 Lecture SQL

    50/78

    2. Quantifiers

    Product ( pname, price, company)

    Company( cname, city)

    Find all companies s.t. all of their products have price < 100

    Universal: hard !

    Find all companies that make only products with price < 100

    same as:

  • 8/13/2019 Lecture SQL

    51/78

    2. Quantifiers

    2. Find all companies s.t. all their products have price < 100

    1. Find the other companies: i.e. s.t. some product 100

    SELECT DISTINCT Company.cnameFROM CompanyWHERE Company.cname IN(SELECTProduct.company

    FROMProductWHEREProduc.price >= 100

    SELECT DISTINCT Company.cnameFROM CompanyWHERE Company.cnameNOTIN(SELECTProduct.company

    FROMProductWHEREProduc.price >= 100

  • 8/13/2019 Lecture SQL

    52/78

    3. Group-by v.s. Nested Query

    Find authors who wrote 10 documents:

    Attempt 1: with nested queries

    SELECTDISTINCTAuthor.name

    FROM Author

    WHERE count(SELECTWrote.urlFROMWrote

    WHEREAuthor.login=Wrote.login)

    > 10

    This isSQL by

    a novice

    Author(login,name)Wrote(login,url)

  • 8/13/2019 Lecture SQL

    53/78

    3. Group-by v.s. Nested Query

    Find all authors who wrote at least 10

    documents:

    Attempt 2: SQL style (with GROUP BY)

    SELECT Author.name

    FROM Author, Wrote

    WHERE Author.login=Wrote.loginGROUP BYAuthor.name

    HAVING count(wrote.url) > 10

    This is

    SQL by

    an expert

    No need for DISTINCT: automatically from GROUP BY

  • 8/13/2019 Lecture SQL

    54/78

    3. Group-by v.s. Nested Query

    Find authors with vocabulary 10000 words:

    SELECT Author.name

    FROM Author, Wrote, MentionsWHERE Author.login=Wrote.login AND Wrote.url=Mentions.url

    GROUP BY Author.name

    HAVING count(distinct Mentions.word) > 10000

    Author(login,name)

    Wrote(login,url)

    Mentions(url,word)

  • 8/13/2019 Lecture SQL

    55/78

    Two Examples

    Store(sid, sname)

    Product(pid, pname, price, sid)

    Find all stores that sell onlyproducts with price > 100

    same as:

    Find all stores s.t. all their products have price > 100)

    SELECT Store.name

  • 8/13/2019 Lecture SQL

    56/78

    SELECTStore.name

    FROM Store, Product

    WHERE Store.sid = Product.sid

    GROUP BY Store.sid, Store.name

    HAVING 100 < min(Product.price)

    SELECTStore.name

    FROM Store

    WHERE Store.sidNOTIN

    (SELECTProduct.sid

    FROM Product

    WHERE Product.price

  • 8/13/2019 Lecture SQL

    57/78

    Two Examples

    Store(sid, sname)

    Product(pid, pname, price, sid)

    For each store,

    find its most expensive product

  • 8/13/2019 Lecture SQL

    58/78

    Two Examples

    SELECTStore.sname, max(Product.price)

    FROM Store, Product

    WHERE Store.sid = Product.sid

    GROUP BY Store.sid, Store.sname

    SELECTStore.sname, x.pname

    FROM Store, Product x

    WHERE Store.sid = x.sid andx.price >=

    ALL(SELECTy.price

    FROMProduct y

    WHEREStore.sid = y.sid)

    This is easy but doesnt do what we want:

    Better:

    But mayreturn

    multiple

    product names

    per store

  • 8/13/2019 Lecture SQL

    59/78

    Two Examples

    SELECTStore.sname, max(x.pname)

    FROM Store, Product xWHERE Store.sid = x.sid and

    x.price >=

    ALL(SELECTy.price

    FROMProduct yWHEREStore.sid = y.sid)

    GROUP BYStore.sname

    Finally, choose some pid arbitrarily, if there are manywith highest price:

  • 8/13/2019 Lecture SQL

    60/78

    NULLS in SQL

    Whenever we dont have a value, we can put a NULL

    Can mean many things:

    Value does not exists

    Value exists but is unknown

    Value not applicable

    Etc.

    The schema specifies for each attribute if can be null

    (nullable attribute) or not How does SQL cope with tables that have NULLs ?

  • 8/13/2019 Lecture SQL

    61/78

    Null Values

    If x= NULL then 4*(3-x)/7 is still NULL

    If x= NULL then x=Joe is UNKNOWN

    In SQL there are three boolean values:

    FALSE = 0

    UNKNOWN = 0.5

    TRUE = 1

  • 8/13/2019 Lecture SQL

    62/78

    Null Values

    C1 AND C2 = min(C1, C2)

    C1 OR C2 = max(C1, C2)

    NOT C1 = 1C1

    Rule in SQL: include only tuples that yield TRUE

    SELECT*FROMPersonWHERE (age < 25) AND

    (height > 6 OR weight > 190)

    E.g.

    age=20

    heigth=NULL

    weight=200

  • 8/13/2019 Lecture SQL

    63/78

    Null Values

    Unexpected behavior:

    Some Persons are not included !

    SELECT*FROM Person

    WHERE age < 25 OR age >= 25

  • 8/13/2019 Lecture SQL

    64/78

    Null Values

    Can test for NULL explicitly: x IS NULL

    x IS NOT NULL

    Now it includes all Persons

    SELECT*FROM PersonWHERE age < 25 OR age >= 25 OR age IS NULL

  • 8/13/2019 Lecture SQL

    65/78

    Outerjoins

    Explicit joins in SQL = inner joins:Product(name, category)Purchase(prodName, store)

    SELECTProduct.name, Purchase.store

    FROM Product JOINPurchase ON

    Product.name = Purchase.prodName

    SELECTProduct.name, Purchase.store

    FROM Product, Purchase

    WHERE Product.name = Purchase.prodName

    Same as:

    But Products that never sold will be lost !

  • 8/13/2019 Lecture SQL

    66/78

    Outerjoins

    Left outer joins in SQL:

    Product(name, category)

    Purchase(prodName, store)

    SELECTProduct.name, Purchase.store

    FROM Product LEFT OUTER JOINPurchase ON

    Product.name = Purchase.prodName

  • 8/13/2019 Lecture SQL

    67/78

    Name Category

    Gizmo gadget

    Camera Photo

    OneClick Photo

    ProdName Store

    Gizmo Wiz

    Camera Ritz

    Camera Wiz

    Name Store

    Gizmo Wiz

    Camera Ritz

    Camera Wiz

    OneClick NULL

    Product Purchase

  • 8/13/2019 Lecture SQL

    68/78

    Application

    Compute, for each product, the total number of sales in September

    Product(name, category)

    Purchase(prodName, month, store)

    SELECTProduct.name, count(*)

    FROM Product, Purchase

    WHERE Product.name = Purchase.prodName

    and Purchase.month = September

    GROUP BY Product.name

    Whats wrong ?

  • 8/13/2019 Lecture SQL

    69/78

    Application

    Compute, for each product, the total number of sales in September

    Product(name, category)

    Purchase(prodName, month, store)

    SELECTProduct.name, count(*)

    FROM Product LEFT OUTER JOINPurchase ON

    Product.name = Purchase.prodName

    and Purchase.month = SeptemberGROUP BY Product.name

    Now we also get the products who sold in 0 quantity

  • 8/13/2019 Lecture SQL

    70/78

    Outer Joins

    Left outer join: Include the left tuple even if theres no match

    Right outer join: Include the right tuple even if theres no match

    Full outer join:

    Include the both left and right tuples even if theres nomatch

  • 8/13/2019 Lecture SQL

    71/78

    Modifying the Database

    Three kinds of modifications

    Insertions

    Deletions

    Updates

    Sometimes they are all called updates

  • 8/13/2019 Lecture SQL

    72/78

    Insertions

    General form:

    Missing attributeNULL.

    May drop attribute names if give them in order.

    INSERT INTO R(A1,., An) VALUES (v1,., vn)

    INSERT INTO Purchase(buyer, seller, product, store)

    VALUES (Joe, Fred, wakeup-clock-espresso-machine,

    The Sharper Image)

    Example: Insert a new purchase to the database:

  • 8/13/2019 Lecture SQL

    73/78

    Insertions

    INSERT INTO PRODUCT(name)

    SELECT DISTINCT Purchase.product

    FROM Purchase

    WHERE Purchase.date > 10/26/01

    The query replaces the VALUES keyword.

    Here we insert manytuples into PRODUCT

  • 8/13/2019 Lecture SQL

    74/78

    Insertion: an Example

    prodNameis foreign key in Product.name

    Suppose database got corrupted and we need to fix it:

    name listPrice category

    gizmo 100 gadgets

    prodName buyerName price

    camera John 200

    gizmo Smith 80

    camera Smith 225

    Task: insert in ProductallprodNamesfrom Purchase

    Product

    Product(name, listPrice, category)

    Purchase(prodName, buyerName, price)

    Purchase

  • 8/13/2019 Lecture SQL

    75/78

    Insertion: an Example

    INSERT INTO Product(name)

    SELECT DISTINCT prodName

    FROM Purchase

    WHERE prodName NOT IN(SELECT name FROM Product)

    name listPrice category

    gizmo 100 Gadgets

    camera - -

  • 8/13/2019 Lecture SQL

    76/78

    Insertion: an Example

    INSERT INTO Product(name, listPrice)

    SELECT DISTINCT prodName, price

    FROM PurchaseWHERE prodName NOT IN(SELECT name FROM Product)

    name listPrice category

    gizmo 100 Gadgets

    camera 200 -

    camera ?? 225 ?? - Depends on the implementation

  • 8/13/2019 Lecture SQL

    77/78

    Deletions

    DELETE FROM PURCHASE

    WHERE seller = Joe AND

    product = Brooklyn Bridge

    Factoid about SQL: there is no way to delete only a singleoccurrence of a tuple that appears twice

    in a relation.

    Example:

  • 8/13/2019 Lecture SQL

    78/78

    Updates

    UPDATE PRODUCT

    SET price = price/2

    WHERE Product.name IN(SELECTproduct

    FROM Purchase

    WHERE Date =Oct, 25, 1999);

    Example: