您可以使用ALTER命令删除主键和auto_increment。语法如下-
ALTER TABLE yourTableName DROP PRIMARY KEY,change yourColumnName yourColumnName data type;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table removePrimaryKey -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentFirstName varchar(20), -> StudentLastName varchar(20), -> PRIMARY KEY(StudentId) -> );
使用DESC命令检查表的描述。语法如下-
desc yourTableName;
使用DESC检查表'removePrimaryKey'的描述。查询如下-
mysql> desc removePrimaryKey;
以下是输出-
+------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+-------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentFirstName | varchar(20) | YES | | NULL | | | StudentLastName | varchar(20) | YES | | NULL | | +------------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
现在,查看上方的“键”和“额外”列。在“键”列中有PRI,在“额外”列中有auto_increment。您需要使用开头讨论的ALTER DROP命令将它们都删除-
查询如下-
mysql> alter table removePrimaryKey DROP PRIMARY KEY,change StudentId StudentId int; Records − 0 Duplicates − 0 Warnings − 0
我们已经成功删除了上面的主键和自动递增。现在您检查主键和auto_increment是否已从表中删除或是否不使用以下查询-
mysql> desc removePrimaryKey;
以下是输出-
+------------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+-------------+------+-----+---------+-------+ | StudentId | int(11) | YES | | NULL | | | StudentFirstName | varchar(20) | YES | | NULL | | | StudentLastName | varchar(20) | YES | | NULL | | +------------------+-------------+------+-----+---------+-------+ 3 rows in set (0.07 sec)