If you specify a date value without a time component, then the default time is midnight. If you specify a date value without a date, then the default date is the first day of the current month.
Oracle DATE columns always contain fields for both date and time. If your queries use a date format without a time portion, then you must ensure that the time fields in the DATE column are set to midnight.
解决方案是对日期数据类型的列施加约束,并在插入或更新表中的行时创建触发器(使用 TRUNC()
)。
如果我使用此解决方案,是否可以保证 Oracle 确实会为没有时间的日期存储更少的字节?
Oracle 使用此标准日期时间类型会产生歧义。创建一个日期类型(只包含一个日期)这么难?这是我的看法(我来自 MSSQL)。
最佳答案
不,您没有任何保证......无论发生什么,Oracle 都会存储现在是午夜的事实。您不能存储没有时间的日期。
如果创建下表:
create table a ( dt date);
insert into a values(sysdate);
insert into a values(trunc(sysdate));
然后运行这个查询:
select dt, dump(dt) from a
返回值是:
+-----------------------------+------------------------------------+ | DT | DUMP(DT) | +-----------------------------+------------------------------------+ | June, 12 2013 18:03:15+0000 | Typ=12 Len=7: 120,113,6,12,19,4,16 | | June, 12 2013 00:00:00+0000 | Typ=12 Len=7: 120,113,6,12,1,1,1 | +-----------------------------+------------------------------------+
DUMP()
返回数据类型、字节长度和数据的内部表示。
换句话说,带时间的日期和被截断的日期都有 7 个字节。它们的长度相同。
顺便说一句,我建议不要因为担心空间问题而破坏可能有用的数据。
关于oracle - 没有时间存储日期是否使用更少的字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17072147/