-- ---------------------------- -- 合同表结构更新脚本 -- 说明:此脚本用于将旧的合同表结构更新为新的结构 -- 请根据实际情况谨慎执行,建议先备份数据 -- ---------------------------- -- ---------------------------- -- 第一步:备份原有数据(可选,建议执行) -- ---------------------------- -- 创建备份表 -- create table biz_contract_backup as select * from biz_contract; -- ---------------------------- -- 第二步:删除旧字段 -- 注意:如果表中已有数据,删除字段会丢失数据,请确认是否需要迁移 -- ---------------------------- alter table biz_contract drop column if exists delivery_dept_id; alter table biz_contract drop column if exists sales_user_id; alter table biz_contract drop column if exists accounting_type; alter table biz_contract drop column if exists customer_name; alter table biz_contract drop column if exists signing_entity; alter table biz_contract drop column if exists target_profit_rate; alter table biz_contract drop column if exists business_tags; alter table biz_contract drop column if exists paid_amount; alter table biz_contract drop column if exists unpaid_amount; alter table biz_contract drop column if exists remark; -- ---------------------------- -- 第三步:添加新字段 -- ---------------------------- alter table biz_contract add column if not exists party_a_name varchar(200) not null comment '甲方名称' after contract_code; alter table biz_contract add column if not exists party_b_name varchar(200) not null comment '乙方名称' after party_a_name; -- ---------------------------- -- 第四步:修改字段注释 -- ---------------------------- alter table biz_contract modify column contract_type char(1) not null comment '合同类型(1人月合同 2实施合同 3销售合同)'; alter table biz_contract modify column start_date date not null comment '合同开始日期'; alter table biz_contract modify column end_date date not null comment '合同结束日期'; alter table biz_contract modify column contract_amount decimal(12,2) not null comment '合同金额'; alter table biz_contract modify column attachment_oss_id bigint(20) default null comment '附件ossId'; -- ---------------------------- -- 第五步:添加转包相关字段 -- ---------------------------- alter table biz_contract add column if not exists is_subcontract varchar(1) default '0' comment '是否转包(0否 1是)' after contract_amount; alter table biz_contract add column if not exists subcontract_from_id bigint(20) default null comment '转包来源合同ID' after is_subcontract; -- 添加外键约束(可选) -- alter table biz_contract add constraint fk_contract_subcontract_from foreign key (subcontract_from_id) references biz_contract(contract_id); -- ---------------------------- -- 说明 -- ---------------------------- -- 1. 如果表中已有数据,需要在执行前先为新字段 party_a_name 和 party_b_name 设置默认值 -- 或者先添加字段允许为空,然后更新数据,最后再改为 not null -- 2. 旧的字段被删除后无法恢复,请务必先备份 -- 3. contract_type 的值保持不变:1=人月合同,2=实施合同,新增 3=销售合同