LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2010, 03:50 PM   #1
worm5252
Member
 
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX, OS X
Posts: 567

Rep: Reputation: 57
Question MySQL Syntax Error


Hey guys,
I am reading Sams Teach Yourself SQL in one hour a day. In this book they work with both Oracle and MySQL to teach you SQL. So I installed mysql on my box and I am creating the empty database they use in this book so I can follow along as I read. I have noticed a few typos in the book and now I have noticed some code errors too. I just don't know enough to fix em. I am working on creating the empty database and I am getting syntax errors when I create certain tables. I have checked both the printed version of the book and the electronic version (which differ slightly) and both give me the same synatax errors.

First is Creating the first table of the database. Here is the code given to me to enter.

The Electronic version
Quote:
create table checks
(‘ check’ integer not null,
payee varchar(20) not null,
amount decimal(6, 2) not null,
remarks varchar(20) not null) ;
The Printed Version
Quote:
create table checks
('check' integer not null,
payee varchar(20) not null,
amount decimal(6, 2) not null,
remarks varchar(20) not null);
In both cases I receive the following error
Code:
mysql> create table checks
    -> ('check' integer not null,
    -> payee varchar(20) not null,
    -> amount decimal(6,2) not null,
    -> remarks varchar(20) not null);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''check' integer not null,
payee varchar(20) not null,
amount decimal(6,2) not n' at line 2
In addition to that I also have another table giving me a similar synatax error.

Quote:
create table convert
(name varchar(1 5) not null,
testnum numeric(9) not null) ;
Any ideas how I can correct this error and create these tables?
 
Old 02-10-2010, 05:08 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I don't do sql too often but I'll give it a shot -

- I don't think the single quotes are supposed to be there in the first one
- the second one has a gap between the two digits - '(1 5)' try removing it

You may be able to prevent future issues by downloading errata from the book website.. I haven't checked to see if this book has one but most do.

<edit>You may find it helpful to check the reference manual - http://dev.mysql.com/doc/index.html</edit>

cheers

Last edited by kbp; 02-10-2010 at 05:10 PM.
 
Old 02-10-2010, 05:37 PM   #3
worm5252
Member
 
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX, OS X
Posts: 567

Original Poster
Rep: Reputation: 57
Code:
mysql> create table checks (check integer not null, payee varchar(20) not null, amount decimal(6,2) not null, remarks varchar(20) not null);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'integer not null, payee varchar(20) not null, amount decimal(6,2) not null, rema' at line 1
mysql> create table convert
    -> (name varchar(15) not null,
    -> testnum numeric(9) not null);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'convert
(name varchar(15) not null,
testnum numeric(9) not null)' at line 1
mysql>
I believe check is a mysql command thus the need for single quotes. Also if I remove the gap in the second problem I still have an issue. I just don't get it.
 
Old 02-10-2010, 06:04 PM   #4
Weird0ne
LQ Newbie
 
Registered: Nov 2009
Distribution: Slackware / Arch
Posts: 10

Rep: Reputation: 2
I believe if the field name is going to be the same name as a MySQL command it needs to be encapsulated in backticks `` not single quotes ''.
 
Old 02-10-2010, 09:00 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,414

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
Indeed, but its best not to use names like that in the first place.
convert is also a keyword (actually a fn) http://dev.mysql.com/doc/refman/5.0/...functions.html.
That's a really(!) crap book. Use the docs on the website or you'll never get anywhere...
http://dev.mysql.com/doc/refman/5.0/en/index.html
 
Old 02-10-2010, 11:12 PM   #6
worm5252
Member
 
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX, OS X
Posts: 567

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by Weird0ne View Post
I believe if the field name is going to be the same name as a MySQL command it needs to be encapsulated in backticks `` not single quotes ''.
That is exactly what the problem was. Thanks. I would have never figured that out because I am still learning.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] MySQL Error when cerating FK in tables, can't pinpoint the syntax error jason.rohde Programming 4 12-14-2009 12:50 AM
perl dbi:mysql shows error syntax error dbi:mysql ?? haxpak Linux - Server 4 02-27-2009 12:09 AM
php / MySQL: You have an error in your SQL syntax error FirstBorn Programming 6 12-31-2008 06:46 PM
MySQL Syntax Error? taylor_venable Programming 2 06-10-2007 09:39 AM
Can't get pass this mysql syntax error Neruocomp Programming 5 07-24-2005 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration