51 lines
1.8 KiB
MySQL
51 lines
1.8 KiB
MySQL
|
|
-- =====================================================
|
|||
|
|
-- 数据库升级脚本: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;
|
|||
|
|
|
|||
|
|
-- =====================================================
|
|||
|
|
-- 升级完成
|
|||
|
|
-- =====================================================
|