I have fairly simple basic table which I will populate with CHAR(4) primary keys.
These values are selected from a chars table with a subquery.
I want to set up a BEFORE INSERT query so that each time I INSERT a new record from PHP, the id field should get the generated CHAR(4) from subquery.
"ASDF","This is body text", "1970-01-01 01:01:01"
so on.
I'm pulling my hair out with this trigger.
DELIMETER |
CREATE TRIGGER messages_newid
BEFORE INSERT ON messages
FOR EACH ROW
BEGIN
SET NEW.id = (SELECT CONCAT(a.val,b.val,c.val,d.val)
FROM chars AS a
JOIN chars AS b
JOIN chars AS c
JOIN chars AS d
ORDER BY RAND()
LIMIT 1);
END
|
Here follows the table structure of messages
CREATE TABLE IF NOT EXISTS `messages` (
`id` varchar(4) collate utf8_unicode_ci NOT NULL,
`body` text collate utf8_unicode_ci,
`created` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And the trick chars table I have found within another answer, which is used for CHAR(4) randomness
CREATE TABLE IF NOT EXISTS `chars` (
`val` char(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
What does this have to do with privileges?
EDIT Here's the new error code
#1227 - Access denied; you need the SUPER privilege for this operation