我的MySQL数据库access & user中有两个表。我想要创建一个触发器,它在每次访问用户表时都会激活,并记录时间戳和请求的所有列。
因此,例如,如果有人从user ID=20请求ID=20,那么触发器将在可访问表中创建一个新行,该行记录了userID, timestamp in unix format, rows column So userID=20, timestamp=1515147950, rowAccessed=name。
这样的扳机看上去会怎么样?
编辑:
用户表(InnoDB):
| ID | name | email | age |
+----+--------+-------------+-----+
| 1 | Alice | alice@a.com | 20 |
| 2 | Bo b | bob@b.com | 12 |
| 3 | Carl | carl@c.com | 32 |访问表(InnoDB):
| ID | userID | timeStamp | column |
+----+--------+-------------+--------+
| 1 | 2 | 1515149281 | name |
| 2 | 1 | 1515148251 | email |access表中的数据是我希望触发器填充的数据。
Access表中的userID列通过InnoDB关系链接到用户表的ID。
发布于 2018-01-05 11:24:12
不需要说你的问题最好的选择就是从代码中处理它。但如果有必要的话.这是一种方法,可能行不通,我无法访问MySQL来测试它,但这是我要从以下方面开始的:
create table user (
ID int primary key,
name text,
email text,
age int
)
create table access (
ID int primary key auto_increment,
userID int,
time timestamp,
columnName text
)
insert into user values (1, 'Alice', 'alice@alice.com', 20), (2, 'Bob', 'bob@bob.com', 25)
create procedure selectUser(colName Boolean, colEmail Boolean, colAge Boolean, id INTEGER)
BEGIN
DECLARE justNow timestamp;
select now() into justNow;
IF colName THEN
insert into access(userID, time, columnName) values (id, justNow, 'name');
END IF;
IF colEmail THEN
insert into access(userID, time, columnName) values (id, justNow, 'email');
END IF;
IF colAge THEN
insert into access(userID, time, columnName) values (id, justNow, 'age');
END IF;
SET @s = CONCAT('SELECT name FROM user');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
call selectUser(true, true, true, 1)我还没有完成列查询的部分,但这很简单。如果这个方法对你有效,请告诉我们。
https://stackoverflow.com/questions/48111694
复制相似问题