mysql - before update 触发器生成哈希的语法

标签 mysql sql syntax triggers mysql-workbench

如何获取更新后生成的哈希值?

thufir@dur:~$ 
thufir@dur:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 138
Server version: 5.1.58-1ubuntu1 (Ubuntu)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use nntp;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------+
| Tables_in_nntp      |
+---------------------+
| articles            |
| newsgroups          |
| newsgroups_articles |
+---------------------+
3 rows in set (0.00 sec)

mysql> describe newsgroups;
+-----------+----------+------+-----+---------+----------------+
| Field     | Type     | Null | Key | Default | Extra          |
+-----------+----------+------+-----+---------+----------------+
| id        | int(11)  | NO   | PRI | NULL    | auto_increment |
| newsgroup | longtext | NO   |     | NULL    |                |
| hash      | char(32) | NO   |     | NULL    |                |
+-----------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> show triggers;
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger   | Event  | Table      | Statement                                           | Timing | Created | sql_mode | Definer        | character_set_client | collation_connection | Database Collation |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| make_hash | INSERT | newsgroups | BEGIN
    set new.hash = md5(new.newsgroup);
  END | BEFORE | NULL    |          | root@localhost | utf8                 | utf8_general_ci      | latin1_swedish_ci  |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

mysql> 
mysql> 
mysql> DELIMITER $$
mysql> 
mysql> USE `nntp`$$
Database changed
mysql> CREATE
    -> TRIGGER `nntp`.`make_hash_update`
    -> AFTER UPDATE ON `nntp`.`newsgroups`
    -> FOR EACH ROW
    -> BEGIN
    -> set old.hash = md5(new.newsgroup);
    -> END$$
ERROR 1362 (HY000): Updating of OLD row is not allowed in trigger
mysql> 
mysql> quit;
    -> exit
    -> ^CCtrl-C -- exit!
Aborted
thufir@dur:~$ 

此外,从工作台中,我看到旧的触发器:

enter image description here

但不知道如何添加额外的触发器。

最佳答案

不是更新后,而是更新前,而且是新的。不太明白语法,但这至少不会产生语法错误。想要使用工作台:

thufir@dur:~$ 
thufir@dur:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 144
Server version: 5.1.58-1ubuntu1 (Ubuntu)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use nntp;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show triggers;
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger   | Event  | Table      | Statement                                           | Timing | Created | sql_mode | Definer        | character_set_client | collation_connection | Database Collation |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| make_hash | INSERT | newsgroups | BEGIN
    set new.hash = md5(new.newsgroup);
  END | BEFORE | NULL    |          | root@localhost | utf8                 | utf8_general_ci      | latin1_swedish_ci  |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

mysql> show create trigger make_hash;
+-----------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Trigger   | sql_mode | SQL Original Statement                                                                                                                                             | character_set_client | collation_connection | Database Collation |
+-----------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| make_hash |          | CREATE DEFINER=`root`@`localhost` TRIGGER `nntp`.`make_hash`
BEFORE INSERT ON `nntp`.`newsgroups`
FOR EACH ROW
BEGIN
    set new.hash = md5(new.newsgroup);
  END | utf8                 | utf8_general_ci      | latin1_swedish_ci  |
+-----------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

mysql> 
mysql> DELIMITER $$
mysql> 
mysql> USE `nntp`$$
Database changed
mysql> CREATE
    -> TRIGGER `nntp`.`make_hash_update`
    -> BEFORE UPDATE ON `nntp`.`newsgroups`
    -> FOR EACH ROW
    -> BEGIN
    -> set new.hash = md5(new.newsgroup);
    -> END$$
Query OK, 0 rows affected (0.19 sec)

mysql> 
mysql> show triggers;
    -> ^CCtrl-C -- exit!
Aborted
thufir@dur:~$ 

关于mysql - before update 触发器生成哈希的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11707511/

相关文章:

sql - 存储过程没有数据输出(Postgresql)

php - 从 postgresql 后端的存储过程中检索通知

java - 使用名称模式调用方法

linux - 在 Linux 命令行上使用 lzma2 压缩和压缩多个文件的具体命令是什么?

sql - Postgresql:一列表的主键

PHP和mysql语法错误

mysql - 模型中的错误检查

如果用户名已被使用,jQuery 检查 mysql 数据库

php - 如何构造一个 SQL 查询来从同一个表的不同行中获取不同的列?

php - 删除层次结构中的孤立项