设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql无法删掉表中的主键

发布时间:2022-07-03 12:38 所属栏目:115 来源:互联网
导读:mysql无法删除表中的主键: 1、mysql删除表中的主键时报错,如下代码所示: mysql alter table student drop primary key; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key 2、问题原
       mysql无法删除表中的主键:

  1、mysql删除表中的主键时报错,如下代码所示:
 
  mysql> alter table student drop primary key;
  ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
  2、问题原因:
 
  查看student表的类型时,发现主键列中有auto_increment(递增)类型选项。如要删除表中的主键,需要先删除auto_increment类型。代码如下所示:
 
  mysql> desc student;
  +-------+-------------+------+-----+---------+----------------+
  | Field | Type        | Null | Key | Default | Extra          |
  +-------+-------------+------+-----+---------+----------------+
  | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
  | name  | char(20)    | NO   | MUL | NULL    |                |
  | age   | tinyint(2)  | NO   | MUL | 0       |                |
  | dept  | varchar(16) | YES  |     | NULL    |                |
  +-------+-------------+------+-----+---------+----------------+
  4 rows in set (0.02 sec)
  3、删除student表中的主键列上面的auto_increment类型。代码如下所示:
 
  mysql> alter table student change id id int;
  Query OK, 0 rows affected (0.04 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  提示:alter table student change id id int;命令修改student表中列的类型,auto_increment类型自然就会被删除。
 
  4、查看student表的类型,发现auto_increment类型已被删除。代码如下所示:
 
  mysql> desc student;
  +-------+-------------+------+-----+---------+-------+
  | Field | Type        | Null | Key | Default | Extra |
  +-------+-------------+------+-----+---------+-------+
  | id    | int(11)     | NO   | PRI | 0       |       |
  | name  | char(20)    | NO   | MUL | NULL    |       |
  | age   | tinyint(2)  | NO   | MUL | 0       |       |
  | dept  | varchar(16) | YES  |     | NULL    |       |
  +-------+-------------+------+-----+---------+-------+
  4 rows in set (0.01 sec)
  5、删除student中的主键。代码如下所示:
 
  mysql> alter table student drop primary key;
  Query OK, 0 rows affected (0.10 sec)
  Records: 0  Duplicates: 0  Warnings: 0
  6、最后查看student表的类型,发现表中的主键已被删除。代码如下:
 
  mysql> desc student;
  +-------+-------------+------+-----+---------+-------+
  | Field | Type        | Null | Key | Default | Extra |
  +-------+-------------+------+-----+---------+-------+
  | id    | int(11)     | NO   |     | 0       |       |
  | name  | char(20)    | NO   | MUL | NULL    |       |
  | age   | tinyint(2)  | NO   | MUL | 0       |       |
  | dept  | varchar(16) | YES  |     | NULL    |       |
  +-------+-------------+------+-----+---------+-------+
  4 rows in set (0.00 sec)

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读