我有三张表,分别是Review、Paper和PCMember,代码如下:
CREATE TABLE REVIEW(
due_date DATE NOT NULL,
review_date DATE NOT NULL,
recommendation VARCHAR(50) NOT NULL,
comment VARCHAR(50) NOT NULL,
pcmem_id NUMBER(10) NOT NULL,
paper_id NUMBER(10) NOT NULL,
CONSTRAINT review_pk PRIMARY KEY (pcmem_id,paper_id),
CONSTRAINT review_fk FOREIGN KEY(paper_id)
REFERENCES PAPER(paper_id),
CONSTRAINT review_fk1 FOREIGN KEY(pcmem_id)
REFERENCES PCMEMBER(pcmem_id));
CREATE TABLE PCMEMBER(
pcmem_id NUMBER(10) NOT NULL PRIMARY KEY,
pc_fname VARCHAR(20) NOT NULL,
pc_sname VARCHAR(20) NOT NULL,
pc_title VARCHAR (20) NOT NULL,
pc_position VARCHAR(20) NOT NULL,
affiliation VARCHAR(20) NOT NULL,
pc_email VARCHAR(20) NOT NULL,
track_id NUMBER(6) NOT NULL,
CONSTRAINT pcmember_fk FOREIGN KEY(track_id)
REFERENCES TRACK(track_id));
CREATE TABLE PAPER(
paper_id NUMBER(10) PRIMARY KEY NOT NULL,
paper_title VARCHAR(20) NOT NULL,
abstract VARCHAR(50) NOT NULL,
paper_type VARCHAR(20) NOT NULL,
submission_date DATE NOT NULL,
track_id NUMBER(6) NOT NULL,
CONSTRAINT paper_fk FOREIGN KEY(track_id)
REFERENCES TRACK(track_id),
CONSTRAINT chk_type CHECK(paper_type IN ('full paper','Research-in-Progress','posters')),
);
我试图添加一个条件,即“每一篇论文都将由3名PC成员进行审阅”。不确定我应该使用什么检查约束?我只需要这个来创建表。谢谢
最佳答案
创建:
a标志PAPER.reviewed DEFAULT 0
插入/更新触发器以防止客户端更改标志
当相关论文只有(没有)三个审阅者时,触发REVIEW
自动设置(取消设置)PAPER.reviewed
标志(更新时同时更新:old.paper_id和new.paper_id)
触发REVIEW
以防止每篇论文超过三个审阅者。
更新
创建触发器的示例(插入时每篇论文阻止超过3个审阅者)
DELIMITER //
CREATE TRIGGER `chck_pcmem` AFTER INSERT ON `review` FOR EACH ROW BEGIN
IF ((SELECT COUNT(1) FROM review WHERE paper_id = NEW.paper_id) > 3) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Only 3 PC members per paper';
END IF;
END//
DELIMITER ;
关于mysql - 需要在SQL中将属性限制为3个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36262177/