Jed Rembold
Monday, November 24, 2025
Does the below query run?
INSERT INTO recent_coms
VALUES
('The Batman',
'action',
'2022-03-04'
);
CREATE TABLE movies (
"name" text,
"genre" text,
"release" date
);
CREATE VIEW comedies AS
SELECT *
FROM movies
WHERE genre = 'comedy';
CREATE VIEW recent_coms AS
SELECT *
FROM comedies
WHERE release > '2015-01-01'
WITH LOCAL CHECK OPTION;
newest that will
only show the “new” boxes from
best_boxesnewred that will
only show new and red boxes referencing only
newestCHECK OPTION setting on one or
both views so that you could add
('Jane', 2, 7, 9, 'red', 'new') to
newred but not
('Bart', 6, 10, 12, 'red', 'used') to
newredbox_volume to
compute the volume of a cubic object, and use that function to populate
a new volume column in
best_boxesscale_boxes which
updates the best_boxes table to scale the
value in the named width,
height, or depth
columns by the provided argument. Run it to ensure it is working
(SELECT scale_boxes(2);)NEW and
OLD will refer to the newly changed or
previous table values (respectively)TRIGGER typeUPDATE or
INSERT triggers should return
NEWDELETE triggers should return
OLDCREATE TRIGGER |||trigger name|||
AFTER UPDATE
ON |||table_name|||
FOR EACH ROW
EXECUTE FUNCTION |||func_name|||();
INSERT,
UPDATE, or DELETE
events that affect a particular tableINSTEAD OF
the event occuring, but we won’t cover that hereIn some cases, triggering off of any update or insert is too broad
Can localize to just a single (or few) columns by using
OF colname1:
AFTER UPDATE OF |||column_name₁|||, |||column_name₂|||Can localize to only triggering off certain rows by inserting a
WHEN condition before the
EXECUTE line
NEW and
OLD within this conditionWHEN OLD.|||name||| != NEW.|||name|||
EXECUTE FUNCTION |||myfunc|||();FOR EACH ROW will cause the trigger
function to run once for every row that was a part of the
triggered event
UPDATE that affected 10 rows? The
trigger function will be run 10 times, once on each rowNEW
and OLD variables in your trigger
functionFOR EACH STATEMENT will cause the
trigger function to run once when the triggering event occurs
UPDATE that affects 10 rows? The
trigger function just runs once.OLD or
NEWYou are very worried about the data within
best_boxes from our earlier activity, such
that you want to keep a log of any name changes that might occur. The
table name_changes was already created for
you from the earlier activity.
name from
best_boxes, will add a row to the
name_changes table, where the operation would
be 'update' in this case. The
changed_at column should be set to the
current timestamp. Test it by changing ‘Bob’ to ‘Bobby’.name_changes whenever a new row is added
to best_boxes. In this case, the operation
would be 'insert' and the
og_name would be
NULL.