如何更新复合主键的一部分?
我有一个 drupal 站点,它使用 CCK 内容字段来存储大部分数据。
我想手动更改一些数据以指向不同的节点版本。
UPDATE content_field_table SET vid = '1234' WHERE nid = '12' AND vid = '123';
问题是 content_field_table 的复合主键为
PRIMARY KEY (`vid`,`delta`)
因此,当我运行更新语句时,出现以下错误:
错误代码:1062 键“PRIMARY”的重复条目“52979-0”
如何根据需要更新视频?
I have a drupal site which uses CCK content fields to store a majority of its data.
I want to manually change some of the data to point to a different node version.
UPDATE content_field_table SET vid = '1234' WHERE nid = '12' AND vid = '123';
The problem is that content_field_table has a composite primary key of
PRIMARY KEY (`vid`,`delta`)
So that when I run the update statement, I get the following error:
Error Code: 1062
Duplicate entry '52979-0' for key 'PRIMARY'
How can I update the vid as needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主键必须是唯一的。因此,您无法将一条记录更改为与现有记录具有相同的 PK
例如,如果您从下面的 SQL 中获取任何记录,则无法完成您想要执行的更新。您需要先进行其他更改或删除记录或删除 PK
A Primary Key must be unique. So you cannot change one record to have the same PK as an existing record
For example if you get any records from the SQL below then the update you want to perform cannot be done. You'll either need to make some other change first or delete a record or remove the PK
不要更改您的主键。也尽可能不要使用复合主键。 MySQL(和其他数据库)以 PK 顺序将记录存储在页中。 MySQL 将在为新页分配空间并插入更多数据之前将每个页填满 15/16。
复合 PK 或更改 PK 的问题是碎片化。我不是指磁盘碎片,而是指索引碎片。当 PK 移动时,数据必须在磁盘上进行洗牌以保持正确的顺序。根据数据集,MySQL 可能必须移动许多物理行才能执行此操作。
更新你的PK也是如此。更改 PK 会更改磁盘上的顺序,并且需要移动许多行。如果可能,使用自动递增 PK,或者不使用 PK,让 MySQL 为您创建一个内部 PK。
您想要的是将新行附加到磁盘上的最后一个可用页,这样既快速又便宜。
Don't change your primary keys. Also don't use compound primary keys whenever possible. MySQL (and other databases) store the records in pages in PK order. MySQL will fill each page 15/16 full before allocating space for a new page, and inserting more data there.
The problem with compound PKs or change your PK is fragmentation. I don't mean disk fragmentation I mean index fragmentation. As the PK moves, the data must be shuffled around disk to keep it in the correct order. Depending on the data set MySQL may have to move many physical rows to do this.
The same goes for updating your PK. Changing the PK changes the order on disk and requires many rows to be moved. If possible use an auto incrementing PK, or use no PK and let MySQL create an internal PK for you.
What you want is for new rows to be appending to the last free page on disk which is quick and cheap.