在Oracle中存储时间

Flu*_*key 0 sql oracle oracle10g

让我快速解释一下.我有一个管理访客的应用程序.

我们有一个访问的开始日期(访问只能是一天).我们可以添加一个时间,例如旅行时间,小组午餐时间,小组进行演示的时间等.所有时间都将采取访问的开始日期,然后相应地追加时间.

简单代码:

   // set tour time in tour table
    $y->setTourTime($visit->getVisitDate("Y-m-d") . $tourTime);
    // can have many presentation
    $p->setPresentationTime($visit->getVisitDate("Y-m-d") . $tourTime);
Run Code Online (Sandbox Code Playgroud)

所以我在多个表中有很多时间戳.但问题是,如果我决定更改访问实体的开始日期,那么我将不得不更改相关表格中的所有时间戳(导游,讲座等).这很难看

我更喜欢的是访问日期,例如访问表中的2010-10-10.然后在讲座表,指南表等中只存储时间而不是日期.你会怎么做?只是将它存储为一个字符串,即"10:00"?

感谢您的任何意见.:-)

Rob*_*ijk 6

你有几种可能性.我更喜欢拥有最适合这项工作的数据类型.使用DATE,TIMESTAMP和INTERVAL最好用时间计算,所以我会使用这样的东西:

SQL> create table visits
  2  ( startdate              date
  3  , starttime_tour         interval day(0) to second(0)
  4  , starttime_lunch        interval day(0) to second(0)
  5  , starttime_presentation interval day(0) to second(0)
  6  , constraint visits_ck1 check (startdate = trunc(startdate))
  7  )
  8  /

Table created.
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关INTERVAL DAY TO SECOND数据类型的更多信息:http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements001.htm#SQLRF00207

以下是您插入和选择的示例:

SQL> insert into visits
  2  values
  3  ( trunc(sysdate)
  4  , to_dsinterval('0 09:00:00')
  5  , to_dsinterval('0 12:00:00')
  6  , to_dsinterval('0 13:00:00')
  7  )
  8  /

1 row created.

SQL> select startdate
  2       , starttime_tour
  3       , starttime_lunch
  4       , starttime_presentation
  5    from visits
  6  /

STARTDATE           STARTTIME_TOUR       STARTTIME_LUNCH      STARTTIME_PRESENTATI
------------------- -------------------- -------------------- --------------------
17-12-2010 00:00:00 +0 09:00:00          +0 12:00:00          +0 13:00:00

1 row selected.
Run Code Online (Sandbox Code Playgroud)

现在计算非常简单:

SQL> select startdate
  2       , startdate + starttime_tour as tour
  3       , startdate + starttime_lunch as lunch
  4       , startdate + starttime_presentation as presentation
  5    from visits
  6  /

STARTDATE           TOUR                LUNCH               PRESENTATION
------------------- ------------------- ------------------- -------------------
17-12-2010 00:00:00 17-12-2010 09:00:00 17-12-2010 12:00:00 17-12-2010 13:00:00

1 row selected.
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

问候,Rob.