我的 sql 查询出现问题。
create table rental
(
rental_id NUMBER(5) NOT NULL,
rental_date timestamp NOT NULL,
inventory_id NUMBER(5) NOT NULL,
customer_id NUMBER(5) NOT NULL,
return_date timestamp NOT NULL,
staff_id NUMBER(5) NOT NULL,
constraint rental_primary PRIMARY KEY (rental_id),
constraint rental_foreign FOREIGN KEY (inventory_id) REFERENCES
inventory(inventory_id),
constraint rental_foreign2 FOREIGN KEY (customer_id) REFERENCES
customer(customer_id),
constraint rental_foreign3 FOREIGN KEY (staff_id) REFERENCES staff(staff_id),
constraint rental_unique_rental_date unique (rental_date),
constraint rental_unique_inventory_id unique (inventory_id),
constraint rental_unique_customer_id unique (customer_id),
constraint rental_rental_date check(to_char(rental_date,'YYYY/MM/DD HH:MI:SS AM')
between '2005/01/01 00:00:01 AM' AND '12/31/2015 11:59:59 PM'),
constraint rental_return_date check(to_char(return_date,'YYYY/MM/DD HH:MI:SS AM')
between '2005/01/01 00:00:01 AM' AND '12/31/2015 11:59:59 PM')
);
当我尝试插入数据时, 它抛出此错误“ORA-01843:不是有效的月份”。 谁能建议我使用什么类型的数据类型来表示租金日期和归还日期来解决这种情况? 这是我尝试上传的示例数据
rental_date return_date
5/24/2011 10:53:30 PM 5/26/2011 10:04:30 PM
注意:我可以在我的 Excel 工作表中看到这种类型的格式,其中有我的数据,但是当我收到错误时
"ORA-01843: not a valid month
Row 145: 144, 2011-05-25 23:49:56 ,1689,357, 2011-06-01 21:41:56 ,2"
我在这里观察不同的时间格式。
谁能给我建议一个解决方案吗?
提前致谢。
最佳答案
在检查约束中,您指定 TO_CHAR() 并使用日期格式掩码 'YYYY/MM/DD HH:MI:SS AM'
。但是您为范围指定的值采用两种不同的格式,例如
'2005/01/01 00:00:01 AM' AND '12/31/2015 11:59:59 PM'
我认为使用日期是个好主意,因为字符串不会像您想象的那样进行比较。
constraint rental_rental_date check(rental_date)
between to_date('2005/01/01 00:00:01 AM', 'YYYY/MM/DD HH:MI:SS AM') and
to_date('2015/12/31 11:59:59 PM', 'YYYY/MM/DD HH:MI:SS AM')
但这可能不是您问题的根源。这可能是您加载过程中的转换,这就是为什么我们需要知道您如何加载数据。
关于oracle - ORA-01843: 月份无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13107900/