简体   繁体   English

这个mysql查询有什么问题?

[英]What is wrong with this mysql query?

This works: 这有效:

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)
)

...while this: ......而这个:

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)
)

...results in: ...结果是:

Error Code: 1075 - Incorrect table definition; 错误代码:1075 - 表定义不正确; there can be only one auto column and it must be defined as a key 只能有一个自动列,必须将其定义为键

I added primary key but still get error. 我添加了主键但仍然出错。

Quote Bug #45987, ERROR 1075 (42000): Incorrect table definition : 引用Bug#45987,ERROR 1075(42000):表定义不正确

Actually, this is not a server bug, just a difference between MyISAM (assumed as default by that manual page) and InnoDB storage engine documented elsewhere: http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html 实际上,这不是服务器错误,只是MyISAM(假定为该手册页的默认值)和其他地方记录的InnoDB存储引擎之间的差异: http//dev.mysql.com/doc/refman/5.1/en/innodb-自动递增,handling.html

MyISAM is no longer the default engine; MyISAM不再是默认引擎; InnoDB is. InnoDB是。

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

Conclusion 结论

InnoDB doesn't support AUTO_INCREMENT but no primary key being defined for the table, but MyISAM does. InnoDB不支持AUTO_INCREMENT,但没有为表定义主键,但MyISAM确实如此。 Choose which engine (MyISAM or InnoDB) you want to use, and deal with the CREATE TABLE statement accordingly. 选择要使用的引擎(MyISAM或InnoDB),并相应地处理CREATE TABLE语句。

Well I'm not a mysql expert, but 好吧,我不是mysql专家,但是

shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT

is an auto increment, not defined as Key.... 是一个自动增量,未定义为Key ....

Given the many unclear or misleading error messages we experience in this business, I can't imagine a clearer error message .... 鉴于我们在此业务中遇到的许多不清楚或误导性的错误消息,我无法想象更清晰的错误消息....

+1 for using unsigned etc.. but you say... it can be not null... BUT by default its 0.. and for me is 0 always null ? +1使用无符号等..但你说...它可以不是空...但是默认情况下它的0 ..而对我来说0总是为空? and a primary key thats auto_increment is NEVER null.. Your pretty in the good direction but do it this way: 并且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;

@Brandon_R: I get the same error using command line of MySQL 5.0.67-community-nt, using the code like this works though -- @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)
);

you MUST define an auto_increment column as a primary key/key 你必须将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
)

or 要么

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
)

You should also define your engine type - i would recommend innodb. 您还应该定义您的引擎类型 - 我会推荐innodb。

Nice to see you using unsigned integer datatypes without specifying silly optional display widths !! 很高兴看到你使用无符号整数数据类型而没有指定愚蠢的可选显示宽度!

Doesn't the error explain this succinctly? 这个错误不能简洁地解释这个吗? The AUTO_INCREMENT column — if you have one — must be a key column. AUTO_INCREMENT列(如果有的话)必须是键列。

As you demonstrate, making it so fixes the problem. 正如您所展示的那样,这样做可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM