首页 > 数据库技术 > 详细

mysql删除自增主键

时间:2021-09-08 18:57:52      阅读:35      评论:0      收藏:0      [点我收藏+]

在MySQL中删除主键需要两步.

(1)如果有auto_increment,先删除之;

(2)删除主键约束 primary key

1、alter table table1 modify id int(11);

mysql> desc table1;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int      | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
| age   | char(33) | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+

#在这里指定id的新类型为int,其他的如自增,自然是删掉了。。
mysql> alter table table1 modify id int;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc table1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int      | NO   | PRI | NULL    |       |
| name  | char(20) | NO   |     | NULL    |       |
| age   | char(33) | NO   |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

2、alter table t9 drop primary key;

#删除主键
mysql> alter table table1 drop primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc table1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int      | NO   |     | NULL    |       |
| name  | char(20) | NO   |     | NULL    |       |
| age   | char(33) | NO   |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql删除自增主键

原文:https://www.cnblogs.com/afei654138148/p/15241836.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!