Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mySQL触发器,用于记录从特殊访问表中的表访问数据的时间和内容_Mysql_Triggers_Unix Timestamp - Fatal编程技术网

mySQL触发器,用于记录从特殊访问表中的表访问数据的时间和内容

mySQL触发器,用于记录从特殊访问表中的表访问数据的时间和内容,mysql,triggers,unix-timestamp,Mysql,Triggers,Unix Timestamp,我的MySQL数据库中有两个表access和user。我想创建一个触发器,它在每次访问用户表时激活,并记录一个时间戳和所有被请求的列 因此,例如,如果有人从user请求name,其中ID=20,那么触发器将在可访问表中创建一个新行,记录userID,unix格式的时间戳,rows列SouserID=20,timestamp=1515147950,rowAccessed=name 这样一个触发器大致看起来如何 编辑: 用户表(InnoDB): 访问表(InnoDB): access表中的数据是我希

我的MySQL数据库中有两个表
access和user
。我想创建一个触发器,它在每次访问用户表时激活,并记录一个时间戳和所有被请求的列

因此,例如,如果有人从
user
请求
name
,其中
ID=20
,那么触发器将在可访问表中创建一个新行,记录
userID,unix格式的时间戳,rows列
So
userID=20,timestamp=1515147950,rowAccessed=name

这样一个触发器大致看起来如何

编辑:

用户表(InnoDB):

访问表(InnoDB):

access表中的数据是我希望触发器填写的数据


Access表中的userID列通过InnoDB关系链接到user表的ID

无需说明问题的最佳选项是从代码中处理它。但如果有必要从Mysql中执行此操作。。。这是一种方法,可能不起作用,我没有访问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)

我还没有完成列查询的部分,但这很简单。让我们知道这种方法是否适合您。

SELECT语句上没有触发器,所以我必须通过php处理这样一个表吗?恐怕是这样……如果不直接对表进行SELECT,而是对自定义函数进行SELECT,这可以实现。。。但是不知道这对你的项目来说是不是太多的工作这个项目是关于学习如何使用mySQL及其所有的细节,所以如果你能解释一下你的意思或者给我一个链接,我可以在那里找到你的意思,我将非常感激。(没有什么比很多工作更重要XD)谢谢,它确实有效,这正是我想要的。我只使用了过程部分,因为我已经创建了所有结构,但该部分有效XD很好地知道:)
| ID | userID |  timeStamp  | column |
+----+--------+-------------+--------+
| 1  |   2    | 1515149281  | name   |
| 2  |   1    | 1515148251  | email  |
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)