2016-03-04 63 views
0

我創建了一個簡單的表:檢查在約束時間戳在甲骨文

CREATE TABLE Messages 
( msgID number(10) PRIMARY KEY, 
    sender_ID number(10), 
    time_sent TIMESTAMP, 
); 

現在我想添加一個約束它,確保發送時間將在2014年後,我寫道:

alter table Messages 
add constraint year_check 
check (time_sent > to_timestamp('2014-12-31 23:59:59')); 

不過,我得到以下錯誤:

ORA-30075: TIME/TIMESTAMP WITH TIME ZONE literal must be specified in CHECK constraint

我不想在我的時間戳的時區和有像這樣插入的值:

INSERT INTO Messages VALUES(1, 1, TIMESTAMP '2014-12-24 07:15:57'); 

我該如何解決我的約束以使此錯誤消失?

回答

0

嘗試使用格式掩碼:

select to_timestamp('2014-12-31 23:59:59') timest from dual 
    2/
select to_timestamp('2014-12-31 23:59:59') timest from dual 
       * 
ERROR at line 1: 
ORA-01843: not a valid month 

select to_timestamp('2014-12-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS') timest from dual 
    2/

TIMEST 
--------------------------------------------------------------------------- 
31-DEC-14 11.59.59.000000000 PM 
+0

難道你讓很多更清晰,你想什麼我改變我的約束是什麼? – user3268401

+0

沒有遺憾,沒有工作 – user3268401

1

當你查找錯誤消息in the manual你會看到推薦:

Action: Use time or timestamp with time zone literals only.

to_timestamp('2014-12-31 23:59:59')返回timestamp(無時區),但甲骨文需要一個timezone with time zone在檢查約束(雖然我不得不承認我不明白爲什麼

您可以使用ANSI時間戳文字被解析爲timestamp with time zone

alter table Messages 
    add constraint year_check 
    check (time_sent > timestamp '2014-12-31 23:59:59'); 

或使用to_timestamp_tz有一個明確的時區:

alter table Messages 
    add constraint year_check 
    check (time_sent > to_timestamp_tz('2014-12-31 23:59:59 +00:00', 'YYYY-MM-DD HH:MI:SS TZH:TZM')); 

順便說一句:我寧願變化1月1日使用>=的條件:

alter table Messages 
    add constraint year_check 
    check (time_sent >= timestamp '2015-01-01 00:00:00'); 

否則,你可以添加一行2014-12-31 23:59:59.567

+0

這裏我理解你的邏輯,但我仍然得到錯誤'第2行添加約束year_check * ERROR: ORA-02293:無法驗證(USER.YEAR_CHECK) - 檢查約束違反 ' – user3268401

+0

@ user3268401,很可能你現有的數據在'2015-01-01'之前。 –

+0

@ user3268401:shannon是對的:您的現有數據根本無法驗證。你需要先解決這個問題。 –