如何将表拆分为两个表并将记录与交叉ID相关联

时间:2013-04-23 09:29:01

标签: mysql sql database-design split

我需要一些帮助,将旧的客户数据库拆分为客户和地址

例如:

让我们调用旧表TB_old 以及2个新的new_customernew_address

TB_old 包含以下列:

cust_id, firstname, lastname, address, city, postalcode, phone, email, password

new_customers 得到了:

cust_id(new,A_I), firstname, lastname, phone, email, pass, address_id(link to new_address)

new_address 得到了:

address_id(new), address, city, postalcode, cust_id (link to new_customers)

我已经做过的事情:

INSERT INTO new_customers firstname, lastname, phone etc etc
SELECT TB_old.firstname TB_old.lastname etc etc from TB_old

我被困在哪里:

我坚持用new_address插入地址,同时用相对的cust_id更新地址,并用相对地址更新new_customer中的address_id。

编辑:额外的图像 说清楚:

2 个答案:

答案 0 :(得分:1)

第一件事:地址属于客户。考虑到这一点,你可以做到:

INSERT INTO new_customers 
    (cust_id, firstname, lastname, phone, email, password)
SELECT 
    (cust_id, firstname, lastname, phone, email, password)
FROM TB_old;


INSERT INTO new_address
    (address_id, cust_id, address, city, postalcode)
SELECT 
    (null, cust_id, address, city, postalcode)
FROM TB_old;

- )

答案 1 :(得分:0)

您需要链接旧密钥和新密钥。最简单的方法:将字段oldKey添加到第一个表,向其中加载数据(存储旧密钥),并使用旧密钥进行填充以填充第二个表。毕竟 - 删除oldKey列。