Solved

Insert command generates error in MySQL

Posted on 2016-09-01
3
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
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.
0
 

Author Comment

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

Accepted Solution

by:
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.
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Suggested Solutions

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Creating and Managing Databases with phpMyAdmin in cPanel.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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