Go Premium for a chance to win a PS4. Enter to Win


Insert command generates error in MySQL

Posted on 2016-09-01
Medium Priority
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 30

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 30

Accepted Solution

Olaf Doschke earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

963 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