简体   繁体   English

创建Mysql日期(更新记录时不更新)

[英]Mysql date created (not to update when record updated)

I'm doing a mysql table and i have one column that has current timestamp on update. 我正在做一个mysql表,我有一个列在更新时有当前时间戳。 This is great because i can see exactly when someone uploads something. 这很好,因为我可以确切地看到有人上传某些内容。 But i dont want the column to change when they edit their upload. 但我不希望列在编辑上传时更改。 Is it best to stick with one column named "date created" and have NO on update or go with two columns "date created & "date modified" - if so whats the best practice for column attributes, and PHP update statements? 是否最好坚持使用一个名为“创建日期”的列,并在更新时使用NO,或者使用“创建日期和修改日期”这两列: - 如果这样,列属性和PHP更新语句的最佳实践是什么?

I usually like to have a separate "CreateDate" and "LastModifiedDate". 我通常喜欢单独的“CreateDate”和“LastModifiedDate”。

As for setting it, it would be nice if you could just set the default value for the CreateDate column to NOW() , but MySQL doesn't allow that . 至于设置它,如果你可以将CreateDate列的默认值设置为NOW()会很好,但MySQL不允许这样做

So, the easiest option would be to use an insert trigger to do it: 因此,最简单的选择是使用插入触发器来执行此操作:

CREATE TRIGGER tbl_insert BEFORE INSERT ON `tbl`
    FOR EACH ROW SET NEW.CreateDate = NOW(), NEW.LastModifiedDate = NOW();

ETA: ETA:

You can use a TIMESTAMP field with a default value of CURRENT_TIMESTAMP and the ON UPDATE CURRENT_TIMESTAMP constraint for the LastModifiedDate as well. 您也可以使用TIMESTAMP字段,其默认值为CURRENT_TIMESTAMP ,而ON UPDATE CURRENT_TIMESTAMP约束也适用于LastModifiedDate。 Unfortunately you can't have two such TIMESTAMP columns on the same table, so the trigger is necessary to handle the CreateDate column. 遗憾的是,您不能在同一个表上有两个这样的TIMESTAMP列,因此触发器是处理CreateDate列所必需的。

Well it depends on how you are using the data value, but mostly it is a good practice to have both created_at and updated_at as attributes for your table. 那么它取决于你如何使用数据值,但是大多数情况下将created_atupdated_at都作为表的属性是一个好习惯。 I found both the attributes helpful in testing, and debugging. 我发现这两个属性都有助于测试和调试。

Just as extra info, remember the date(); 就像额外信息一样,记住date(); function in PHP. PHP中的函数。

Apart from that, I'd definitely have a last modified date, set to creation date by default. 除此之外,我肯定有一个上次修改日期,默认情况下设置为创建日期。

This is not really an answer as I don't think there is A best practice, but that's a question on which I had long thoughts. 这不是一个真正的答案,因为我认为没有最佳实践,但这是一个我长期思考的问题。

Before even knowing what a table would be used for, I used to create 3 fields: Date created, Date modified, and User, which would tell me when a row was created and by who, then when this row was modified and by who... Erm, no I have only one user field. 在知道表将用于什么之前,我曾经创建了3个字段:创建日期,修改日期和用户,这将告诉我何时创建行以及谁,然后何时修改此行以及由谁修改。 ..嗯,不,我只有一个用户字段。 So do I do a modifier and a creator field? 那么我要做一个修饰符和一个创建者字段吗? Then a user changes everything in an article, and another user corrects a mispelling... Who has changed what? 然后用户更改文章中的所有内容,而另一个用户更正了错误拼写...谁改变了什么?

In the end, I wanted to have a view over the history of database inputs, but following this very bad logic (date created, modified, user), I knew nothing. 最后,我想了解数据库输入的历史,但是遵循这个非常糟糕的逻辑(创建,修改日期,用户),我一无所知。 I know you didn't mention the user, but the problem remains for modifications. 我知道你没有提到用户,但问题仍然是修改。 And what about when it's deleted? 什么时候被删除? You won't know. 你不会知道的。

