Ok. So the short of it is, I was trying to do an INSERT SELECT such as:
START TRANSACTION;
INSERT INTO dbNEW.entity (commonName, surname)
SELECT namefirst, namelast
FROM dbOLD.user;
SET @key = LAST_INSERT_ID();
INSERT INTO dbNEW.user (userID, entityID, other)
SELECT user_id, @key, other
FROM dbOLD.user;
COMMIT;
Of course @key does not return each subsequent LAST_INSERT_ID() from each insert but the ID from only the last insert.
Basically, I'm splitting an old USER Table into an ENTITY and USER like:
dbOLD.user
+-------------+---------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+------------+----------------+
| user_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| namefirst | varchar(20) | NO | | | |
| namelast | varchar(20) | NO | | | |
| other | varchar(10) | NO | | | |
+-------------+---------------------+------+-----+------------+----------------+
dbNEW.user
+-------------+---------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+------------+----------------+
| userID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| entityID | int(10) unsigned | NO | MUL | 0 | |
| other | varchar(10) | NO | | | |
+-------------+---------------------+------+-----+------------+----------------+
dbNEW.entity
+--------------+---------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+------------+----------------+
| entityID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| commonName | varchar(20) | NO | | | |
| surname | varchar(20) | NO | | | |
+--------------+---------------------+------+-----+------------+----------------+
Why would I want to do this? Basically, I have a "STORE" entity which is going to have fields common to "USERS" such as address and phone number. So any "ENTITY" might have none to multiple addresses (shipping, billing, mailing) and none to multiple phone numbers (fax, main, billing, cell, home) There may be other ways to accomplish this, but this is the solution I ended up with.
The STOREs and USERS from the old db need to keep their old PKs and gain an additional ENTITY fk. How can I do this without making a dump and manually editing it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…