开发者

Setting one column value after another

开发者 https://www.devze.com 2022-12-17 10:31 出处:网络
I have a tab开发者_JS百科le where I want one value to be set to the same as another when inserting.

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,

_L


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号