这个 mysql 查询有什么问题?

发布于 2024-10-24 18:53:20 字数 669 浏览 1 评论 0原文

这有效:

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 - 表定义不正确;只能有一个自动列,并且必须将其定义为键

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

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; there can be only one auto column and it must be defined as a key

I added primary key but still get error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

九八野马 2024-10-31 18:53:20

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

实际上,这不是服务器错误,只是 MyISAM(该手册页假定为默认值)和其他地方记录的 InnoDB 存储引擎之间的差异:http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html< /a>


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

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

结论

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

Quote Bug #45987, ERROR 1075 (42000): Incorrect table definition:

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 is no longer the default engine; InnoDB is.

Also: 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. Choose which engine (MyISAM or InnoDB) you want to use, and deal with the CREATE TABLE statement accordingly.

猫弦 2024-10-31 18:53:20

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

shout_id INT UNSIGNED NOT NULL AUTO_INCRMENT

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

考虑到我们在这个业务中遇到的许多不清楚或误导性的错误消息,我无法想象一个更清晰的错误消息错误信息 ....

Well I'm not a mysql expert, but

shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT

is an auto increment, not defined as Key....

Given the many unclear or misleading error messages we experience in this business, I can't imagine a clearer error message ....

攒一口袋星星 2024-10-31 18:53:20

+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;

+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 ? and a primary key thats auto_increment is NEVER null..
Your pretty in the good direction but do it this way:

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;
一刻暧昧 2024-10-31 18:53:20

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

@Brandon_R: I get the same error using command line of MySQL 5.0.67-community-nt, using the code like this works though --

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)
);
不再让梦枯萎 2024-10-31 18:53:20

您必须将 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。

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

you MUST define an auto_increment column as a primary key/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),
  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.

Nice to see you using unsigned integer datatypes without specifying silly optional display widths !!

不及他 2024-10-31 18:53:20

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

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

Doesn't the error explain this succinctly? The AUTO_INCREMENT column — if you have one — must be a key column.

As you demonstrate, making it so fixes the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文