创建一个功能和一个触发器以检查和显示电影

时间:2019-05-28 14:09:04

标签: postgresql database-trigger

显示与使用功能和触发器购买的电影类型相同的电影名称。

我知道我必须使用NEW来获取电影ID。

例如:

INSERT INTO purchase (who_id, what_id) VALUES (1,2);

函数中的某处:

SELECT titles 
FROM movies 
WHERE id_genre = ( SELECT id_genre 
                   FROM movies 
                   WHERE movies.id_movie = NEW.what_id);
CREATE TABLE client (
name varchar(20) not null,
surname varchar(40) not null,
id_client serial primary key
);

CREATE TABLE genre (
g_name varchar(30) not null,
id_genre serial PRIMARY KEY
);

CREATE TABLE movies(
title varchar(40) not null,
id_movie serial primary key,
price numeric(4,2),
genre integer REFERENCES genre ON DELETE SET NULL ON
UPDATE CASCADE);

CREATE TABLE purchase(
who_id int not null REFERENCES client ON DELETE
RESTRICT ON UPDATE RESTRICT,
what_id int not null REFERENCES movie ON DELETE
RESTRICT ON UPDATE CASCADE,
);

CREATE TRIGGER T_check_display AFTER INSERT ON purchase 
FOR EACH ROW EXECUTE PROCEDURE F_check_display;
CREATE FUNCTION F_check_display() RETURNS TRIGGER
LANGUAGE plpgsql
AS
'
    BEGIN
        RAISE NOTICE '' Added new purchase'';
    RETURN NEW;
    ---- Check and display all the movie titles which have the same genre as 
           the one inserted by a trigger -----
END;    ';

0 个答案:

没有答案