如何复制数据库记录并跟踪其新ID

时间:2012-10-06 03:34:26

标签: php mysql sql database

我有三张桌子:年,员工,职位。假设我已经在这些表中包含这些数据。

years:
----------------
| id | name    |
----------------
|  1 | 2011    |
----------------

positions:
------------------------------
| id | name        | year_id |
------------------------------
|  1 | Director    |  1      |
|  2 | Manager     |  1      |
------------------------------

employees:
---------------------------------------------------------
| id | name                     | position_id | year_id |
---------------------------------------------------------
|  1 | Employee A (Director)    |  1          |  1      |
|  2 | Employee B (Manager)     |  2          |  1      |
---------------------------------------------------------

========================================

年表是一个中心点。 如果我插入新年纪录,我还必须复制与上一年相关的所有职位和员工

因此,如果我将2012年插入年份表,则数据应该是这样的:

years:
----------------
| id | name    |
----------------
|  1 | 2011    |
|  2 | 2012    |
----------------

positions:
------------------------------
| id | name        | year_id |
------------------------------
|  1 | Director    |  1      |
|  2 | Manager     |  1      |
|  3 | Director    |  2      |
|  4 | Manager     |  2      |
------------------------------

employees:
---------------------------------------------------------
| id | name                     | position_id | year_id |
---------------------------------------------------------
|  1 | Employee A (Director)    |  1          |  1      |
|  2 | Employee B (Manager)     |  2          |  1      |
|  3 | Employee A (Director)    |  3   (?)    |  2      |
|  4 | Employee B (Manager)     |  4   (?)    |  2      |
---------------------------------------------------------

注意 employees表的第三和第四行中的问号。

我使用这些查询来插入新年并复制所有相关职位和员工:

// Insert new year record
INSERT INTO years (name) VALUES (2012);

// Get last inserted year ID
$inserted_year_id = .......... // skipped

// Copy positions
INSERT INTO positions (name, year_id) SELECT name, $inserted_year_id AS last_year_id FROM positions WHERE year_id = 1;

// Copy employees
INSERT INTO employees (name, position_id, year_id) SELECT name, position_id, $inserted_year_id AS last_year_id FROM employees WHERE year_id = 1;

问题在于复制员工查询。我找不到获取或跟踪新ID位置的方法。

有没有一种简单的方法可以做到这一点?

非常感谢。

2 个答案:

答案 0 :(得分:2)

您的数据模型存在严重缺陷,可能需要彻底检查,但如果您坚持像您描述的那样复制数据,那么这应该可以解决问题:

// Copy employees
INSERT INTO employees (name, position_id, year_id)
SELECT name, new_positions.id, $inserted_year_id AS last_year_id
FROM employees
JOIN positions AS old_positions ON old_positions.id = employees.position_id
                                AND old_positions.year_id = employees.year_id
JOIN positions AS new_positions ON new_positions.name = old_positions.name
                                AND new_positions.year_id = $inserted_year_id
WHERE employees.year_id = 1

答案 1 :(得分:2)

我认为你应该阅读database normalization。复制数据会导致维护问题和错误报告。

如果您选择了与以下不同的设计,则在员工更改职位,终止职位或停止职位之前,不会插入任何内容。还有很多其他方法可以解决这个问题,但是您应该尽量减少冗余(即每个Employee只有一个副本),然后分别跟踪随时间变化的数据。在尝试实现类似的内容之前,请先阅读foreign keys

positions:
-- If you are keeping track of the years that each position is active, 
-- using dates provides simplicity.  Note: this design assumes that positions 
-- are never reactivated after being deactivated.
------------------------------------------------
| id | name        | DateActive | DateInactive |
------------------------------------------------
|  1 | Director    | 01/01/2011 |              |
|  2 | Manager     | 01/01/2011 |              |
------------------------------------------------

employees:
---------------------------------------------------------------
| id | name                     | DateHired  | DateTerminated |
---------------------------------------------------------------
|  1 | Employee A               | 01/01/2011 |                |
|  2 | Employee B               | 01/01/2011 |                |
|  3 | Employee C               | 01/01/2011 | 10/01/2012     |
---------------------------------------------------------------

EmployeePositionRelationships 
--If you are keeping track of time that each employee held a position
-- Employee A has been a Director since 1/1/2011
-- Employee B was a Manager from 1/1/2011 to 10/6/2012. Then they became a Director
-- Employee B was a Manager from 1/1/2011 to 10/1/2012. Then they were terminated
--------------------------------------------------------
EmployeeId | PositionId | DateStarted | DateEnded      |
--------------------------------------------------------
1          |  1          | 01/01/2011 |                |
2          |  2          | 01/01/2011 | 10/6/2012      |
3          |  2          | 01/01/2011 | 10/1/2012      |
2          |  1          | 10/6/2012  |                |
--------------------------------------------------------