在MySQL中使用触发器作为断言

时间:2018-10-29 21:58:23

标签: mysql sql database-trigger

我在MySql中有一个数据库和一些表。我正在尝试使用CREATE TRIGGER在MySQL中实现Oracle断言。我不确定是否使用了正确的语法。

到目前为止,这是我所拥有的,但是我不确定为什么会出错。

CREATE TRIGGER tg_review_before_insert
BEFORE INSERT ON review
FOR EACH ROW
SET NEW.paper = IF((SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
    FROM review R
    WHERE R.paperid = P.paperid)
);

这是我的桌子:

CREATE TABLE paper(
    paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
    title VARCHAR(50) NOT NULL,
    abstract VARCHAR(250),
    pdf VARCHAR(100),
    PRIMARY KEY(paperid)
);

CREATE TABLE author(
    email VARCHAR(100) NOT NULL,
    name VARCHAR(50),
    affiliation VARCHAR(100),
    PRIMARY KEY(email)
);

CREATE TABLE writePaper(
    paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
    email VARCHAR(100),
    paper_order INT,
    PRIMARY KEY(paperid, email),
    FOREIGN KEY(paperid) REFERENCES paper(paperid),
    FOREIGN KEY(email) REFERENCES author(email)
);

CREATE TABLE pcmember(
    email VARCHAR(100) NOT NULL,
    name VARCHAR(20),
    PRIMARY KEY(email)
);

CREATE TABLE review(
    reportid INT UNSIGNED,
    sdate DATE,
    comment VARCHAR(250),
    recommendation CHAR(1),
    paperid INT UNSIGNED,
    email VARCHAR(100),
    PRIMARY KEY(paperid, email),
    FOREIGN KEY(paperid) REFERENCES paper(paperid),
    FOREIGN KEY(email) REFERENCES pcmember(email)
);

以下是我要从Oracle SQL代码到MySQL SQL代码实现的断言:

CREATE ASSERTION assert
CHECK NOT EXISTS(SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
    FROM review R
    WHERE R.paperid = P.paperid)
);

CREATE ASSERTION atmostfivepapers
CHECK NOT EXISTS(SELECT * FROM pcmember P WHERE 5 <
    ( SELECT *
    FROM review R
    WHERE R.email = P.email
    )
);

0 个答案:

没有答案