Setting one column value after another
I have a tab开发者_JS百科le where I want one value to be set to the same as another when inserting.
Here's the table in question;
CREATE TABLE categories (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(30) NOT NULL,
root INT NULL DEFAULT id,
PRIMARY KEY(id)
);
I want the column ´root´ to get the same value as the ´id´ column gets when inserting a new row. I guess I can just do it in two queries, but I hoped I could do it in just one.
Thanks,
_LI would use triggers. Do you use MySQL 5.0.2 or later? Because before that MySQL does not have triggers.
Try:
CREATE TRIGGER id_root_same AFTER INSERT ON categories
FOR EACH ROW SET root = NEW.id;
But why would you do such a thing? It does not make sense.
精彩评论