当客户端在表上添加新行时,如何创建记录表,该记录表会保留 ;
- 客户 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/