doubianyu7844 2013-06-11 09:39 采纳率: 0%
浏览 26
已采纳

mysql触发器或php代码

I have MySQL database that has 21 tables, in each table I have 'created' column(timestamp). this field holds insert time of row.

However:

1- I can write a trigger for fill the column for each table like this:

CREATE TRIGGER `created` BEFORE INSERT ON `some_table`
 FOR EACH ROW SET NEW.`created`=NOW()

2- I can handle it by PHP:

    $result = mysqli_query($con,"INSERT INTO table_name (column1, column2, column3,created)
VALUES (value1, value2, value3,now()) ");

I concerned about writing trigger for each table has overhead. Is it right? which one is better specific on performance? which one is better at all?

  • 写回答

1条回答 默认 最新

  • dongtan9518 2013-06-11 09:45
    关注

    You should not need to fill this column yourself if you configure the TIMESTAMP properties for it... http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html

    To clarify from the documentation here is a working example of how subsequent updates do not change a timestamp field when correctly configured for the desired behaviour

    create temporary table example (apID int auto_increment, 
                          audit timestamp DEFAULT CURRENT_TIMESTAMP, 
                          name varchar(50),
                          primary key(apID));
    insert into example(name) values('test');
    select * from example;
    update example set name='tes' where apID=1;
    select * from example;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用