mysql - 在MYSQL中创建记录表

标签 mysql triggers

当客户端在表上添加新行时,如何创建记录表,该记录表会保留 ;

  • 客户 ID,
  • 添加数据后,
  • 数据 ID。

例如我在mysql中有一个PersonTable;当一个客户(users表中的Id为2)添加一个Id为12的新人时,我的记录表会自动添加cliend_id(2),related_Id(12)和新记录的创建时间。

我认为mysql中有一种方法,可以编写触发器,但是有没有可能的方法。我是mysql的初学者。谢谢

最佳答案

我不确定您运行的是哪个版本的 MySQL,但从版本 5.5 开始,使用 TIMESTAMP 作为类型时这是可能的。 >=5.6.5 版本也支持 DATETIME 类型的此功能。

-- TIMESTAMP example with 1 column
CREATE TABLE foo (
  'current_ts' TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- DATETIME example with 2 columns
CREATE TABLE foo (
    'creation_time'     DATETIME DEFAULT CURRENT_TIMESTAMP,
    'modification_time' DATETIME ON UPDATE CURRENT_TIMESTAMP
)

来自文档:

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table. The following notes first describe automatic initialization and updating for MySQL 5.6.5 and up, then the differences for versions preceding 5.6.5.

https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html

https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

关于mysql - 在MYSQL中创建记录表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42112630/

相关文章:

python - mysql插入代码产生以下错误: Not all parameters were used in the SQL statement

MySQL 在 <ColumName> 上创建外键时出错(检查数据类型)

MySQL - 将自动插入其他表的触发器

sql - 使用oracle plsql触发器从生日计算年龄并将年龄插入表中

mysql - 如何在MySQL中更新第三个表时将多个记录从一个表插入到另一个表中

mysql - 修剪 mysql 表 - 平均记录

php - HMVC 和文件夹中的 View (Codeigniter)

WPF : Chainging another control's property in some control's Trigger

google-apps-script - 为什么 onChange 和 onEdit 触发器会同时触发?

mysql - 根据之前的 COUNT() 显示多个组的最大值