在MySQL中创建触发器

时间:2017-05-25 21:14:39

标签: mysql triggers

我有两张桌子:

表:文章

userid int(11) PK 
articleid int(11) PK 
posting_datetime datetime 
popularity varchar(20) 
quality varchar(20) 
title varchar(100) 
content varchar(1000)

表:报告

userid int(11) PK 
article_userid int(11) PK 
article_articleid int(11) PK 
reason varchar(60)

并且

INSERT INTO Article
VALUES
    (1,1,'2017-01-21 11:13:34','Regular','Clean','Inauguration of President Trump','The inauguration of Donald Trump as the 45th President of the United States marked commencement of the four-year term of Donald Trump as President and Mike Pence as Vice President. The public ceremony held on Friday, January 20, 2017 on the West Front of the United States Capitol Building in Washington, D.C. Along with being the oldest and wealthiest person inaugurated as president, he is the first without prior military or governmental service experience.'),
    (1,2,'2017-04-02 10:23:33','Regular','Clean','White House Hosts Easter','The White House will host Easter this year. Some 21,000 people are invited to attend the annual White House Easter Egg Roll on Monday, one of the year’s largest events held at the mansion.'),
    (3,1,'2014-12-26 17:43:37','Regular','Clean','iPhone 6 is released','The iPhone 6 and iPhone 6 Plus are smartphones designed and marketed by Apple Inc. The devices are part of the iPhone series and were announced on September 9, 2014, and released on September 19, 2014.'),
    (3,2,'2017-04-20 15:41:34','Regular','Clean','iPhone 8 to be announced in September','The iPhone 8 and iPhone 8 Plus are smartphones designed and marketed by Apple Inc. The devices are part of the iPhone series and will be announced on September 10, 2017, and released on September 22, 2017.'),
    (4,1,'2016-10-24 02:01:12','Regular','Suspicious','Cleveland Cavs signed Kevin Durant','Kevin Durant announced in a post on The Players Tribune that he will sign with the Cleveland Cavaliers. Kevin Durant signed a 2 year / $54,274,505 contract with the Cleveland Cavaliers, including $54,274,505 guaranteed, and an annual average salary of $27,137,253. In 2016-17, Durant will earn a base salary of $26,540,100, while carrying a cap hit of $26,540,100 and a dead cap value of $54,274,505.'),
    (6,1,'2015-07-09 01:00:00','Regular','Junk','2009 Warmest Year on Record','Earth’s 2009 surface temperatures were the warmest since modern recordkeeping began in 1880, according to independent analyses by NASA and the National Oceanic and Atmospheric Administration (NOAA).'),
    (9,1,'2013-12-19 12:54:21','Regular','Clean','Governance and Cybersecurity','Good governance should translate into your organization having high confidence that these important principles hold true.');

INSERT INTO Reports
VALUES
    (8,3,2,'Kinda incorrect'),
    (10,3,2,'Apple did not announce anything!'),
    (22,3,2,'Check dates'),
    (17,3,2,'Not true'),
    (15,3,2,'Kinda incorrect'),
    (22,4,1,'Incorrect Team'),
    (21,4,1,'Incorrect Team'),
    (20,4,1,'Double check the team'),
    (19,4,1,'Double check the team'),
    (18,4,1,'Story in not true!'),
    (17,4,1,'Not true'),
    (16,4,1,'Durant went to the Warriors not the Cavs.'),
    (15,4,1,'Is this real!'),
    (12,4,1,'Check the facts'),
    (22,6,1,'He went to Cleveland'),
    (21,6,1,'The hottest year is 2016'),
    (20,6,1,'The hottest year is 2016'),
    (19,6,1, 'Im not quite sure its true'),
    (18,6,1,'Story in not true!'),
    (17,6,1,'Wrong information'),
    (16,6,1,'The hottest year is 2016'),
    (15,6,1,'Fake News!'),
    (12,6,1,'Wrong!'),
    (10,6,1,'Incorrect year'),
    (8,6,1,'Incorrect year');

我必须"创建触发器update_quality"如果报道的文章超过5次,我必须将文章中的质量值更改为可疑,如果报告超过10次,我会将其标记为垃圾。

如何在mySQL中创建上述触发器。无法正确使用语法。我的尝试:

DELIMITER // 
CREATE TRIGGER update_quality /* Q7.a */ AFTER INSERT ON article 
    FOR each row BEGIN 
        SELECT article_articleid, count(article_articleid) 
        from reports 
        group by article_articleid; 
        IF count(article_articleid)>5 
        then 
            update article 
            set quality=suspicous 
            WHERE article.articleid=report.article_articleid 
        END;
// 
DELIMITER ; 

0 个答案:

没有答案