1

有人知道这个 MYSQL 5.0 语法有什么问题吗?

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
)

我会告诉你错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 5 行的“left INTEGER DEFAULT NULL, top INTEGER DEFAULT NULL, status tinyint(1) NOT”附近使用正确的语法

4

3 回答 3

2

因为leftMySQL 5.0 的保留字。此外,即使您可以转义字段名称,在表定义中使用保留字也不是一个好主意。

于 2011-12-29T17:32:38.857 回答
2

你必须这样写:

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
)

看看左行中的 ``(反引号)字符!

于 2011-12-29T17:34:45.723 回答
0

您使用保留字作为字段名称。你可以这样做,但是你必须正确地逃避它们,就像这样:

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
)

我的建议是避免保留名称。

于 2011-12-29T17:35:16.347 回答