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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (…
Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now