仅以 HH24:MM 格式存储时间在表中?是否可以?

发布于 2024-12-20 03:00:19 字数 335 浏览 1 评论 0原文

我想知道我是否可以存储一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

九八野马 2024-12-27 03:00:20

就我个人而言,我会选择自定义对象类型,但那是因为我是在面向对象的环境下长大的。如果你处理得当,它会给你带来很多好处。

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.

很糊涂小朋友 2024-12-27 03:00:19

您应该将航班日期和时间值存储为日期类型的单列。然后,您可以选择如何为您的 UI 设置该值的格式。

如果您使用 11g,您可以使用虚拟列对其进行预格式化,例如:

create table t1
   ( flight_date date,
     flight_time as (to_char(flight_date,'HH24:SS:MI')),
     flight_day as (to_char(flight_date,'dd/mm/yyyy'))
   ); 

尽管格式化可能应该在您的 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:

create table t1
   ( flight_date date,
     flight_time as (to_char(flight_date,'HH24:SS:MI')),
     flight_day as (to_char(flight_date,'dd/mm/yyyy'))
   ); 

although formatting should probably be done in your UI code.

柠北森屋 2024-12-27 03:00:19

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's TIMESTAMP, 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 a DATE type value, you should store it as a NUMBER. If all you want is to store a string that looks like a time, you can use VARCHAR2, but you would also be able to store nonsensical values like 66:74.

白云不回头 2024-12-27 03:00:19

INTERVAL DAY TO SECOND,仅接受 0 秒值的检查约束可以满足您的需求:

SQL> create table flight (
  2     time interval day(0) to second (0),
  3     constraint chk_time
  4     check(
  5             extract(second from time) = 0
  6         )
  7  )
  8  /

Table created
SQL> insert into flight(time)
  2  values (interval '10:30' hour to minute)
  3  /

1 row inserted
SQL> insert into flight(time)
  2  values (interval '10:30:30' hour to second)
  3  /

insert into flight(time)
values (interval '10:30:30' hour to second)

ORA-02290: check constraint (ESTCEDAR.CHK_TIME) violated
SQL> insert into flight(time)
  2  values (interval '23:30' hour to minute)
  3  /

1 row inserted
SQL> insert into flight(time)
  2  values (interval '24:30' hour to minute)
  3  /

insert into flight(time)
values (interval '24:30' hour to minute)

ORA-01873: the leading precision of the interval is too small
SQL> insert into flight(time)
  2  values (interval '1 10:30:30' day to second)
  3  /

insert into flight(time)
values (interval '1 10:30:30' day to second)

ORA-01873: the leading precision of the interval is too small
SQL> select *
  2  from flight
  3  /

TIME
-------------------
+0 10:30:00
+0 23:30:00

SQL> 

There is INTERVAL DAY TO SECOND that, with a check constraint to accept only values with 0 seconds, could suite for your needs:

SQL> create table flight (
  2     time interval day(0) to second (0),
  3     constraint chk_time
  4     check(
  5             extract(second from time) = 0
  6         )
  7  )
  8  /

Table created
SQL> insert into flight(time)
  2  values (interval '10:30' hour to minute)
  3  /

1 row inserted
SQL> insert into flight(time)
  2  values (interval '10:30:30' hour to second)
  3  /

insert into flight(time)
values (interval '10:30:30' hour to second)

ORA-02290: check constraint (ESTCEDAR.CHK_TIME) violated
SQL> insert into flight(time)
  2  values (interval '23:30' hour to minute)
  3  /

1 row inserted
SQL> insert into flight(time)
  2  values (interval '24:30' hour to minute)
  3  /

insert into flight(time)
values (interval '24:30' hour to minute)

ORA-01873: the leading precision of the interval is too small
SQL> insert into flight(time)
  2  values (interval '1 10:30:30' day to second)
  3  /

insert into flight(time)
values (interval '1 10:30:30' day to second)

ORA-01873: the leading precision of the interval is too small
SQL> select *
  2  from flight
  3  /

TIME
-------------------
+0 10:30:00
+0 23:30:00

SQL> 
揪着可爱 2024-12-27 03:00:19

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')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文