Files
FullAutoWaterCheck/db/upgrade_scandata_split_columns.sql
2026-02-04 09:46:06 +08:00

56 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
数据库升级脚本 - 分离联络单号和件号
执行日期: 2026-02-04
目的: 将barcode字段拆分为ContactNumber和ItemNumber两个独立字段
影响表: scandata
*/
USE fullautowaterpressure;
-- 1. 添加新字段
ALTER TABLE `scandata`
ADD COLUMN `ContactNumber` VARCHAR(100) NULL COMMENT '联络单号' AFTER `barcode`,
ADD COLUMN `ItemNumber` VARCHAR(100) NULL COMMENT '件号' AFTER `ContactNumber`;
-- 2. 迁移现有数据如果barcode包含"-"分隔符)
UPDATE `scandata`
SET
`ContactNumber` = SUBSTRING_INDEX(`barcode`, '-', 1),
`ItemNumber` = SUBSTRING_INDEX(`barcode`, '-', -1)
WHERE `barcode` IS NOT NULL AND `barcode` LIKE '%-%';
-- 3. 对于没有分隔符的数据将整个barcode作为联络单号
UPDATE `scandata`
SET
`ContactNumber` = `barcode`,
`ItemNumber` = ''
WHERE `barcode` IS NOT NULL AND `barcode` NOT LIKE '%-%';
-- 4. 添加索引以提高查询性能
CREATE INDEX `idx_contact_number` ON `scandata`(`ContactNumber`);
CREATE INDEX `idx_item_number` ON `scandata`(`ItemNumber`);
-- 5. 验证数据迁移
SELECT
COUNT(*) as total_records,
COUNT(ContactNumber) as has_contact_number,
COUNT(ItemNumber) as has_item_number
FROM `scandata`;
-- 6. 显示迁移后的示例数据
SELECT
Id,
barcode as '原条码',
ContactNumber as '联络单号',
ItemNumber as '件号',
CreateTime
FROM `scandata`
ORDER BY Id DESC
LIMIT 10;
-- 注意:
-- 1. barcode字段保留用于兼容性不删除
-- 2. 新数据将同时填充barcode、ContactNumber和ItemNumber
-- 3. 如果需要回滚,可以删除新增的两个字段