SQL Data Manipulation Syntax
Insert
If we name the attributes we are providing values for, the system will use NULL
or a default for the rest
Create a table before insert
CREATE TABLE Invite (
name TEXT,
campus TEXT DEFAULT 'StG',
email TEST,
age INT
);
INSERT INTO Invite(name, email)(
SELECT first, email
FROM Student
WHERE cgpa > 3.4
);
the query have values for name and email, campus gets the default value, age gets NULL
Delete
-- delete tuples satisfying WHERE clause
DELETE FROM <<relation>>
WHERE <<condition>>;
-- delete all tuples
DELETE FROM <<relation>>
-- alternatively can use DROP, which destroy the table
DROP TABLE <<relation>>
Example
DELETE FROM Course
WHERE NOT EXISTS (
SELECT *
FROM Took JOIN Offering ON Took.oid = Offering.oid
WHERE grade > 50 AND
Offering.dept = Course.dept AND
Offering.num = Course.num
);
Update
Updating one tuple
Update several tupleManipulating Views
Generally, it is impossible to modify a virtual view.
We don't often (most systems prohibit) translate updates on views into equivalent updates on base tables.
Materialized Views
Problem: each time a base table changes, the materialized view may change. Since cannot afford to recompute the view with each change
Solution: periodic reconstruction of the materialized view, which is otherwise "out of date".