2012-07-28 44 views
0

好之前触发,所以我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> 

的已经应该用trigger填充?

好了,因为我的根做了一个触发像这样:

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> 

然而,这只是虚拟数据。我怎么能让这个触发器实际上创建哈希?

+0

“TRIGGER命令剥夺了用户.. 。' - 你应该检查你的用户拥有什么特权,并且如果有必要的话,用不同的用户运行创建触发器。 – Vatev 2012-07-28 23:41:48

+0

@Vatev我会以root身份尝试,但我不认为这是问题,用户java有'USAGE'。 – Thufir 2012-07-28 23:55:50

回答

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客户端上使用的是你可能wan't到change the DELIMITER before and after the create trigger statement

+0

我正在查看[手册](http://dev.mysql.com/doc/refman/5.0/zh-CN/create-trigger.html),并且您是否在给SQL改变表格?这些命令来自MySql控制台? – Thufir 2012-07-28 23:22:10

+0

这并不是一个完整的陈述,我添加了缺失的部分。 – Vatev 2012-07-28 23:39:32

+0

ok创建了一个虚拟触发器,但无法获得上面描述的那种触发器的语法。 – Thufir 2012-07-29 06:35:06