But get me right, I 'm not throwing the stone to anyone: most of my tables are still designed this way. 但是让我说对了,我不会把石头丢给任何人:我的大多数桌子仍然是这样设计的。 Nevertheless it's time for change, and I plan to create a history table, but I am not sure yet of its structure. 不过现在是改变的时候了,我打算创建一个历史表,但我还不确定它的结构。

So if you don't mind, while giving my opinion on your problem I'd like to suggest my two plans so far - not hugely different from one another - and see if people have an opinion about it. 因此,如果你不介意,在对你的问题发表意见的同时,我想建议我的两个计划到目前为止 - 彼此之间并没有太大的不同 - 看看人们对此有何看法。 And sorry again if I'm polluting your post, but I'm just jumping on the opportunity :-p 如果我污染了你的帖子,我会再次感到抱歉,但我只是抓住机会:-p

History table #1: 历史表#1:

Table RowID Action Date User 表RowID操作日期用户

Documents 465 Creation 2010-09-25 12:15:19 25 文件465 Creation 2010-09-25 12:15:19 25
Documents 465 Modification 2010-09-25 18:03:38 12 文件465修改2010-09-25 18:03:38 12
Documents 465 Modification 2010-12-28 14:15:30 25 文件465修改2010-12-28 14:15:30 25
Documents 465 Deletion 2011-01-25 14:55:31 33 文件465删除2011-01-25 14:55:31 33

In that case I wonder if there would be a use for a unique ID, as I don't see why I would be looking for a specific row. 在那种情况下,我想知道是否会使用一个唯一的ID,因为我不明白为什么我会寻找一个特定的行。 That will be always the history of a specific row for a specific table. 这将始终是特定表的特定行的历史记录。

History table #2: 历史表#2:

Table RowID History 表RowID历史记录

Documents 465 {[{action:"creation",date:"2010-09-25 12:15:19",user:25}, 文件465 {[{action:“creation”,date:“2010-09-25 12:15:19”,user:25},
{action:"modification",date:"2010-09-25 18:03:38",user:12}, {action:“modification”,date:“2010-09-25 18:03:38”,user:12},
{action:"modification",date:"2010-12-28 14:15:30",user:25}, {action:“modification”,date:“2010-12-28 14:15:30”,user:25},
{action:"deletion",date:"2011-01-25 14:55:31",user:33}]} {action:“删除”,日期:“2011-01-25 14:55:31”,用户:33}]}

Here one row will show us the whole history of one table row through a JSON Object (or serialized array). 这一行将通过JSON对象(或序列化数组)向我们显示一个表行的整个历史记录。 So I wouldn't wonder about the primary key: table and rowId will definitely do the job. 所以我不会怀疑主键:table和rowId肯定会完成这项工作。 Even if I prefer this one, the advantage of the first structure over this one is that you can search for events in a timeframe, while it's impossible here. 即使我更喜欢这个,第一个结构优于这个结构的优点是你可以在一个时间范围内搜索事件,而这在这里是不可能的。

Next step will be to decide if I add a text field to those structures to keep a DIFF in order to be able to restore a previous version. 下一步将决定是否向这些结构添加文本字段以保留DIFF以便能够恢复以前的版本。 But that's another story. 但这是另一个故事。

I know DateAdded and DateCreated can be the best solution for some situations but if the goal is to know about the history of your DB entries, I think a dedicated history table is the best choice. 我知道DateAdded和DateCreated可能是某些情况下的最佳解决方案,但如果目标是了解数据库条目的历史记录,我认为专用历史表是最佳选择。

This Is what I have and it seems to be working: 这就是我所拥有的,它似乎正在起作用:

Name type attributes default 名称类型属性默认

created timestamp CURRENT_TIMESTAMP 创建时间戳CURRENT_TIMESTAMP
modified timestamp on update CURRENT_TIMESTAMP CURRENT_TIMESTAMP 更新CURRENT_TIMESTAMP CURRENT_TIMESTAMP时修改的时间戳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM