【问题标题】:syntax for trigger before insert [closed]插入前触发器的语法[关闭]
【发布时间】:2012-07-28 23:04:02
【问题描述】:

好的,所以我see 认为这是错误的做法:

mysql> 
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    |                |
+-----------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> show create table newsgroups;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                      |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| newsgroups | CREATE TABLE `newsgroups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `newsgroup` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> ALTER TABLE newsgroups ADD UNIQUE (newsgroup);
ERROR 1170 (42000): BLOB/TEXT column 'newsgroup' used in key specification without a key length
mysql> 

has 应该填充trigger?

好的,作为root我做了一个触发器:

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

mysql> 
mysql> delimiter |
mysql> CREATE TRIGGER make_hash BEFORE INSERT ON newsgroups
    ->   FOR EACH ROW BEGIN
    ->       INSERT INTO hash values ('0');
    ->   END;
    -> |
Query OK, 0 rows affected (0.18 sec)

mysql> 

但是,这只是虚拟数据。如何使该触发器实际创建哈希?

【问题讨论】:

  • 'TRIGGER command denied to user ...' - 你应该检查你的用户有什么权限,并在必要时用不同的用户运行创建触发器。
  • @Vatev 我会以 root 身份尝试,但我认为这不是问题,用户 java 有 USAGE

标签: mysql triggers schema constraints mysql-error-1170


【解决方案1】:

我认为您应该保持主键不变。
您可以添加一个哈希列

ALTER TABLE `newsgroups` ADD COLUMN `hash` CHAR(32) NOT NULL DEFAULT '';

然后用

填充它
UPDATE newsgroups SET hash = MD5(newsgroup);

然后删除重复项并添加您的唯一约束。

您还可以添加BEFORE INSERTBEFORE UPDATE 触发器来设置hash

CREATE DEFINER=`root`@`localhost` 
TRIGGER `before_insert_newsgroups` 
BEFORE INSERT ON `newsgroups` 
FOR EACH ROW BEGIN

    set new.hash = md5(new.newsgroup);

END

根据您使用的 SQL 客户端,您可能不想change the DELIMITER before and after the create trigger statement

【讨论】:

  • 我正在查看manual,您是否正在使用 SQL 来更改表?这些命令来自 MySql 控制台?
  • 这不是一个完整的陈述,我补充了缺失的部分。
  • ok 创建了一个虚拟触发器,但无法获得您上面描述的那种触发器的语法。
  • 我似乎在这里遗漏了一些东西。您需要另一个表来存储哈希值吗?
  • 同一张表应该有新闻组(只是一个字符串,80个字符左右)和hash,这样hash才能设置为唯一的。
猜你喜欢
  • 2012-03-16
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多