When I am looking to create some timestamp fields (or other date/time style fields), what is the best way to name them? Should I just put record_timestamp?
You should describe the purpose of the column and not necessarily the data type. You can include date/time/timestamp in the name, but you should also include the meaning. For example
Adding Date/Time/Timestamp and such at the end is particularly useful when the abscence of the addition would conflict with another column. For example, a table may need both a Status and a StatusTime.
xyz_at for a
xyz_on for a
date field - eg
Normally I'd avoid including the data type in the field name - much better if you can infer what you need to know about the type from the name of any field (a field called
description is unlikely to be an
integer) - but being able to tell the difference between a
timestamp and a
date is often helpful.
I looked at your profile and it says you work with SQL Server and in SQL Server TIMESTAMP data type has nothing to do with date or time and its used to kind of version stamping the rows. This is very useful in identifying which rows have been modified from a given point of time.
If you use TIMESTAMP then you don't have to specify a column name and SQL Server will create a column "TimeStamp" for you. But it is recommended to use "ROWVERSION" data type and in this case you have to specify the column name.
What's the best name for a column like this? It depends, and I would use something like VersionStamp, RV etc... What I consider important is NOT how you name it but are you using that consistently across the board.
I found that using column names such as
expire_time leads to better readability when it comes to method naming and specs (RSpec).
I prefer to use a prefix of DT for date stamps. For example: DTOpened, DTClosed, DTLastAccessed. This lets me list out all the DTxxxx for a quick reference of all date stamps in a given table.
I work for Texas Instruments, and on their systems they use xxxx_dttm
I prefer using conventions that already exist.
Unix and programming languages have a widely accepted convention of
mtime for Modification Time
For creation time,
- BSD and Windows use birthtime
- Windows also uses Creation Time
- xstat uses
- ext4 uses
- JFS and btrfs use
otime(don't ask, guessing "origination").
So for me, I pick
crtime for meta data.
For user supplied data, I go with what the field represents. If it's a birthday, I just say
As far as precision, for some it seems to hang them up on too much precision. You can store your
birthdate as a timestamp (after all you were technically birthed at a time of day), but the SQL spec has casts from higher-precision to lower precision so if you're using a decent database this shouldn't be an issue. In your app itself, you can always truncate when needed. That is to say, I would never go
I would used a meaningful prefix and _TSMP as a suffix e.g. CREATION_TSMP or LAST_UPDATE_TSMP
In order to maintain consistency accross column names, I would suggest you to the following syntax:
dateCreated dateUpdated dateAccessed dateStarted date...
As suggest by @Evan Carroll, go with the existing standards unless you have strong reason to break the pattern.
If this is something new then you can follow any answer which best suits you.
I use *_on and *_by because it helps me to keep it consistent for when and who of the row:
- created_on & created_by - updated_on & updated_by - deleted_on & deleted_by -- soft delete - approved_on & approved_by