Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantics of naming a MYSQL timestamp column

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?

like image 397
JoJo Avatar asked Feb 02 '11 20:02

JoJo


2 Answers

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.

like image 109
Dan J Avatar answered Oct 10 '22 12:10

Dan J


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.

like image 20
Dennis Kreminsky Avatar answered Oct 10 '22 11:10

Dennis Kreminsky