命名 MYSQL 时间戳列的语义

发布于 2024-10-15 18:01:55 字数 361 浏览 4 评论 0原文

我需要一个表示以下数据的 MySQL 表:

  • 2 月 12 日下午 5:00 - Verdasco VS Monfils
  • 2 月 12 日下午 9:25 - Sampras VS Hewitt
  • 2 月 13 日, 8:15am - Nishikori VS Del Potro

我想将时间列命名为 timetimestampdate,但正如你所言要知道,这些都是保留关键字。命名时间列的最佳方式是什么,而又不会故意听起来很俗气,从而绕过命名限制?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假装不在乎 2024-10-22 18:01:55

我理解名为 Events 的表中名为 EventDate 的列(根据我学过的所有数据库约定,应该称为 Event< /em>... ;)) 是多余的,但请考虑:

您有一个 Event 表,其中包含 Date 列和 Booking 表,其记录代表客户在活动中预订座位,还带有日期列(在本上下文中,指的是预订座位的日期

SELECT e.Date AS EventDate, b.Date AS BookingDate, [...]
FROM Event e
JOIN Booking b ON b.EventId = e.Id

:在每个此类查询中考虑不明确的列名是一种惩罚,它可能(在我看来确实如此)超过了 Event.EventDate 的假定冗余。

考虑另一个参数,我们可以将其应用于每个表中的 ID 列的名称:在 Event EventId 中调用 PK 而不仅仅是 Id 让我们编写这个 JOIN 标准:

JOIN Booking b ON b.EventId = e.EventId

我们一眼就能看到它引用了正确的列。

总之,如果我们必须在调用列 DateEventDate 之间做出决定,后者有很多好处:

  • 正如 @Benjamin Seiller 指出的,列应该标识列的含义,不一定是其数据类型
  • 此外,当您的表包含三个不同的日期字段(表示与实体相关的日期)时,会发生什么情况?如果您没有根据它们的含义来命名它们,那么您现在必须记住哪个字段意味着什么...
  • 一旦我们希望查询多个表,不明确的列名就会出现问题
  • 。出现在多个表中(例如外键)具有显式且相同的名称,我们的查询语法变得更能抵抗错误(例如加入错误的列)

在决定列时考虑可能使用表的所有情况名称。在这种情况下,我认为在列名中提供特定于上下文的信息的好处远远超过重复表名的冗余。

I understand the feeling that a column called EventDate in a table called Events (which, by all the DB conventions I've ever learned, should be called Event... ;)) is redundant, but consider:

You have an Event table with a Date column and a Booking table, whose records represent a customer's booking a seat at an event, also with a Date column (which, in this context, refers to the date that the seat was booked):

SELECT e.Date AS EventDate, b.Date AS BookingDate, [...]
FROM Event e
JOIN Booking b ON b.EventId = e.Id

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 merely Id lets us write this JOIN criterion:

JOIN Booking b ON b.EventId = e.EventId

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 or EventDate, the latter has many benefits:

  • As @Benjamin Seiller points out, a column should identify the column's meaning, not necessarily its datatype
  • Further, what happens when you have a table with three different date fields representing dates relevant to the entity? If you don't name them all based on their meaning, you now have to remember which field means what...
  • As soon as we wish to query multiple tables, ambiguous column names present problems
  • If columns that appear in multiple tables (such as foreign keys) have explicit and identical names, our query syntax becomes a little more resistant to bugs (such as joining on the wrong column)

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.

画中仙 2024-10-22 18:01:55

我更喜欢过去时和“时间”中的动词,即“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.

一桥轻雨一伞开 2024-10-22 18:01:55

列名不应指向数据类型,而应指向其含义。
例如 event_start_datetimecreated 用于创建该条目的时间戳。

The column-name should not point to the data-type but to its meaning.
Like event_start_datetime or created for the timestamp of creation of that entry.

樱&纷飞 2024-10-22 18:01:55

沃达斯科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.

若言繁花未落 2024-10-22 18:01:55

MySQL 使用反引号作为其标识符分隔符。如果将标识符括在反引号中,则可以使用保留关键字(或标识符名称中甚至有空格):

SELECT `timestamp` FROM  `tennis matches`;

只是不要忘记之后始终对这些(或所有)标识符使用反引号。

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:

SELECT `timestamp` FROM  `tennis matches`;

Just don't forget to always use backticks for those (or all) identifiers afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文