Insert command generates error in MySQL

Posted on 2016-09-01
Last Modified: 2016-09-27
I was given the following code to create some tables in MySQL:

create table vt_staff (
    stf_id          decimal(6,0)   primary key
  , stf_name_last   varchar(25)    not null
  , stf_name_first  varchar(25)    not null
  , stf_job_title   varchar(25)    not null
  , constraint stf_id_range         check (stf_id > 0)
  , constraint job_title_values
         check ( stf_job_title in ('vet', 'vet assnt', 'clerical', 'kennel'))

create table vt_animals(
    an_id           decimal(6,0)  primary key
  , an_type         varchar(25)    not null  
  , an_name         varchar(25)    null
  , an_dob          date            not null
  , cl_id           decimal(6,0)     not null  
  ,  constraint an_id_range         check (an_id > 0)
  ,  constraint an_dob_ck           check (trunc(an_dob) = an_dob)

create table vt_exam_headers(
    ex_id           decimal(6,0)     primary key
  , an_id           decimal(6,0)     not null  
  , stf_id          decimal(6,0)     not null  
  , ex_date         date            not null
  , constraint vt_exam_headers_animal_fk  foreign key(an_id) references vt_animals
  , constraint vt_exam_headers_staff_fk   foreign key(stf_id) references vt_staff
  , constraint ex_id_range          check (ex_id > 0)
  , constraint exam_date_range      check (ex_date >= date '2010-01-01')

All the code executed fine. However when I tried running the following insert statements for the vt_exam_headers table, I keep obtaining an error, e.g..,

insert into vt_exam_headers(ex_id, an_id, stf_id, ex_date)
  values (2289, 21320, 38, TO_DATE('2015-04-11 13:00', 'YYYY-MM-DD HH24:MI') );

insert into vt_exam_headers(ex_id, an_id, stf_id, ex_date)
  values (2290, 21320, 38, TO_DATE('2015-04-11 17:00', 'YYYY-MM-DD HH24:MI') );
I believe the problem is related to the TO_DATE function, but am not sure of the correct syntax.

Would appreciate any help.
Question by:geeta_m9
  • 2
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41780450
What is the error message saying?

If it tells something about TO_DATE or ex_date, your assumption might be right.

Why not test this isolated? echo TO_DATE('2015-04-11 13:00', 'YYYY-MM-DD HH24:MI');
It is not a native php function, so it must be defined, included or required somewhere, before you can execute it, otherwise you get the error "Call to undefined function TO_DATE()", which happens to me. So the function is not defined.

Since you just created tables, you also have to add animals and staff first, before the exams an_id and stf_id can be populated with existing IDs. You have a foreign key constraint, that means whatever value you put into vt_exam_headers.an_id and vt_exam_headers.stf_id have to exist in the animals and staff tables, this is what is promised with this constraint. It means exams point to a related staff member and animal, any ID not existing in those table is just that, a number, worthless, meaningless, so this is about the referential integrity of data and prevents you to store meaningless data.

Bye, Olaf.

Author Comment

ID: 41780602
I have already inserted the records into the other tables. The error I obtain is, "FUNCTION TO_DATE does not exist." When I enter, echo TO_DATE('2015-04-11 13:00', 'YYYY-MM-DD HH24:MI');",  like you suggested, I obtain the error: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your server version for the right syntax..."
LVL 29

Accepted Solution

Olaf Doschke earned 500 total points
ID: 41781036
No, not echo within the query, echo as separate command.

Well, more important: "FUNCTION TO_DATE does not exist." What does this say? You miss a file defining that function. It is not part of the PHP language, but most probably a user defined function you miss in some other php file.

You may have ripped this out of another php file, which does the right include/require to be able to call TO_DATE. So, go back to where you got this from and look out for what other php files are included there, or do a search of all php files of the project to find the php file with this function declaration.

Bye, Olaf.

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL query to show different levels from a table. 5 69
MySQL Init Waits 25 100
php image upload 3 47
Insert values are dynamic 11 42
A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi ( had suggested a “sed” way, I actually shell …
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question