user1716672
user1716672

Reputation: 1073

Mysql throws an error when one table name is not surrounded by single quotes

I'm building a simple app which lists teams and matches. The Team and Match databases were built with the following scripts (I'm using PhpMyadmin):

CREATE TABLE IF NOT EXISTS `Team` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(120) NOT NULL,
  `screen_name` varchar(100) NOT NULL,
  `sport_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

CREATE TABLE IF NOT EXISTS `Match` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sport_id` int(11) NOT NULL,
  `team_one_id` int(11) NOT NULL,
  `team_two_id` int(11) NOT NULL,
  `venue` varchar(80) NOT NULL,
  `kick_off` datetime NOT NULL,
  PRIMARY KEY (`id`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

If i do:

SELECT * FROM Team

The script runs and I get an empty result. But, incredibly, if I do

SELECT * FROM Match

I get the following error:

1064 - 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 'Match' at line 1

Instead, I have to do:

SELECT * FROM `Match`

And it works. I have other tables in the database but this is the only behaving like this. Any ideas why?

Upvotes: 2

Views: 199

Answers (3)

Robert
Robert

Reputation: 25753

You need encapsulate it in quotes because Match is a keyword.

See key word list

Upvotes: 1

Robadob
Robadob

Reputation: 5349

match is a reserved word in SQL

Read more here: https://drupal.org/node/141051

Upvotes: 2

Bearcat9425
Bearcat9425

Reputation: 1600

Match is a Function in MySQL therefore you must put the quotes around it.

Upvotes: 2

Related Questions