使用if语句构建MySQL触发器

时间:2017-04-04 00:50:36

标签: mysql triggers

我正在使用此处https://dev.mysql.com/doc/index-other.html

中的示例MySQL数据库'world'

以下是表格的说明:

mysql> desc city; desc country; desc countrylanguage;
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int(11)  | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Field          | Type                                                                                  | Null | Key | Default | Extra |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Code           | char(3)                                                                               | NO   | PRI |         |       |
| Name           | char(52)                                                                              | NO   |     |         |       |
| Continent      | enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') | NO   |     | Asia    |       |
| Region         | char(26)                                                                              | NO   |     |         |       |
| SurfaceArea    | float(10,2)                                                                           | NO   |     | 0.00    |       |
| IndepYear      | smallint(6)                                                                           | YES  |     | NULL    |       |
| Population     | int(11)                                                                               | NO   |     | 0       |       |
| LifeExpectancy | float(3,1)                                                                            | YES  |     | NULL    |       |
| GNP            | float(10,2)                                                                           | YES  |     | NULL    |       |
| GNPOld         | float(10,2)                                                                           | YES  |     | NULL    |       |
| LocalName      | char(45)                                                                              | NO   |     |         |       |
| GovernmentForm | char(45)                                                                              | NO   |     |         |       |
| HeadOfState    | char(60)                                                                              | YES  |     | NULL    |       |
| Capital        | int(11)                                                                               | YES  |     | NULL    |       |
| Code2          | char(2)                                                                               | NO   |     |         |       |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
15 rows in set (0.00 sec)

+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| CountryCode | char(3)       | NO   | PRI |         |       |
| Language    | char(30)      | NO   | PRI |         |       |
| IsOfficial  | enum('T','F') | NO   |     | F       |       |
| Percentage  | float(4,1)    | NO   |     | 0.0     |       |
+-------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

我正在尝试建立一个触发器,它只会在满足以下条件时从城市表中删除城市:

  • 语言为官方语言(countrylanguage.IsOfficial ='T')
  • 官方语言不是英语(countrylanguage.IsOfficial<>英语)
  • 超过20%的人口使用(countrylanguage.Percentage> 20)

这是我现在的代码 -

DELIMITER $$
    CREATE TRIGGER city_45_SDD
    BEFORE DELETE ON city
    FOR EACH ROW
    BEGIN
        DECLARE Lang varchar(25);
        DECLARE Percent Float(4);
        DECLARE Official varchar(1);
    SELECT Language INTO Lang FROM countrylanguage;
    SELECT Percentage INTO Percent FROM countrylanguage;
    SELECT IsOfficial INTO Official FROM countrylanguage;

    IF Lang <> 'English' AND Percent > 45 AND Official = 'T'
        DELETE FROM city (ID, Name, CountryCode, District, Population);
    END IF;
END$$
DELIMITER ;

不确定我做错了什么,所以如果有人能向我解释我做错了什么,那就太棒了。

抱歉,我忘了提及这些问题。

  • if语句是否格式正确?我用上面写的格式和IF(...)THEN语句看过它。
  • 如何让DELETE FROM使用查询中的值?
  • 此外,是否可以比较查询和表格中的值?我如何区分,例如查询的city.Name和表的city.Name?

0 个答案:

没有答案