Solved

Insert command generates error in MySQL

Posted on 2016-09-01
3
40 Views
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.
0
Comment
Question by:geeta_m9
  • 2
3 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
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.
0
 

Author Comment

by:geeta_m9
Comment Utility
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..."
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
Comment Utility
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

15 Experts available now in Live!

Get 1:1 Help Now