关于stackoverflow的第一个问题:
我正在使用MYSQL数据库
我有一张表,我想跟踪它的变化。我的具体目标是将更新列的名称放入跟踪原始表的历史记录表中。
到目前为止,我已经在表accountuser中创建了一个历史表,其中有4个额外的列用于审计。 我想在列更新列中写下更新列的名称,有关如何实现此目的的任何帮助?在下面的查询中,这是我需要帮助的地方:[这里我想要更新列的名称]
CREATE TABLE hefboom.accountuser_history LIKE hefboom.accountuser;
ALTER TABLE hefboom.accountuser_history MODIFY COLUMN Id int(11) NOT NULL,
DROP PRIMARY KEY, ENGINE = MyISAM, ADD action VARCHAR(8) DEFAULT 'insert' FIRST,
ADD revision INT(6) NOT NULL AUTO_INCREMENT AFTER action,
ADD dt_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER revision,
ADD updatedcolumn VARCHAR (50) DEFAULT NULL AFTER dt_datetime,
ADD PRIMARY KEY (Id, revision);
DROP TRIGGER IF EXISTS hefboom.accountuser__ai;
DROP TRIGGER IF EXISTS hefboom.accountuser__au;
DROP TRIGGER IF EXISTS hefboom.accountuser__bd;
CREATE TRIGGER hefboom.accountuser__ai AFTER INSERT ON hefboom.accountuser
FOR EACH ROW
INSERT INTO hefboom.accountuser_history SELECT 'insert', NULL, NOW(), [updatedcolumns can be left NULL]
FROM hefboom.accountuser AS d WHERE d.Id = NEW.Id;
CREATE TRIGGER hefboom.accountuser__au AFTER UPDATE ON hefboom.accountuser FOR EACH ROW
INSERT INTO hefboom.accountuser_history SELECT 'update', NULL, NOW(), [updatedcolumns can be left NULL]
FROM hefboom.accountuser AS d WHERE d.Id = NEW.Id;
CREATE TRIGGER hefboom.accountuser__bd BEFORE DELETE ON hefboom.accountuser FOR EACH ROW
INSERT INTO hefboom.accountuser_history SELECT 'delete', NULL, NOW(), [Here I want the name of the updated column]
FROM hefboom.accountuser AS d WHERE d.Id = OLD.Id;
我的第二个问题是,如果在一个事务中更新了一列以上,是否会在历史表中添加多行?
感谢任何帮助。