要删除主键,请首先使用ALTER更改表。这样,使用DROP删除键,如下所示
语法alter table yourTableName drop primary key;
让我们首先创建一个表-mysql> create table DemoTable
-> (
-> StudentId int NOT NULL,
-> StudentName varchar(20),
-> StudentAge int,
-> primary key(StudentId)
-> );
这是检查表说明的查询-mysql> desc DemoTable;+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| StudentId | int(11) | NO | PRI | NULL | |
| StudentName | varchar(20) | YES | | NULL | |
| StudentAge | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
以下是在MySQL中删除主键的查询-mysql> alter table DemoTable drop primary key;
Records: 0 Duplicates: 0 Warnings: 0
让我们再次检查表描述-mysql> desc DemoTable;
这将产生以下输出-+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| StudentId | int(11) | NO | | NULL | |
| StudentName | varchar(20) | YES | | NULL | |
| StudentAge | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)