MySQL 如何在MySQL中删除主键?
要删除主键,首先使用ALTER来修改表。然后使用DROP删除主键,如下所示
阅读更多:MySQL 教程
语法
alter table yourTableName drop primary key;
Mysql
让我们首先创建一个表 –
mysql> create table DemoTable
-> (
-> StudentId int NOT NULL,
-> StudentName varchar(20),
-> StudentAge int,
-> primary key(StudentId)
-> );
Query OK, 0 rows affected (0.48 sec)
Mysql
以下是查询表描述的查询 –
mysql> desc DemoTable;
Mysql
这将产生以下输出 –
+-------------+-------------+------+-----+---------+-------+
| 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中删除主键的查询 –
mysql> alter table DemoTable drop primary key;
Query OK, 0 rows affected (1.70 sec)
Records: 0 Duplicates: 0 Warnings: 0
Mysql
让我们再次检查表描述 –
mysql> desc DemoTable;
Mysql
这将产生以下输出 –
+-------------+-------------+------+-----+---------+-------+
| 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)
Mysql