mysql数据库转移数据

换数据库
1.建立数据库和相关表
drop database if exists imm_frontend;
如果imm_frontend 存在删除,不存在忽略

create database imm_frontend default charset=utf8;
创建数据库 imm_frontend 默认编码utf8 可以保存 utf8一个汉字占3个字节

drop table if exists `imm_frontend`.` friendstore `;
如果imm_frontend库的friendstore表存在删除,不存在忽略

CREATE TABLE `imm_frontend`.`friendstore` (
`id` int(11) NOT NULL auto_increment,
`storeId` int(11) NOT NULL default '0',
`friendStoreId` int(11) NOT NULL default '0',
`orderKey` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在imm_frontend库中创建表friendstore,innodb引擎是使用外键,事务等功能

2.转移数据

######### friendstore ##########
增加friendstore表字段
## add a temporay column to identify which table the record immigrate from, 1 from brokerfriend, 2 from companybroker ##
alter table imm_frontend.friendstore add column friendType int(11);

把brokerfriend中brokerId ,friendBrokerId ,orderKey数据导入到friendstore中
## immigrate friendstore from brokerfriend ##
insert ignore into imm_frontend.friendstore (storeId, friendStoreId, orderKey, friendType) select f.brokerId, f.friendBrokerId, f.orderKey, 1 from frontend.brokerfriend f;

把companybroker中companyId, friendBrokerId, orderKey数据导入到friendstore中
## immigrate friendstore from companybroker ##
insert ignore into imm_frontend.friendstore (storeId, friendStoreId, orderKey,friendType) select f.companyId, f.friendBrokerId, orderKey, 2 from frontend.companybroker f;

更新storeId
## update storeId in friendstore, before this the storeId is actually the brokerId ##
update imm_frontend.friendstore friendstore, imm_frontend.store store, frontend.brokerperson p set friendstore.storeId=store.id where friendstore.storeId=p.id and p.userId=store.userId and friendstore.friendType=1;

## update storeId in friendstore, before this the storeId is actually the companyId ##
update imm_frontend.friendstore friendstore, imm_frontend.store store, frontend.brokercompany c set friendstore.storeId=store.id where friendstore.storeId=c.id and c.userId=store.userId and friendstore.friendType=2;

## update friendStoreId, before this the friendStoreId is actually the brokerId ##
update imm_frontend.friendstore friendstore, imm_frontend.store store, frontend.brokerperson p set friendstore.friendStoreId=store.id where friendstore.friendStoreId=p.id and p.userId=store.userId;

## drop temporay column friendType ##
alter table imm_frontend.friendstore drop column friendType;

######### friendstore end ##########

保存文件.sql 执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值