我有一个交易表,我的问题是跟踪transactionDateTime
列。使用SQL,我似乎无法使DATE
或TIMESTAMP
格式化,以便任何插入的数据必须在任何给定日期的上午8点到晚上10点之间:
CREATE TABLE Transaction_T
(transactionID NUMBER(8,0) ,
employeeID NUMBER(10,0) NOT NULL,
customerID NUMBER(6,0) NOT NULL,
transactionDate VARCHAR2(9) NOT NULL,
transactionPayment VARCHAR2(10) check(transactionPayment IN('cash','credit','debit','ebt')),
transactionType VARCHAR2(10) check(transactionType IN('void','return','sale')),
transactionDateTime TIMESTAMP check(transactionDateTime < '% 22:00:00' AND transactionDateTime > '% 08:00:00') NOT NULL,
CONSTRAINT TransactionPK PRIMARY KEY (transactionID),
CONSTRAINT TransactionFK1 FOREIGN KEY (employeeID) REFERENCES Employee_T(employeeID),
CONSTRAINT TranscationFK2 FOREIGN KEY (customerID) REFERENCES Customer_T(customerID));
上面的表已创建,但在尝试插入正确的Date(Time)
时,我会收到有关1到12小时的格式错误,或者当时间低于12时的有效月份。我使用过Oracle文档格式,但仍然没有运气:
INSERT INTO
transaction_T(transactionID, employeeID, customerID, transactionDate, transactionPayment, transactionType, transactionDateTime)
VALUES(00000001, 0001, 000004, '04-APR-13','ebt','sale','04-APR-13 13:25:10');
答案 0 :(得分:8)
检查约束中的%
在这里不起作用。试试这个:
CHECK (
EXTRACT(HOUR FROM transactionDateTime) < 22 AND
EXTRACT(HOUR FROM transactionDateTime) >= 8)
答案 1 :(得分:1)
您可以像这样表达您的支票限制:
check(transactionDateTime between trunc(transactionDateTime) + 8/24 and trunc(transactionDateTime) + 22/24))