mysql删除自增主键

在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删除自增主键

上一篇:http 重定向到 https


下一篇:Python实现二叉树按层级遍历