I get the syntax error at or near "ON" with the code below. What is the correct way to write this? I'm trying to create a trigger for the game_info table that will update the game_count table when data is added to game_info.
CREATE TRIGGER my_trigger
ON game_info
AFTER INSERT
AS
BEGIN
INSERT INTO game_count(title,count)
SELECT title, count(*)
FROM public.game_info
GROUP BY title
ORDER BY count DESC
END
creating the game_info table
CREATE TABLE game_info (
rental_id INT NOT NULL,
film_id SMALLINT NOT NULL,
title CHAR(255) NOT NULL,
description TEXT NOT NULL,
length SMALLINT NOT NULL
);
creating the game_count table
CREATE TABLE game_count (
title CHAR(255) NOT NULL,
count BIGINT NOT NULL
)
inserting data into game_info
INSERT INTO game_info (rental_id, film_id, title, description, length)
SELECT
r.rental_id,
i.film_id,
f.title,
f.description,
f.length
FROM public.inventory as i
INNER JOIN public.rental as r ON r.inventory_id=i.inventory_id
INNER JOIN public.film as f ON f.film_id=i.film_id
inserting data into game_count
INSERT INTO game_count(title, count)
SELECT title, count(*)
FROM public.game_info
GROUP BY title
ORDER BY count DESC;
after insertaboveon game_infoand give it a shot againasand try again. Also, follow these tutorials: mysqltutorial.org/create-the-first-trigger-in-mysql.aspx and w3resource.com/mysql/mysql-triggers.php#MTAI