开发者

Which method for storing creation date, update, creator ... should I use

I am designing a database for a site which will be heavily datadriven with forums and chat and other community features.

My question is this: I have identified certain information I would like to store for at least most of the tables. Creation date, last update, creator and others.

Which is the best way to store these, in a dedicated table or as fields in each respective table?

Why is the better solution better? Can the opinion of best solution change if I have 50 tables or 100 tables? 100 000 records vs 4 million?

Are there other aspects开发者_运维技巧?


Creation date and last update date are best stored as columns in the same table with their respective data. By doing that, you can use the TIMESTAMP data type so they are updated automatically when their respective rows change.

The NULL timestamp on the row_created column will default to the timestamp when the row is created. In row_updated, the timestamp will be updated automatically every time the row is modified in any way. You do not need to modify them in application code.

CREATE TABLE test.table (
  row_created TIMESTAMP NULL,
  row_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

The creator user isn't as critical to store in the same table, but it doesn't really hurt to do it in the same table or another. For simplicity I would keep it in the same table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