命名 MYSQL 时间戳列的语义
我需要一个表示以下数据的 MySQL 表:
- 2 月 12 日下午 5:00 - Verdasco VS Monfils
- 2 月 12 日下午 9:25 - Sampras VS Hewitt
- 2 月 13 日, 8:15am - Nishikori VS Del Potro
我想将时间列命名为 time、timestamp 或 date,但正如你所言要知道,这些都是保留关键字。命名时间列的最佳方式是什么,而又不会故意听起来很俗气,从而绕过命名限制?
I need a MySQL table that represents this data:
- Feb 12, 5:00pm - Verdasco VS Monfils
- Feb 12, 9:25pm - Sampras VS Hewitt
- Feb 13, 8:15am - Nishikori VS Del Potro
I wanted to name the time column time, timestamp, or date, but as you know, those are all reserved keywords. What is the best way to name a time column without purposely sounding cheesy to get around the naming limitations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我理解名为
Events
的表中名为EventDate
的列(根据我学过的所有数据库约定,应该称为 Event< /em>... ;)) 是多余的,但请考虑:您有一个
Event
表,其中包含Date
列和Booking
表,其记录代表客户在活动中预订座位,还带有日期
列(在本上下文中,指的是预订座位的日期):在每个此类查询中考虑不明确的列名是一种惩罚,它可能(在我看来确实如此)超过了
Event.EventDate
的假定冗余。考虑另一个参数,我们可以将其应用于每个表中的 ID 列的名称:在
Event
EventId
中调用 PK 而不仅仅是Id
让我们编写这个 JOIN 标准:我们一眼就能看到它引用了正确的列。
总之,如果我们必须在调用列
Date
或EventDate
之间做出决定,后者有很多好处:在决定列时考虑可能使用表的所有情况名称。在这种情况下,我认为在列名中提供特定于上下文的信息的好处远远超过重复表名的冗余。
I understand the feeling that a column called
EventDate
in a table calledEvents
(which, by all the DB conventions I've ever learned, should be called Event... ;)) is redundant, but consider:You have an
Event
table with aDate
column and aBooking
table, whose records represent a customer's booking a seat at an event, also with aDate
column (which, in this context, refers to the date that the seat was booked):Having to account for the ambiguous column names in every such query is a penalty which may (and does, in my opinion) outweigh the supposed redundancy of
Event.EventDate
.Consider another argument which we can apply to the names of the ID columns in each table: calling the PK in
Event
EventId
instead of merelyId
lets us write this JOIN criterion:Which we can see at a glance references the correct columns.
In summary, if we have to make the decision between calling the column
Date
orEventDate
, the latter has many benefits:Consider all the situations in which your table may be used when deciding on column names. In this case, I'll argue that the benefits of supplying context-specific information in the column name far outweigh the redundancy of repeating the table name.
我更喜欢过去时和“时间”中的动词,即“createdTime”、“publishedTime”,在你的情况下我找不到动词,但“matchTime”看起来不错。
I prefer verb in past tense and "Time", i.e. 'createdTime', 'publishedTime', in your case I can't find a verb, but 'matchTime' looks ok.
列名不应指向数据类型,而应指向其含义。
例如
event_start_datetime
或created
用于创建该条目的时间戳。The column-name should not point to the data-type but to its meaning.
Like
event_start_datetime
orcreated
for the timestamp of creation of that entry.沃达斯科VS孟菲尔斯是什么?它们是事件吗?然后我会选择
EventDate
或类似的东西。尝试按照您的描述方式命名它。What is Verdasco VS Monfils? Are they events? Then I would go with
EventDate
or something similar. Try to name it of how you would describe it.MySQL 使用反引号作为其标识符分隔符。如果将标识符括在反引号中,则可以使用保留关键字(或标识符名称中甚至有空格):
只是不要忘记之后始终对这些(或所有)标识符使用反引号。
MySQL uses the backtick as its identifier delimiter. If you wrap an identifier in backticks, you can use a reserved keyword (or even spaces) in an identifier name:
Just don't forget to always use backticks for those (or all) identifiers afterwards.