4

在我的数据库中,我有一个Employee通过递归关联,员工可以成为其他员工的老板。

表说明:

mysql> DESC Employee;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| SSN         | varchar(64)  | NO   | PRI | NULL    |       |
| name        | varchar(64)  | YES  |     | NULL    |       |
| designation | varchar(128) | NO   |     | NULL    |       |
| MSSN        | varchar(64)  | NO   | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

员工表的当前状态是:

mysql> SELECT * FROM Employee;
    +-----+------+-------------+------+
    | SSN | name | designation | MSSN |
    +-----+------+-------------+------+
    | 1   | A    | OWNER       | 1    |
    | 2   | B    | BOSS        | 1    |  
    | 3   | C    | WORKER      | 2    |  
    | 4   | D    | BOSS        | 2    |  
    | 5   | E    | WORKER      | 4    |   
    | 6   | F    | WORKER      | 1    |  
    | 7   | G    | WORKER      | 4    |  
    +-----+------+-------------+------+
    7 rows in set (0.00 sec)

以下是表中各行之间的层次关系:

     A
    / \
   B   F
  / \
 c   D
    / \
   G   E

我想对 INSERT 施加以下约束

  • 员工不能对自己进行 BOSS。这仅适用于 OWNER。因此查询之类的。
    INSERT INTO Employee VALUES ("8", "H", "BOSS", "8"); 应该拒绝。
  • OWNER可以插入 新的新的。

因为我正在使用 5.5 之前的 MYSQL 版本(不支持信号)。
所以我使用了一个my_singal()一个存储过程。

写成这样:

CREATE PROCEDURE `my_signal`(in_errortext VARCHAR(255))
BEGIN
    SET @sql=CONCAT('UPDATE `', in_errortext, '` SET x=1');
    PREPARE my_signal_stmt FROM @sql;
    EXECUTE my_signal_stmt;
    DEALLOCATE PREPARE my_signal_stmt;
END// 

应用约束,我written a Trigger知道了check constraints are not yet implemented in MySQL

DELIMITER $$
CREATE
  TRIGGER `employee_before_insert` BEFORE INSERT
    ON `Employee`
    FOR EACH ROW BEGIN
      CASE
       WHEN NEW.designation = 'OWNER'  THEN
          CALL my_signal('Error: can not insert new OWNER !');

       WHEN NEW.SSN = NEW.MSSN THEN
          CALL my_signal('Error: Row can not reference itself!');
      END CASE; 
  END$$   
DELIMITER ;  

它已成功编译并加载到数据库中。但是当我尝试插入时:

mysql> INSERT INTO Employee VALUES ("12", "K", "BOSS",   "12");
ERROR 1336 (0A000): Dynamic SQL is not allowed in stored function or trigger

我在 这里这里学习

  • SQL 准备语句(PREPARE、EXECUTE、DEALLOCATE PREPARE)可用于存储过程,但不能用于存储函数或触发器。因此,存储函数和触发器不能使用动态 SQL(您将语句构造为字符串然后执行它们)。

经过一番努力,我可以编写另一个触发器,如下所示。按照我的要求工作正常。

mysql> CREATE
    -> TRIGGER `employee_before_insert` BEFORE INSERT
    ->     ON `Employee`
    ->     FOR EACH ROW BEGIN
    ->      IF UCASE(NEW.designation) = 'OWNER'  THEN  /*UCASE*/
    ->        UPDATE `Error: can not insert new OWNER !` set x=1; 
    ->      END IF;
    ->      IF (NEW.SSN = NEW.MSSN) THEN
    ->        UPDATE `Error: Row can not reference itself!` set x=1;
    ->      END IF;
    -> END$$
Query OK, 0 rows affected (0.08 sec)

mysql> DELIMITER ;
mysql> INSERT INTO Employee VALUES ("12", "K", 'owner',   "11");
ERROR 1146 (42S02): Table 'dumy.Error: can not insert new OWNER !'
  doesn't exist
