4

这有效:

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date)
)

...而这:

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date)
)

...结果是:

错误代码:1075 - 表定义不正确;只能有一个自动列,并且必须将其定义为键

我添加了主键但仍然出现错误。

4

6 回答 6

7

引用错误 #45987,错误 1075 (42000):不正确的表定义

实际上,这不是服务器错误,只是 MyISAM(该手册页假定为默认设置)和其他地方记录的 InnoDB 存储引擎之间的区别:http: //dev.mysql.com/doc/refman/5.1/en/innodb-自动增量处理.html

MyISAM 不再是默认引擎;InnoDB 是。

另外:http ://bugs.mysql.com/bug.php?id=60104

结论

InnoDB 不支持 AUTO_INCREMENT 但没有为表定义主键,但 MyISAM 支持。选择您要使用的引擎(MyISAM 或 InnoDB),并相应地处理 CREATE TABLE 语句。

于 2011-03-19T15:59:38.587 回答
1

错误不是简洁地解释了这一点吗?AUTO_INCREMENT 列(如果有的话)必须是键列。

正如您所演示的,这样做可以解决问题。

于 2011-03-19T17:03:02.440 回答
1

您必须将 auto_increment 列定义为主键/键

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date),
  primary key (shout_id) -- primary key
)

或者

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date),
  key (shout_id) -- key
)

您还应该定义您的引擎类型 - 我会推荐 innodb。

很高兴看到您使用无符号整数数据类型而不指定愚蠢的可选显示宽度!

于 2011-03-19T16:18:32.437 回答
1

好吧,我不是 mysql 专家,但是

say_id INT UNSIGNED NOT NULL AUTO_INCREMENT

是一个自动增量,未定义为 Key....

鉴于我们在这项业务中遇到的许多不清楚或误导性的错误信息,我无法想象有更清晰的错误信息......

于 2011-03-19T15:57:41.080 回答
1

+1 使用无符号等.. 但你说......它不能为空......但默认情况下它的 0.. 对我来说 0 总是为空?和一个主键 auto_increment 永远不会为空。你的方向很好,但这样做:

create table shoutbox_shout (
    shout_id int unsigned auto_increment primary key,
    user_id int unsigned not null,
    shout_date datetime,
    message mediumtest not null
)engine=innodb;
于 2011-03-19T15:58:41.910 回答
1

@Brandon_R:我在使用 MySQL 5.0.67-community-nt 的命令行时遇到了同样的错误,但使用这样的代码是可行的——

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date),
  PRIMARY KEY (shout_id)
);
于 2011-03-19T16:00:25.097 回答