0

I have a table 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,
_L

ptrn
  • 4,902
  • 6
  • 28
  • 30

1 Answers1

0

I 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.

nalply
  • 26,770
  • 15
  • 78
  • 101