Answer the question
In order to leave comments, you need to log in
How to remove primary key with auto increment?
Hello!
I'm trying to remove the PRIMARY KEY attribute from the table below!
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| doughnut_name | varchar(10) | YES | | NULL | |
| doughnut_type | varchar(6) | YES | | NULL | |
| id | int | NO | PRI | NULL | auto_increment |
+---------------+-------------+------+-----+---------+----------------+
ALTER TABLE `doughnut_list`
DROP PRIMARY KEY ;
id
but already without AUTO_INCREMENT
but withPRIMARY KEY
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| doughnut_name | varchar(10) | YES | | NULL | |
| doughnut_type | varchar(6) | YES | | NULL | |
| id | int | NO | PRI | NULL | |
+---------------+-------------+------+-----+---------+-------+
ALTER TABLE `doughnut_list` DROP PRIMARY KEY;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| doughnut_name | varchar(10) | YES | | NULL | |
| doughnut_type | varchar(6) | YES | | NULL | |
| id | int | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+
id
itself is not deleted, then why does the interpreter swear at AUTO_INCREMENT
? Well, this one would have been removed PRIMARY KEY
, but the column would have remained, vmylse id
? I don't understand this mechanics. it turns out that in order to delete a column that is PRIMARY KEY
and which has it, it is also AUTO_INCREMENT
necessary to delete it completely, and recreate it? Or is there still a way to do it in one command without recreating the column? And in general, what is the connection PRIMARY KEY
in AUTO_INCREMENT
this case? Why does this error occur, that they say it is impossible to drop primer ki while hanging on this column AUTO_INCREMENT
? The column is not actually deleted, well, let it be with this AUTO_INCREMENT
volume ...
Answer the question
In order to leave comments, you need to log in
If a field is declared as AUTO_INCREMENT, then it MUST be a primary key or its prefix. If you remove the PRIMARY KEY but NOT remove the AUTO_INCREMENT, the resulting structure will not meet this requirement - hence the error.
Therefore, in one ALTER TABLE, you should perform both operations - and remove the primary key, and the AUTO_INCREMENT attribute (for example, change to DEFAULT {value}). Or execute two ALTER TABLEs, but in reverse order - first remove the AUTO_INCREMENT attribute, and only then the primary key.
DEMO
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question