Temporals
The SQL Standard provides four data types for columns that contain temporal data: DATE, TIME, TIMESTAMP, and INTERVAL. Table 7-3 shows the SQL Standard requirements and the level of support (data type and storage size in bytes) the Big Eight have for these data types, while Table 7-4 shows the minimum and maximum temporal values/value sizes allowed for each data type.
DATE | TIME | TIMESTAMP | WITH TIME ZONE | INTERVAL | DATETIME | |
---|---|---|---|---|---|---|
ANSI SQL | Yes | Yes | Yes | Yes | Yes | No |
IBM | 4 | 3 | 10 | No | No | No |
Informix | 4 | No | No | No | 11 | 11 |
Ingres | 12 | No | No | No | No | No |
InterBase | 8 | 8 | 8 | No | No | No |
Microsoft | No | No | No | No | No | 8 |
MySQL | 3 | 3 | 4 | No | No | 8 |
Oracle | 7 | No | 11 | 13 | 11 | No |
Sybase | No | No | No | No | No | 8 |
DATE Min | DATE Max | TIME Precision Default | TIMESTAMP Precision Default | Fractional Seconds Specifiable | |
---|---|---|---|---|---|
ANSI SQL | 0001-01-01 | 9999-12-31 | N/S | N/S | Yes |
IBM | 0001-01-01 | 9999-12-31 | Second | Microsecond | No |
Informix | 0001-01-01 | 9999-12-31 | Millisecond | Millisecond | Yes |
Ingres | 0001-01-01 | 9999-12-31 | Second | Second | No |
InterBase | 0001-01-01 | 9999-12-31 | Second | Second | No |
Microsoft | 1753-01-01 | 9999-12-31 | Millisecond | Millisecond | No |
MySQL | 0001-01-01 | 9999-12-31 | Second | Second | No |
Oracle | 01-JAN-4712 BC | 31-DEC-4712 AD | Microsecond | Microsecond | Yes |
Sybase | 1753-01-01 | 9999-12-31 | Millisecond | Millisecond | No |
which ensures that every column1 value will be a time with a fractional seconds precision of two digitsfor example, TIME '15:30:10.25'.The main SQL Standard data types for temporal information are DATE, TIME, and TIMESTAMP. There is also an INTERVAL data type, supported only by Informix and Oracle; a TIME WITH TIME ZONE data type, supported only byPostgreSQL, and the PostgreSQL makers say you shouldn't use it; and a TIMESTAMP WITH TIME ZONE data type, supported only by Oracle, although IBM has a CURRENT TIMEZONE niladic function: subtracting CURRENT TIMEZONE from a local time converts the local time to Universal Coordinated Time (UTC). So there isn't much diversity here, except for precision and the curious business of the minimum date value.
CREATE TABLE Table1 (
column1 TIME(2)
...)
Minimum Date ValueLooking at Table 7-4, you'll see that the minimum date value varies a lot. The explanations for the various minimum year figures chosen are:"0001" (1 AD) is the first allowable year according to the SQL Standard."1582" is the year the Gregorian calendar was first used in some Catholic countries."1753" is the year the Gregorian calendar became official in England and America."4713 BC" is the year from which we count Julian days. The start date of the Julian period was calculated by Julius Scaliger, who proposed that days should be counted in decimal, without regard to months or years. Astronomers adopted this system and took noon GMT-4712-01-01 Julian (that is, January 1, 4713 BC) as their zero point. (Note that 4713 BC is the year 4712 according to the astronomical year numbering.)In fact, the differences are unimportant, because the DBMSs that support dates such as '0001-01-01' are merely projecting the Gregorian calendar backward in time as if it was in force in 1 AD. This assumption, which is called prolepticism, is falseso calculations that use old dates are specious. |
The Bottom Line: Temporals
If you use TIMESTAMP instead of DATE or TIME, you often aren't using any additional space because the DBMS uses a combined column for storage, internally. On the other hand, TIMESTAMPs sometimes take more space to store, partly because the default precision of a TIMESTAMP column includes a fractional seconds portion; information that is rarely necessary. Searches for the date component of a TIMESTAMP will be slow. The optimizer won't see that particular date values occur frequently because the time is stored along with it.Dates and times are just different magnitudes of the same propertyand we don't use two different columns to store the "integer" and "decimal" parts of a number, do we? Temporal comparisons and transfers are easier if there is only one data type. Furthermore, a TIME doesn't really mean anything unless the date is known as well.TIMESTAMP (or its non-standard SQL-extension equivalent, DATETIME) is supported by more DBMSs than the other temporal data types.Recommendation: Prefer TIMESTAMP for temporal data.