Reputation: 8487
Ok, so I see that this the wrong approach:
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>
The has should be populated with a trigger?
Ok, as root I made a trigger as so:
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>
However, that's just dummy data. How can I make that trigger actually create the hash?
Upvotes: 0
Views: 769
Reputation: 7590
I think you should leave your primary key as is.
You can add a hash column
ALTER TABLE `newsgroups` ADD COLUMN `hash` CHAR(32) NOT NULL DEFAULT '';
and then populate it with
UPDATE newsgroups SET hash = MD5(newsgroup);
then remove the duplicates and add your unique constraint.
You can also add BEFORE INSERT
and BEFORE UPDATE
triggers to set the hash
value
CREATE DEFINER=`root`@`localhost`
TRIGGER `before_insert_newsgroups`
BEFORE INSERT ON `newsgroups`
FOR EACH ROW BEGIN
set new.hash = md5(new.newsgroup);
END
Depending on the SQL client you are using you might wan't to change the DELIMITER before and after the create trigger statement
Upvotes: 1