【问题标题】:How to add time query in Oracle?如何在Oracle中添加时间查询?
【发布时间】:2018-04-08 08:32:55
【问题描述】:
CREATE TABLE Appointment

(

appointID INTEGER,

appoint_date DATE,

appoint_time  TIME,

appoint_type VARCHAR(5),

primary key (appointID)

);

INSERT INTO Appointment VALUES(1, '15-Apr-2017', '10:00', 'long');

INSERT INTO Appointment VALUES(2, '15-Apr-2017', '10:30', 'short');

INSERT INTO Appointment VALUES(3, '28-May-2017', '14:00', 'long');

INSERT INTO Appointment VALUES(4, '20-May-2017', '15:00', 'short');

INSERT INTO Appointment VALUES(5, '11-May-2017', '10:30', 'long');

INSERT INTO Appointment VALUES(6, '26-Jun-2017', '9:30', 'short');

INSERT INTO Appointment VALUES(7, '30-Jun-2017', '14:00', 'long');

INSERT INTO Appointment VALUES(8, '30-Jun-2017', '15:30', 'short');

INSERT INTO Appointment VALUES(9, '28-Apr-2017', '16:00', 'short');

INSERT INTO Appointment VALUES(10,'30-Apr-2017', '13:00', 'short');

当我尝试添加 TIME 时,我不断收到此错误:

Error starting at line : 24 in command -

CREATE TABLE Appointment(

appointID INTEGER,

appoint_date DATE,

appoint_time  TIME,

appoint_type VARCHAR(5),

primary key (appointID)

)

Error report -

ORA-00902: invalid datatype

00902. 00000 -  "invalid datatype"

*Cause:    
*Action:

我也在尝试添加我的 DOCTOR TABLE,但我不断收到错误报告

ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:**

create table Doctor

(

    appointID   INTEGER     not null,

    regnum  CHAR(6),   

    doc_name    VARCHAR(40),

    doc_gender  CHAR(1),

    qual    VARCHAR(80),   

    foreign key (appointID) references Appointment

    primary key (appointID, regnum)

);

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    这是我的建议:避免使用 CHAR 数据类型,除非它有意义(例如在 gender 中,就像你所做的那样),以及 VARCHAR >>> 使用 VARCHAR2 代替(个人,我从来没有用过CHAR,也从来没有用过VARCHAR)。

    DATE 数据类型同时包含日期和时间组件,因此使用它是安全的。

    构成主键约束的列不必指定NOT NULL 约束,因为无论如何主键都不允许空值。

    所以,这是一个工作示例:

    SQL> create table appointment
      2    (appointid    integer constraint pk_app primary key,
      3     appoint_date date,
      4     appoint_type varchar2(5)
      5    );
    
    Table created.
    
    SQL>
    SQL> insert into appointment values
      2    (1, to_date('15.04.2017 10:00', 'dd.mm.yyyy hh24:mi'), 'long');
    
    1 row created.
    
    SQL>
    SQL> create table doctor
      2    (appointid   integer constraint fk_doc_app references appointment (appointid),
      3     regnum      varchar2(6),
      4     doc_name    varchar2(40),
      5     doc_gender  char(1),
      6     qual        varchar2(80),
      7     --
      8     constraint pk_doc primary key (appointid, regnum)
      9    );
    
    Table created.
    
    SQL>
    

    【讨论】:

    • 好答案,请说明为什么 OP 也会收到错误消息。
    • 因为这行末尾缺少逗号:foreign key (appointID) references Appointment, @user75ponic(如果你问的是这个)。
    • OP 使用 TIME 作为 datatype 如果我没记错的话,Oracle 中不存在。
    • @user75ponic,Rehan 在他的回答中已经说过了;仍然存在,所以......是的,我同意你没有弄错:)
    【解决方案2】:

    没有称为'TIME'的数据类型,你可以使用时间戳:

    CREATE TABLE Appointment
    (
    appointID INT,
    appoint_date DATE,
    appoint_time timestamp ,
    appoint_type VARCHAR(5),
    primary key (appointID)
    );
    

    对于插入,您必须使用 TO_DATE 和 TO_TIMESTAMP 函数,因为您要插入的是字符串。:

    INSERT INTO Appointment VALUES(3, To_date('15-Apr-2017','DD-MON-YY'), TO_TIMESTAMP('10:00','HH24:MI'), 'long');

    http://sqlfiddle.com/#!4/59f5ef

    【讨论】:

    • 谢谢,当我尝试将数据插入约会表时,我现在收到无效月份错误。知道如何解决这个问题吗?谢谢你
    • 你能给出这个的putput吗:SELECT * FROM nls_session_parameters WHERE parameter = 'NLS_DATE_LANGUAGE'
    • 上面更新了答案。
    • 好答案@RehanAzher
    猜你喜欢
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2018-10-13
    • 2013-03-09
    相关资源
    最近更新 更多