1

Does anybody know what is wrong in this MYSQL 5.0 syntax?

CREATE TABLE IF NOT EXISTS target (
  _id int(11) NOT NULL AUTO_INCREMENT,
  time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  map_id int(11) DEFAULT NULL,
  left int(11) DEFAULT NULL,
  top int(11) DEFAULT NULL,
  status tinyint(1) NOT NULL,
  temperature int(11) DEFAULT NULL,
  humidity float DEFAULT NULL,
  lum int(11) DEFAULT NULL,
  PRIMARY KEY (_id),
  FOREIGN KEY (map_id) REFERENCES map(id) ON DELETE CASCADE
)

I'll show you the error:

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 'left INTEGER DEFAULT NULL, top INTEGER DEFAULT NULL, status tinyint(1) NOT' at line 5

1

3 Answers 3

2

Because left is a MySQL 5.0 reserved word. Also, even though you can escape the field name, it's never a great idea to use reserved words in a table definition.

Sign up to request clarification or add additional context in comments.

Comments

2

you must write it like this:

CREATE TABLE IF NOT EXISTS target (
  _id int(11) NOT NULL AUTO_INCREMENT,
  time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  map_id int(11) DEFAULT NULL,
  `left` int(11) DEFAULT NULL,
  top int(11) DEFAULT NULL,
  status tinyint(1) NOT NULL,
  temperature int(11) DEFAULT NULL,
  humidity float DEFAULT NULL,
  lum int(11) DEFAULT NULL,
  PRIMARY KEY (_id),
  FOREIGN KEY (map_id) REFERENCES map(id) ON DELETE CASCADE
)

look at the `` (backticks) characters in left row !

Comments

0

You're using reserved words as field names. You can do that, but then you have to properly escape them, like this:

CREATE TABLE IF NOT EXISTS target (
  `_id` int(11) NOT NULL AUTO_INCREMENT,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `map_id` int(11) DEFAULT NULL,
  `left` int(11) DEFAULT NULL,
  `top` int(11) DEFAULT NULL,
  `status` tinyint(1) NOT NULL,
  `temperature` int(11) DEFAULT NULL,
  `humidity` decimal(13,2) DEFAULT NULL,
  `lum` int(11) DEFAULT NULL,
  PRIMARY KEY (_id),
  FOREIGN KEY (map_id) REFERENCES map(id) ON DELETE CASCADE
)

My advise would be to avoid reserved names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.