I have a HW assignment using the " Media.db" commonly used for SQL exercises.
First we added a new column to the " albums" table called "play time".
Then I added a constraint to make sure the values added to the table are only positive values.
Then I made an update statement that updates the new "play_time' column to contain the total play time for an album based on the tracks it contains.
But I'm having trouble with this last part:
Attach an "after insert" trigger on the tables "tracks" that recalculates the "play_time" value to ensure that it is always correct. So, for example, if a new track is added to an album, then the "play_time" column is updated.
Using RSQLite in R, this is what I have: Here is what I Have:
# add the trigger
step_4 <- paste0(" CREATE TRIGGER IF NOT EXISTS
AFTER INSERT TrackId ON tracks
FOR EACH ROW
WHEN
BEGIN
(UPDATE albums
SET play_time = ( SELECT
COUNT(play_time)
FROM
tracks
INNER JOIN albums ON albums.albumid = tracks.albumid
GROUP BY
tracks.albumid))
"
)
rs4 <- dbExecute(dbcon,step_4)
This is what I keep getting:
Error: near "TrackId": syntax error
Please help.
I tried changing the syntax, using Inner_join differently,
and this is what I did make an update statement that updates the new "play_time' column to contain the total play time for an album based on the tracks it contains.
# update the "play_time" column
step_3 <- paste0(" UPDATE albums
SET play_time = ( SELECT
tracks.albumid
title
COUNT(play_time)
FROM
tracks
INNER JOIN albums ON albums.albumid = tracks.albumid
GROUP BY
tracks.albumid)
WHERE
play_time
")
rs3 <- dbExecute(dbcon,step_3)