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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章