mysql> INSERT INTO Employee VALUES ("12", "K", 'Ajay',   "12");
ERROR 1146 (42S02): Table 'dumy.Error: Row can not reference itself!' 
  doesn't exist

因为我已经my_signal() 在许多过程中使用,我需要编写许多新的存储函数和触发器,我需要my_signal()再次使用函数。

It would also good for easy manipulation if sometime MYSQL version upgraded to 5.5(+).

有人可以建议我以其他方式编写 my_signal() 来打印自定义错误消息吗?

我尝试如下:

分隔符 $$
创建过程my_signal(in_errortext VARCHAR(255))
声明 sql varchar(512);
开始
SET sql=CONCAT('UPDATE ', in_errortext, 'SET x=1');
更新 sql 设置 x =1;
结束$$

但没用:(。

请帮助我。我将非常感激!

我特别不擅长MYSQL @Stored Procedures。

如果您想在此处尝试您的系统,您可以快速找到构建此数据库的命令。

编辑 ~ MySQL 解释器充当编译器!

我们不能从触发器和存储函数中调用动态 SQL语句。

为了在所有存储的例程中保持相同的接口 my_signal() 我修改了 my_signal(); 添加Static SQL语句Dynamic SQL并确保它不会从触发器执行。我认为应该允许because MySQL is an interpreter not a compiler

新的 my_signal()

DELIMITER $$
CREATE PROCEDURE `my_signal`(in_errortext VARCHAR(255))
BEGIN

    IF in_errortext = 'ERROR_INSERT_OWNER' THEN  /* Static SQL*/
        UPDATE `Error: can not insert new OWNER !` set x=1; 
    END IF;

    IF in_errortext = 'ERROR_INSERT_SELF_REFERENCE' THEN /* Static SQL*/
        UPDATE `Error: Row can not reference itself!` set x=1;

    ELSE /* Dynamic SQL*/    
        SET @sql=CONCAT('UPDATE `', in_errortext, '` SET x=1');
        PREPARE my_signal_stmt FROM @sql;
        EXECUTE my_signal_stmt;
        DEALLOCATE PREPARE my_signal_stmt;
    END IF;
END$$  

新触发器

DELIMITER $$
CREATE
    TRIGGER `employee_before_insert` BEFORE INSERT
    ON `Employee`
    FOR EACH ROW BEGIN
        CASE
         WHEN UPPER(NEW.name) = 'OWNER'  THEN
            CALL my_signal('ERROR_INSERT_OWNER');

         WHEN NEW.SSN = NEW.MSSN THEN
            CALL my_signal('ERROR_INSERT_SELF_REFERENCE');
        END CASE; 
    END$$   
DELIMITER ;

希望我尝试插入:

mysql> INSERT INTO Employee VALUES ("9", "X", "WOKER", "9" );
ERROR 1336 (0A000): Dynamic SQL is not allowed in stored function or trigger
mysql> INSERT INTO Employee VALUES ("9", "X", "OWNER", "9" );
ERROR 1336 (0A000): Dynamic SQL is not allowed in stored function or trigger  

哎呀!它的行为类似于编译器,而不是解释器。不是吗?

编辑:回答

幸运的是,我在编辑问题时得到了RolandoMySQLDBA的回答。
答案在这里
最后:我可以理解我们不能在触发器中执行动态 SQL。我可能不得不求助于对 my_signal() 进行编码。MySQL 现在还没有其他选择。

4

1 回答 1

1

Unfortunately, prior to MySQL 5.5 the only way to force a ROLLBACK from within a trigger is to generate an error just like you did intentionally.

This causes two problems.

  1. the error message is very confusing for the developer since you can't return a custom error message
  2. If a "real" error would be caused by the manipulating statment, under some circumstances you might even overwrite this and make debugging even harder.

This is from the view of a developer - I had a lot of discussions with our DBA concerning this.. but even a well done database - especially if built on MySQL - sometimes must rely on the application. :)

于 2012-11-26T17:14:46.667 回答