【问题标题】:Error when trying to insert values into a table and not sure why尝试将值插入表时出错,但不确定原因
【发布时间】:2019-12-03 11:20:54
【问题描述】:

我正在使用 Oracle,我正在尝试将值插入一个名为 bug 的表中,但我不确定它的含义以及如何修复它。

我的错误如下:

从第 78 行开始的错误命令 - INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (00, 'Crash', '软件停止正常运行并退出', DATE '1980-03-20 09:26:50')

命令行错误:78 列:133 错误报告 - SQL 错误: ORA-01861: 文字与格式字符串不匹配 01861. 00000 - “文字与格式字符串不匹配”

*原因:输入中的文字必须与输入中的文字长度相同 格式字符串(前导空格除外)。如果 “FX”修饰符已打开,文字必须完全匹配, 没有多余的空格。

*操作:更正格式字符串以匹配文字。

任何帮助将不胜感激。

到目前为止,我的代码如下所示:

--CREATE SCRIPTS
/*put your create scripts here – your script should not commented out*/

CREATE TABLE Project
(
    Proj_ID integer,
    Proj_Name varchar(10),
    Proj_Start_Date date,
    primary key (Proj_ID)
);

CREATE TABLE Bug
(
    Bug_ID integer,
    Bug_Type varchar(40),
    Bug_Desc varchar(20),
    Bug_Time date,
    primary key(Bug_ID)
);

CREATE TABLE Engineer
(
    Engineer_ID integer,
    Engineer_Name varchar(10),
    Engineer_Type varchar(20),
    primary key (Engineer_ID)
);

CREATE TABLE Bug_Project
(
    Bug_ID integer,
    Proj_ID integer,
    primary key(Bug_ID, Proj_ID),
    foreign key(Bug_ID) references Bug (Bug_ID),
    foreign key(Proj_ID) references  Project (Proj_ID)
);

CREATE TABLE Fix_Allocation
(
    Engineer_ID integer,
    Bug_ID integer,
    primary key(Engineer_ID, Bug_ID),
    foreign key(Engineer_ID) references Engineer (Engineer_ID),
    foreign key(Bug_ID) references Bug (Bug_ID)
);

CREATE TABLE Test_Allocation
(
    Engineer_ID integer,
    Bug_ID integer,
    primary key(Engineer_ID, Bug_ID),
    foreign key(Engineer_ID) references Engineer (Engineer_ID),
    foreign key(Bug_ID) references Bug (Bug_ID)
);

CREATE TABLE Note
(
    Engineer_ID integer,
    Bug_ID integer,
    Note_author varchar(10),
    Note_contents varchar(20),
    primary key(Engineer_ID, Bug_ID),
    foreign key(Engineer_ID) references Engineer (Engineer_ID),
    foreign key(Bug_ID) references Bug (Bug_ID)
);

COMMIT;
--INSERT SCRIPTS
/*put your insert scripts here – your script should not commented out */

INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (00, 'Project 1', DATE '1980-02-14');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (01, 'Project 2', DATE '1985-12-11');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (02, 'Project 3', DATE '1992-06-03');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (03, 'Project 4', DATE '2000-07-22');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (04, 'Project 5', DATE '2012-03-19');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (05, 'Project 6', DATE '2015-10-21');

INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (00, 'Crash', 'Software stopped functioning properly and exited', DATE '1980-03-20 09:26:50');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (01, 'Run Time Error', 'Wrong output due to a logical error', DATE '1982-06-31 11:36:32');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (02, 'Compilation Error', 'Problems with the compiler, failed complication of source code', DATE '1987-07-12 14:11:15');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (03, 'Crash', 'Software stopped functioning properly and exited', DATE '1993-01-31 03:21:17');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (04, 'Logical Error', 'Unexpected behavior due to problem in source code', DATE '1997-04-01 10:46:18');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (05, 'Run Time Error', 'Wrong output due to a logical error', DATE '2001-12-24 12:12:37');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (06, 'GUI Error', 'Glitchy interface ', DATE '2005-09-02 17:11:55');

INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (00, 'Ava', 'Tester');
INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (01, 'Alexander', 'Fixer');
INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (02, 'Aiden', 'Fixer');
INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (03, 'Anthony', 'Tester');
INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (04, 'Adam', 'Fixer');
INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (05, 'Alex', 'Tester');
INSERT INTO Engineer(Engineer_ID, Engineer_Name, Engineer_Type) VALUES (06, 'John', 'Fixer');

INSERT INTO Bug_Project VALUES ();
INSERT INTO Bug_Project VALUES ();
INSERT INTO Bug_Project VALUES ();

INSERT INTO Fix_Allocation VALUES ();
INSERT INTO Fix_Allocation VALUES ();
INSERT INTO Fix_Allocation VALUES ();

INSERT INTO Test_Allocation VALUES ();
INSERT INTO Test_Allocation VALUES ();
INSERT INTO Test_Allocation VALUES ();

INSERT INTO Note VALUES ();
INSERT INTO Note VALUES ();
INSERT INTO Note VALUES ();

COMMIT;
--SELECT SCRIPT
/*put your select scripts here (with indication of which query is answered) – your script should not commented out

-- Query 1:  List of all the bugs, and their details.
SELECT * FROM Bug;

-- Query 2: List of all bugs, and their notes.

-- Query 3: List of all bugs, with their notes, and the engineers who have written them; sorted by name of engineer.

-- Query 4: List the bugs and how much cumulative time (in hours) they have taken; ordered by time taken.

-- Query 5: The bug that has taken most time to fix, and the projects it is connected to.

COMMIT;
--DROP SCRIPT
/*put your drop scripts here (in the correct order)– your script should not commented out

DROP TABLE Note;
DROP TABLE Test_Allocation;
DROP TABLE Fix_Allocation;
DROP TABLE Engineer;
DROP TABLE Bug_Project;
DROP TABLE Bug;
DROP TABLE Project;

COMMIT;

编辑:

INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (01, 'Run Time Error', 'Wrong output due to a logical error', To_DATE ('1982-06-31 11:36:32', 'YYYY-MM-DD HH24:MO:SS'));

这是我收到的错误消息:

从第 79 行开始的错误命令 - INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (01, '运行时错误', '由于逻辑错误导致错误输出', To_DATE ('1982-06-31 11:36:32', 'YYYY- MM-DD HH24:MO:SS')) 错误报告 - ORA-01821: 日期格式无法识别

【问题讨论】:

  • 分钟的格式掩码是MI,因此您的to_date() 应该具有yyyy-mm-dd-hh24:mi:ss 的格式掩码(大写或小写都没有关系)。但是,您还有另一个问题:6 月 31 日不存在(6 月只有 30 天)。
  • 是的,我意识到我的错误哈​​哈,谢谢

标签: sql oracle oracle11g


【解决方案1】:

日期函数只能采用'YYYY-MM-DD'格式。

DATE '1982-06-31 11:36:32' 错误。

您可以使用 to_date 如下。

To_DATE ('1982-06-31 11:36:32,'YYYY-MM-DD HH24:MI:SS') 
-- hour in 24 hour format as can be seen in other dates in your example.
-- if your date contains AM or PM then you can use, To_DATE ('1982-06-31 11:36:32 AM' ,'YYYY-MM-DD HH:MI:SS AM') 

干杯!!

【讨论】:

  • 我试图实现这一点,但它给了我一个错误,仍然说数据类型无法识别。我会在上面添加错误和代码
  • 实际错误是什么?哪个语句给了你这个错误?最后 12 个插入语句也是完全无效的。而数字00就是0,01就是1。数字前面不需要0。
  • 是的,我知道,但你是什么意思无效,你在说哪些?
  • 在您的问题中,提交前的 12 个插入语句无效
【解决方案2】:

如果您有时间组件,请使用 TIMESTAMP 文字而不是 DATE 文字:

TIMESTAMP '2005-09-02 17:11:55'

您可以将此值插入DATE

【讨论】:

  • 我试过了,它摆脱了错误,但在输出的表格中它只有日期,没有时间。
  • @Dillon。 . .有一段时间,你只是看不到它。要查看时间组件,您需要将列格式化为字符串或时间戳。
猜你喜欢
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多