仅以 HH24:MM 格式存储时间在表中?是否可以?
我想知道我是否可以存储一个名为 flight_time
的字段,这是航班起飞的时间,而不是像其他人建议的那样让我使用错误的间隔数据类型......
我想要为了只存储一个时间,而不是当前时间,而是将来的时间,我将日期写在单独的字段flight_date
中。
问题
也许我正在处理这个错误,因为我认为您可以将时间和日期存储在该字段中?
是否有任何数据类型可以屏蔽格式但我不想转换任何内容?
只使用 VARCHAR2
怎么样,因为一旦填充表格,我就会以任何方式输入它?
I want to know if I can store a field called flight_time
this is the time the flight departs not how long it takes like someone else suggested making me use the wrong data type of interval....
I want to just store a time and not the current time but one in the future I have the date written out in a separate field flight_date
.
Questions
Maybe I am approaching this wrong as I think you can store the time with the date in that one field?
Is there any data type where I can mask the format but I don't want to convert anything?
What about just using VARCHAR2
because I am going to type it out any way once I populate the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就我个人而言,我会选择自定义对象类型,但那是因为我是在面向对象的环境下长大的。如果你处理得当,它会给你带来很多好处。
Personally I'd go for a custom Object Type but that's because I've been brought up with OO. It will give you plenty of benefits if you approach it well.
您应该将航班日期和时间值存储为日期类型的单列。然后,您可以选择如何为您的 UI 设置该值的格式。
如果您使用 11g,您可以使用虚拟列对其进行预格式化,例如:
尽管格式化可能应该在您的 UI 代码中完成。
You should store flight date and time value as a single column of type date. You can then choose how to format that value for your UI.
If you use 11g you could pre-format it using virtual columns e.g:
although formatting should probably be done in your UI code.
Oracle 没有一天中的时间数据类型的概念。
DATE
,其中包括日期和时间。有TIMESTAMP
,其中包含秒的小数部分。Oracle 中的日期时间计算以一天为单位完成。所以,
SYSDATE+1
是明天的这个时间。SYSDATE+0.25
距离现在还有 6 小时。如果您想使用存储的时间来确定DATE
类型值,则应将其存储为NUMBER
。如果您只想存储看起来像时间的字符串,则可以使用 VARCHAR2,但您也可以存储无意义的值,例如 66:74。Oracle does not have the concept of a time of day data type. There's
DATE
, which includes both the date and time of day. There'sTIMESTAMP
, which includes fractional seconds.Datetime calculations in Oracle are done using units of one day. So,
SYSDATE+1
is this time tomorrow.SYSDATE+0.25
is 6 hours from now. If you ever want to use this time that you're storing to determine aDATE
type value, you should store it as aNUMBER
. If all you want is to store a string that looks like a time, you can useVARCHAR2
, but you would also be able to store nonsensical values like 66:74.有INTERVAL DAY TO SECOND,仅接受 0 秒值的检查约束可以满足您的需求:
There is INTERVAL DAY TO SECOND that, with a check constraint to accept only values with 0 seconds, could suite for your needs:
Oracle 一天中的时间概念是一天的一小部分。
因此,您可以将时间存储为 0 到 1 之间的浮点数,或者 0 到 1439 之间的整数分钟数。
在第一种情况下,您可以这样显示它:
TO_CHAR(TRUNC(SYSDATE)+FRACTIME, 'HH24:MI:SS')
在第二种情况下,您可以这样显示它:
TO_CHAR(TRUNC(SYSDATE)+(MINUTES/第1440章)
Oracle's notion of time within a day is fraction-of-a-day.
Accordingly, you could store time as a floating point number between zero and one, or as an integer number of minutes between 0 and 1439, for example.
In the first case, you could display it like this:
TO_CHAR(TRUNC(SYSDATE)+FRACTIME, 'HH24:MI:SS')
In the second case you could display it like this:
TO_CHAR(TRUNC(SYSDATE)+(MINUTES/1440), 'HH24:MI:SS')