A modification command does not return a result (as a query does), but changes the database in some way.
Three kinds of modifications:
To insert a single tuple:
INSERT INTO <relation>
VALUES ( <list of values> );Example: add to Likes(drinker, beer) the fact that Sally likes Bud.
INSERT INTO Likes VALUES(’Sally’ , ’Bud’);
-- Another way to add the fact that Sally
-- likes Bud to Likes(drinker, beer):
INSERT INTO Likes(beer, drinker)
VALUES(’Bud’,‘Sally’);Example:
CREATE TABLE Drinkers (
name CHAR(30) PRIMARY KEY,
addr CHAR(50) DEFAULT ’123 Sesame St.’,
phone CHAR(16)
);We may insert the entire result of a query into a relation, using the form:
INSERT INTO <relation>
( <subquery> );INSERT INTO PotBuddies
(SELECT d2.drinker
FROM Frequents d1, Frequents d2
WHERE d1.drinker = ’Sally’ AND
d2.drinker <> ’Sally’ AND
d1.bar = d2.bar
);
To delete tuples satisfying a condition from some relation:
DELETE FROM <relation>
WHERE <condition>;DELETE FROM Likes
WHERE drinker = ’Sally’ AND
beer = ’Bud’;-- Delete all Tuples
DELETE FROM Likes;DELETE FROM Beers b
WHERE EXISTS (
SELECT name FROM Beers
WHERE manf = b.manf AND
name <> b.name);Now, when b is the tuple for Bud Lite, do we delete that tuple too? Answer: we do delete Bud Lite as well.
The reason is that deletion proceeds in two stages:
To change certain attributes in certain tuples of a relation:
UPDATE <relation>
SET <list of attribute assignments>
WHERE <condition on tuples>;UPDATE Drinkers
SET phone = ‘555-1212’
WHERE name = ‘Fred’;UPDATE Sells
SET price = 4.00
WHERE price > 4.00;