Files
FullAutoWaterCheck/db/upgrade_normaltemperature_split_columns.sql
GukSang.Jin 6f2a810e00 优化
2026-02-04 09:58:56 +08:00

51 lines
1.8 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.
-- =====================================================
-- 数据库升级脚本normaltemperature表添加联络单号和件号字段
-- 创建时间2026-02-04
-- 说明将barcode字段拆分为ContactNumber和ItemNumber两个独立字段
-- =====================================================
USE fullautowaterpressure;
-- 1. 添加新字段
ALTER TABLE `normaltemperature`
ADD COLUMN `ContactNumber` VARCHAR(100) NULL COMMENT '联络单号' AFTER `barcode`,
ADD COLUMN `ItemNumber` VARCHAR(100) NULL COMMENT '件号' AFTER `ContactNumber`;
-- 2. 迁移现有数据假设barcode格式为"联络单号-件号"
-- 如果barcode包含"-",则拆分
UPDATE `normaltemperature`
SET
`ContactNumber` = SUBSTRING_INDEX(`barcode`, '-', 1),
`ItemNumber` = SUBSTRING_INDEX(`barcode`, '-', -1)
WHERE `barcode` IS NOT NULL AND `barcode` LIKE '%-%';
-- 如果barcode不包含"-",则全部作为联络单号
UPDATE `normaltemperature`
SET
`ContactNumber` = `barcode`,
`ItemNumber` = ''
WHERE `barcode` IS NOT NULL AND `barcode` NOT LIKE '%-%';
-- 3. 创建索引以提高查询性能
CREATE INDEX `idx_contact_number` ON `normaltemperature`(`ContactNumber`);
CREATE INDEX `idx_item_number` ON `normaltemperature`(`ItemNumber`);
-- 4. 验证数据迁移
SELECT
COUNT(*) as total_records,
COUNT(ContactNumber) as has_contact_number,
COUNT(ItemNumber) as has_item_number,
COUNT(CASE WHEN ContactNumber IS NOT NULL AND ItemNumber IS NOT NULL THEN 1 END) as both_fields
FROM normaltemperature;
-- 5. 显示示例数据
SELECT
Id, barcode, ContactNumber, ItemNumber, CreateTime
FROM normaltemperature
ORDER BY Id DESC
LIMIT 10;
-- =====================================================
-- 升级完成
-- =====================================================