MySql ERROR 1075 (42000) at line 1:...

ERROR 1075 (42000) at line 1: Incorrect table definition; there can be only one auto column and it must be defined as a key

说我创建表的语法有问题:
不正确的表定义;只能有一个自动列,它必须定义为一个主键。

错误:

create table ft_fruit
(
   id                   int not null auto_increment,
   themeid              int not null,
   name                 varchar(30) not null,
   setmealname          varchar(30),
   title                varchar(100) not null,
   `desc`               text,
   old_price            numeric(10,2) not null,
   price                numeric(10,2) not null,
   stock                int default 0 comment '默认0',
   count                int default 0 comment '默认0',
   state                char(1) not null default '0' comment '上架 0
            下架 1
            默认 0',
   isrecommend          char(1) not null default '0' comment '0 不推荐
            1 推荐

            默认0',
   ishot                char(1) comment '0 否
            1 是
            默认0,为1时,优先级比真实销量的高',
   isnew                char(1) default '0' comment '0 否
            1 是
            默认0',
   search_count         int not null default 0 comment '默认0',
   issetmeal            char(1) comment '0 否
            1 是
            默认0,为1时,商品价格将以《套餐表》的价格为准',
   creater              int not null,
   creatername          varchar(100),
   creatertime          datetime not null,
   modify               int,
   modifyname           varchar(100),
   modifytime           datetime,
   remark               varchar(100),
   extend1              text,
   extend2              text,
   extend3              text
);

alter table ft_fruit
   add primary key (id);

正确:

create table ft_fruit
(
   id                   int not null auto_increment,
   themeid              int not null,
   name                 varchar(30) not null,
   setmealname          varchar(30),
   title                varchar(100) not null,
   `desc`               text,
   old_price            numeric(10,2) not null,
   price                numeric(10,2) not null,
   stock                int default 0 comment '默认0',
   count                int default 0 comment '默认0',
   state                char(1) not null default '0' comment '上架 0
            下架 1
            默认 0',
   isrecommend          char(1) not null default '0' comment '0 不推荐
            1 推荐

            默认0',
   ishot                char(1) comment '0 否
            1 是
            默认0,为1时,优先级比真实销量的高',
   isnew                char(1) default '0' comment '0 否
            1 是
            默认0',
   search_count         int not null default 0 comment '默认0',
   issetmeal            char(1) comment '0 否
            1 是
            默认0,为1时,商品价格将以《套餐表》的价格为准',
   creater              int not null,
   creatername          varchar(100),
   creatertime          datetime not null,
   modify               int,
   modifyname           varchar(100),
   modifytime           datetime,
   remark               varchar(100),
   extend1              text,
   extend2              text,
   extend3              text,
   primary key (id)
);

我是用powerdesigner 16.5生成的,生成的版本是MySQL 5.0

而我使用的则是MySQL 5.7

我的理解:提示设置成自动增长的列,必须是主键,也就是说不能等创建表成功后,再去修改id为主键,应该是在创建表的时候就定义id为主键,才能使用自动增长关键字。
所以就有了以上语法的差别。

发布了229 篇原创文章 · 获赞 148 · 访问量 91万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章