我希望在每个会话的基础上指定列的默认值。下面的脚本不起作用,但描述了我想如何使用它。我目前使用的是 MySQL 5.5.28,但如有必要可以升级。
CREATE TABLE my_tbl (
id INT NOT NULL AUTO_INCREMENT,
data VARCHAR(45),
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
date_modified TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
created_by INT DEFAULT CUSTOM_USER_ID,
modified_by INT ON UPDATE CUSTOM_USER_ID
PRIMARY KEY (id)
) ENGINE = InnoDB;
-- Will set created_id and modified_id=111
SET @CUSTOM_USER_ID=111;
INSERT INTO my_tbl(data) VALUES('hello');
-- Will set modified_id=222
SET @CUSTOM_USER_ID=222;
UPDATE my_tbl SET data='goodby' WHERE id=1;
-- Will keep modified_id=222
SET @CUSTOM_USER_ID=333;
UPDATE my_tbl SET data='hello again',modified_id=modified_id WHERE id=1;