删除 MySQL 中的主键
我有以下表架构,它将 user_customers 映射到实时 MySQL 数据库上的权限:
mysql> describe user_customer_permission;
+------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_customer_id | int(11) | NO | PRI | NULL | |
| permission_id | int(11) | NO | PRI | NULL | |
+------------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
我想删除 user_customer_id 和permission_id 的主键并保留 id 的主键。
当我运行命令时:
alter table user_customer_permission drop primary key;
我收到以下错误:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
如何删除列的主键?
I have the following table schema which maps user_customers to permissions on a live MySQL database:
mysql> describe user_customer_permission;
+------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_customer_id | int(11) | NO | PRI | NULL | |
| permission_id | int(11) | NO | PRI | NULL | |
+------------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
I would like to remove the primary keys for user_customer_id and permission_id and retain the primary key for id.
When I run the command:
alter table user_customer_permission drop primary key;
I get the following error:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
How can I drop a column's primary key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果没有索引,维护自动增量列就会变得过于昂贵,这就是为什么 MySQL 要求自动增量列位于索引的最左边部分。
您应该在删除键之前删除自动增量属性:
请注意,您有一个涵盖所有三列的复合
PRIMARY KEY
,并且不保证id
是唯一的。如果它恰好是唯一的,您可以将其再次设为
PRIMARY KEY
和AUTO_INCRMENT
:Without an index, maintaining an autoincrement column becomes too expensive, that's why
MySQL
requires an autoincrement column to be a leftmost part of an index.You should remove the autoincrement property before dropping the key:
Note that you have a composite
PRIMARY KEY
which covers all three columns andid
is not guaranteed to be unique.If it happens to be unique, you can make it to be a
PRIMARY KEY
andAUTO_INCREMENT
again:一行:
您也不会丢失自动增量,并且必须重新添加它,这可能会产生副作用。
One Line:
You will also not lose the auto-increment and have to re-add it which could have side-effects.
我相信@Quassnoi 已经回答了你的直接问题。
顺便说一句,也许这只是您的一些尴尬的措辞,但您似乎有这样的印象:您有三个主键,每个字段一个。
事实并非如此。根据定义,您只能有一个主键。这里有一个主键,它由三个字段组成。
因此,您不能“删除列上的主键”,您可以删除主键,也可以不删除主键。
如果您想要仅包含一列的主键,则可以删除 3 列上的现有主键并在 1 列上创建新主键。
I believe @Quassnoi has answered your direct question.
Just a side note, maybe this is just some awkward wording on your part, but you seem to be under the impression that you have three primary keys, one on each field.
This is not the case. By definition, you can only have one primary key. What you have here is a primary key that is a composite of three fields.
Thus, you cannot "drop the primary key on a column", you can drop the primary key, or not drop the primary key.
If you want a primary key that only includes one column, you can drop the existing primary key on 3 columns and create a new primary key on 1 column.
如果您有复合主键,请执行以下操作 -
ALTER TABLE table_name DROP PRIMARY KEY,ADD PRIMARY KEY (col_name1, col_name2);
In case you have composite primary key, do like this-
ALTER TABLE table_name DROP PRIMARY KEY,ADD PRIMARY KEY (col_name1, col_name2);
在列中添加主键。
从表中删除主键。
To add primary key in the column.
To remove primary key from the table.
“如果你恢复主键,你肯定可以将其恢复为AUTO_INCRMENT”
不应该有是否需要ID列的“恢复PK属性”和“恢复自动增量属性”的问题。
鉴于它是表的先前定义中的自动增量,很可能存在某些程序在不提供 ID 值的情况下插入到该表中(因为 ID 列无论如何都是自动增量的)。
任何此类程序的操作都将因不恢复自动增量属性而中断。
"if you restore the primary key, you sure may revert it back to AUTO_INCREMENT"
There should be no question of whether or not it is desirable to "restore the PK property" and "restore the autoincrement property" of the ID column.
Given that it WAS an autoincrement in the prior definition of the table, it is quite likely that there exists some program that inserts into this table without providing an ID value (because the ID column is autoincrement anyway).
Any such program's operation will break by not restoring the autoincrement property.
我遇到了同样的问题,除了表中的一些值之外。 更改了主键
尽管我使用
ALTER TABLE
user_customer_permissionDROP PRIMARY KEY , ADD PRIMARY KEY (
id)
我的服务器上仍然存在问题。我在表内创建了新字段,将值转移到新字段并删除了旧字段,问题解决了!
I had same problem and beside some values inside my table. Although I changed my Primary Key with
ALTER TABLE
user_customer_permissionDROP PRIMARY KEY , ADD PRIMARY KEY (
id)
problem continued on my server. I created new field inside the table I transfered the values into new field and deleted old one, problem solved!!
首先修改列以删除 auto_increment 字段,如下所示:
alter table user_customer_permission 修改列 id int;
接下来,删除主键。
更改表 user_customer_permission 删除主键;
First modify the column to remove the auto_increment field like this:
alter table user_customer_permission modify column id int;
Next, drop the primary key.
alter table user_customer_permission drop primary key;
首先备份数据库。然后删除与该表关联的任何外键。截断外键表。截断当前表。删除所需的主键。使用 sqlyog 或 workbench 或 heidisql 或 dbeaver 或 phpmyadmin。
First backup the database. Then drop any foreign key associated with the table. truncate the foreign key table.Truncate the current table. Remove the required primary keys. Use sqlyog or workbench or heidisql or dbeaver or phpmyadmin.
该列必须位于索引中。如果不是,请键入以下命令
ALTER TABLE;添加约束唯一();
那么你可以使用
ALTER TABLE;删除主键,
添加主键(id);
the column has to be in the Indexes. If it isn't type the following command
ALTER TABLE <your_table_name> ADD CONSTRAINT UNIQUE(<your_column_name>);
then you can use
ALTER TABLE <your_table_name> DROP PRIMARY KEY,
ADD PRIMARY KEY(id);
在 SQL 管理器中找到该表,右键单击它并选择设计,然后右键单击小钥匙图标并选择删除主键。
Find the table in SQL manager right click it and select design, then right click the little key icon and select remove primary key.