SQL: Updating the Data

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [0/15]
❖ Data Modification in SQL

We have seen statements to modify table meta-data (in DB catalog):

SQL also provides statements for modifying data in tables: Constraint checking is applied automatically on any change.

Operation fails (no change to DB) if any constraint check fails

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [1/15]
❖ Insertion

Add new tuples via the INSERT operation:

INSERT INTO RelationName
VALUES (val1, val2, val3, ...)

INSERT INTO RelationName(Attr1, Attr2, ...)
VALUES (valForAttr1, valForAttr2, ...)

INSERT INTO RelationName
VALUES Tuple1, Tuple2, Tuple3, ...

The first two add a single new tuple into RelationName.

The last form adds multiple tuples into RelationName.

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [2/15]
❖ Insertion (cont)


INSERT INTO R VALUES (v1,v2,...)

INSERT INTO R(A1,A2,...) VALUES (v1,v2,...)
COMP3311 20T3 ♢ SQL: Updating the Data ♢ [3/15]
❖ Insertion (cont)

Example: Add the fact that Justin likes 'Old'.

INSERT INTO Likes VALUES ('Justin','Old');
-- or --
INSERT INTO Likes(drinker,beer) VALUES('Justin','Old');
-- or --
INSERT INTO Likes(beer,drinker) VALUES('Old','Justin');


Example: Add a new beer with unknown style.

INSERT INTO Beers(name,brewer)
       VALUES('Mysterio','Hop Nation');
-- which inserts the tuple ...
('Mysterio', 'Hop Nation', null)

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [4/15]
❖ Insertion (cont)

Example: insertion with default values

ALTER TABLE Likes
   ALTER COLUMN beer SET DEFAULT 'New';
ALTER TABLE Likes
   ALTER COLUMN drinker SET DEFAULT 'Joe';

INSERT INTO Likes(drinker) VALUES('Fred');
INSERT INTO Likes(beer) VALUES('Sparkling Ale');

-- inserts the two new tuples ...
('Fred', 'New')
('Joe', 'Sparkling Ale')

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [5/15]
❖ Insertion (cont)

Example: insertion with insufficient values.

E.g. specify that drinkers' phone numbers cannot be NULL.

ALTER TABLE Drinkers
   ALTER COLUMN phone SET NOT NULL;


Then try to insert a drinker whose phone number we don't know:

INSERT INTO Drinkers(name,addr) VALUES ('Zoe','Manly');

ERROR:  null value in column "phone" violates
                              not-null constraint
DETAIL:  Failing row contains (Zoe, Manly, null).

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [6/15]
❖ Bulk Insertion of Data

Tuples may be inserted individually:

insert into Stuff(x,y,s) values (2,4,'green');
insert into Stuff(x,y,s) values (4,8,null);
insert into Stuff(x,y,s) values (8,null,'red');
...

but this is tedious if 1000's of tuples are involved.

It is also inefficient

So, most DBMSs provide non-SQL methods for bulk insertion
COMP3311 20T3 ♢ SQL: Updating the Data ♢ [7/15]
❖ Bulk Insertion of Data (cont)

Bulk insertion methods typically ...

Example: PostgreSQL's copy statement:

COPY Stuff(x,y,s) FROM stdin;
2       4       green
4       8       \N
8       \N      red
\.

Can also copy from a named file   (but must be readable by PostrgeSQL server)

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [8/15]
❖ Deletion

Removing tuples is accomplished via DELETE statement:

DELETE FROM Relation
WHERE  Condition

Removes all tuples from Relation  that satisfy Condition.

Example: Justin no longer likes Sparkling Ale.

DELETE FROM Likes
WHERE drinker = 'Justin'
      AND beer = 'Sparkling Ale';

Special case: Make relation R  empty.

DELETE FROM R;    or    DELETE FROM R WHERE true;

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [9/15]
❖ Deletion (cont)

Example: remove all expensive beers from sale.

DELETE FROM Sells WHERE price >= 5.00;


Example: remove all beers with unknown style

DELETE FROM Beers WHERE style IS NULL;

This fails* if such Beers are referenced from other tables

E.g. such Beers are liked by someone or sold in some bar

* no beers are removed, even if some are not referenced

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [10/15]
❖ Semantics of Deletion

Method A for   DELETE FROM R  WHERE Cond :

FOR EACH tuple T in R DO
    IF T satisfies Cond THEN
        remove T from relation R
    END
END

Method B for   DELETE FROM R  WHERE Cond :

FOR EACH tuple T in R DO
    IF T satisfies Cond THEN
        make a note of this T
    END
END
FOR EACH noted tuple T DO
    remove T from relation R
END

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [11/15]
❖ Semantics of Deletion (cont)

Does it matter which method the DBMS uses?

For most cases, the same tuples would be deleted

But if Cond  involes a query on the table R

E.g.

DELETE FROM Beers
WHERE (SELECT count(*) FROM Beers) > 10;

Method A deletes beers until there are only 10 left
Method B deletes all beers if there were more than 10 to start with

COMP3311 20T3 ♢ SQL: Updating the Data ♢ [12/15]
❖ Updates

The UPDATE statement allows you to ...

  • modify values of specified attributes in specified tuples of a relation

    UPDATE R
    SET    List of assignments
    WHERE  Condition
    

    Each tuple in relation R  that satisfies Condition is affected

    Assignments may:

  • COMP3311 20T3 ♢ SQL: Updating the Data ♢ [13/15]
    ❖ Updates (cont)

    Example: Adam changes his phone number.

    UPDATE Drinkers
    SET    phone = '9385-2222'
    WHERE  name = 'Adam';
    

    Example: John moves to Coogee.

    UPDATE Drinkers
    SET    addr = 'Coogee',
           phone = '9665-4321'
    WHERE  name = 'John';
    

    COMP3311 20T3 ♢ SQL: Updating the Data ♢ [14/15]
    ❖ Updates (cont)

    Examples that modify many tuples ...

    Example: Make $6 the maximum price for beer.

    UPDATE Sells
    SET    price = 6.00
    WHERE  price > 6.00;
    


    Example: Increase beer prices by 10%.

    UPDATE Sells
    SET    price = price * 1.10;
    

    Updates all tuples (as if WHERE true)

    COMP3311 20T3 ♢ SQL: Updating the Data ♢ [15/15]


    Produced: 28 Sep 2020