Hongmu Notes
Home Language Notes How can the data table rearrange the primary key from 1 without affecting other contents?
Language Notes mySql Summary of pitfalls

How can the data table rearrange the primary key from 1 without affecting other contents?

How can the data table rearrange the primary key from 1 without affecting other contents?

After spending the entire morning conducting research, we finally found a solution.

One primary table and one secondary table – you want to rearrange the primary keys for both tables in a single operation.

Originally, we planned to use PHP for looping and processing the data, but it turned out to be ridiculously slow—so we simply switched to using SQL code for the processing!

SQLcode:

 隐藏内容:评论后查看
#创建同结构的item_instance_new
CREATE TABLE item_instance_new LIKE item_instance;
#设置自增ID:
ALTER TABLE `item_instance_new` CHANGE `guid` `guid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;
#创建一个备份字段
ALTER TABLE `item_instance_new` ADD `guid_bak_id` INT(10) NOT NULL AFTER `guid`;
#将旧表字段移动到新表
INSERT INTO item_instance_new (guid_bak_id, itemEntry, owner_guid, creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, reforgeID, transmogrifyId, upgradeID, durability, playedTime, text, pet_species, pet_breed, pet_quality, pet_level, isbot, money, aid)
SELECT guid, itemEntry, owner_guid, creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, reforgeID, transmogrifyId, upgradeID, durability, playedTime, text, pet_species, pet_breed, pet_quality, pet_level, isbot, money, aid
FROM item_instance;
#取消自增ID
ALTER TABLE `item_instance_new` CHANGE `guid` `guid` INT(10) UNSIGNED NOT NULL DEFAULT '0';

#创建同结构的 character_inventory_new
CREATE TABLE character_inventory_new LIKE character_inventory;
#设置自增
ALTER TABLE `character_inventory_new` CHANGE `item` `item` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Item Global Unique Identifier';
#创建一个备份字段
ALTER TABLE `character_inventory_new` ADD `item_bak_id` INT(10) NOT NULL AFTER `item`;
#条件判断后进行迁移
INSERT INTO character_inventory_new (bag, slot, guid, item, item_bak_id)
SELECT t1.bag, t1.slot, t1.guid, t3.guid, t1.item
FROM character_inventory t1
JOIN item_instance_new t3 ON t1.item = t3.guid_bak_id
WHERE t1.item = t3.guid_bak_id;
#查找多余的ID数据
SELECT * FROM character_inventory
WHERE item NOT IN (SELECT item_bak_id FROM character_inventory_new);
#多余是数据进行重新排列
INSERT INTO character_inventory_new (bag, slot, guid, item_bak_id)
SELECT bag, slot, guid, item FROM character_inventory WHERE item NOT IN (SELECT item_bak_id FROM character_inventory_new);
#取消自增ID并还原设置
ALTER TABLE `character_inventory_new` CHANGE `item` `item` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Item Global Unique Identifier';

Code Detailed Explanation

This code is a database migration script designed to create new tables and migrate data from old tables to the new ones. The following explains each step:

  1. Create an `item_instance_new` table with the same structure: ThroughCREATE TABLE item_instance_new LIKE item_instance;The statement creates a new table, item_instance_new, with the same structure as the item_instance table.

  2. Set auto-increment ID: OKALTER TABLE item_instance_newCHANGEguid guid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;Set the `guid` field in the `item_instance_new` table to an auto-incrementing ID; this means that when data is inserted, this field will automatically generate a unique ID.

  3. Create a backup field: OKALTER TABLE item_instance_newADDguid_bak_idINT(10) NOT NULL AFTERguid;The statement adds a field named `guid_bak_id` to the `item_instance_new` table, used to back up the `guid` field from the old table.

  4. Move fields from the old table to the new table: OKINSERT INTO item_instance_new (guid_bak_id, itemEntry, owner_guid, creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, reforgeID, transmogrifyId, upgradeID, durability, playedTime, text, pet_species, pet_breed, pet_quality, pet_level, isbot, money, aid) SELECT guid, itemEntry, owner_guid, creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, reforgeID, transmogrifyId, upgradeID, durability, playedTime, text, pet_species, pet_breed, pet_quality, pet_level, isbot, money, aid FROM item_instance;This statement inserts data from the `item_instance` table into the `item_instance_new` table, while simultaneously populating the `guid_bak_id` field with the value from the `guid` field.

  5. Cancel auto-incrementing ID: OKALTER TABLE item_instance_newCHANGEguid guid INT(10) UNSIGNED NOT NULL DEFAULT '0';The statement removes the auto-increment attribute from the `guid` field in the `item_instance_new` table and sets its default value to 0.

  6. Create a `character_inventory_new` table with the same structure: OKCREATE TABLE character_inventory_new LIKE character_inventory;The statement creates a new table named `character_inventory_new` with the same structure as the `character_inventory` table.

  7. Set Auto-Increment: OKALTER TABLE character_inventory_newCHANGEitem item INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Item Global Unique Identifier';Set the `item` field in the `character_inventory_new` table to an auto-incrementing ID, and add an explanatory comment.

  8. Perform migration after conditional judgment: PassINSERT INTO character_inventory_new (bag, slot, guid, item) SELECT t1.bag, t1.slot, t1.guid, t3.guid FROM character_inventory t1 JOIN item_instance_new t3 ON t1.item = t3.guid_bak_id WHERE t1.item = t3.guid_bak_id;This statement migrates data from the `character_inventory` table that meets the specified conditions to the `character_inventory_new` table. The specific condition is that `t1.item` (the `item` field in the `character_inventory` table) equals `t3.guid_bak_id` (the `guid_bak_id` field in the `item_instance_new` table).

  9. Disable auto-incrementing ID and restore settings: OKALTER TABLE character_inventory_newCHANGEitem item INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Item Global Unique Identifier';Remove the auto-increment attribute from the `item` field in the `character_inventory_new` table, set its default value to 0, and add an explanatory comment.

This code is designed to migrate data from the old tables `item_instance` and `character_inventory` to the new tables `item_instance_new` and `character_inventory_new`, and to configure certain fields in these new tables.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

sqlite uses PDO to execute SQL statements exec(), query()

sqlite uses PDO to execute SQL statements exec(), query() Language Notes mySql

In PHP scripts, executing SQL queries using PDO to interact with a database can be done through three different approaches; the choice of which method to use depends on the specific operation you intend to perform. 1. Using the PDO::exec() method: When executing queries such as INSERT, UPDATE, or DELETE that do not return a result set, use the exec() method on a PDO object to execute the query. Upon successful execution, this method returns the number of affected rows...
👁 323
Mysql efficiency improvement: limit this piece of shit

Mysql efficiency improvement: limit this piece of shit Language Notes mySql

In mysql, the efficiency of limit is not high, especially my millions of data, limit 100000, 50, so writing, the query is even slower, at first it was quite fast, and the server cup behind it was directly filled for me! I thought the server was hacked, so I looked for the problem. I looked for it for a few days, but it turned out to be this dog, which made me angry! So I inquired about the principle of limit, emmm, change! Must be changed ...
👁 155
SQL statement to truncate the database

SQL statement to truncate the database Language Notes mySql

The TRUNCATE command can be used to clear a table; however, if you need to clear an entire database, you must clear each table individually. Below is an example SQL statement for clearing an entire database: SET foreign_key_checks = 0; SELECT CONCAT('TRUNCATE TABLE `',table_name,'`;') FR...
👁 297

Recommended reading

(Adaptive Mobile Version) Responsive Foreign Language School Website Source Code – HTML5 Responsive University/Institutional Website Template (PBootCMS – 0740)

(Adaptive Mobile Version) Responsive Foreign Language School Website Source Code – HTML5 Responsive University/Institutional Website Template (PBootCMS – 0740) Practical Collection pbootcms Template

A responsive PbootCMS website template source code for foreign language schools and universities, supporting mobile access. The design style is tailored for international education institutions, making it ideal for foreign language universities and international schools to showcase their teaching environments and admissions information. This template helps educational institutions attract domestic and international students online. Template Demo | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 63
Wireless Digital Doorbell Website Template 0210

Wireless Digital Doorbell Website Template 0210 Practical Collection Yiyou template

An EyouCMS website template designed for the wireless digital doorbell industry. Its modern and practical design is ideal for showcasing digital doorbell products, wireless technologies, smart home applications, and brand identity. This template helps smart home enterprises showcase their products online and attract home-based customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 61
How does python package and run on Windows?

How does python package and run on Windows? Language Notes python

The most common way to package and run Python programs on Windows is to use PyInstaller. PyInstaller is a free and cross-platform Python application packaging tool, which can package Python code and its dependent libraries into an independent executable file, making it possible to run Python programs on systems without Python interpreters installed. The following is to make …
👁 300
Geothermal Water Diversion System Website Template 0875

Geothermal Water Diversion System Website Template 0875 Practical Collection Yiyou template

This EyouCMS template is ideal for companies specializing in geothermal water distribution systems. Its professional, tech-oriented design effectively showcases water distribution products, underfloor heating systems, and related engineering applications. It enables HVAC equipment manufacturers to showcase their products online and attract both commercial and residential clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Eyou...
👁 36
(PC + WAP) Marketing-oriented green furniture and office solutions – PbootCMS website template; Download office desk and chair website source code – 0104

(PC + WAP) Marketing-oriented green furniture and office solutions – PbootCMS website template; Download office desk and chair website source code – 0104 Practical Collection pbootcms Template

This PbootCMS template features a green-themed design, specifically tailored for marketing-oriented office furniture and eco-friendly furniture enterprises, and is compatible with both PC and WAP devices. Its modern, minimalist style effectively showcases the aesthetic appeal and practicality of products such as office desks, chairs, and screens. This template enables furniture businesses to showcase their product lines online, thereby enhancing their brand image and boosting sales performance. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 56
(Adaptive mobile version) HTML5 responsive corporate website template for toy wholesale and manufacturing companies (pbootCMS); Download toy and recreational facility website source code – 0741

(Adaptive mobile version) HTML5 responsive corporate website template for toy wholesale and manufacturing companies (pbootCMS); Download toy and recreational facility website source code – 0741 Practical Collection pbootcms Template

A responsive PbootCMS corporate website template designed for wholesale toy manufacturing businesses for children's play areas, compatible with both PC and WAP devices. Its lively, child-friendly design is ideal for showcasing playground facilities, toy products, and brand stories. This template helps children's play area operators or toy manufacturers attract family customers and distributors online. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: ww...
👁 43