I have a table where I have an auto increment column id
and another column ord
.
My goal is to have a 'default' value for ord
that is id
. I am using a trigger (see below), but it always uses 0
instead of the id
value.
When I remove the auto_increment from id
, it works as it should. How can I set the 'default' value of a field to the one of an auto_increment field?
Table
CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ord` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
Trigger
CREATE TRIGGER `ord_ai` BEFORE INSERT ON `mytable`
FOR EACH ROW IF NEW.ord IS NULL THEN
SET NEW.ord := NEW.id;
END IF
Read the next
AUTO_INCREMENT
value of the id field fromINFORMATION_SCHMEA.TABLES
and use it forord
field.