2012-10-06 36 views
1

我有三個表:年,員工,職位。假設我已經在這些表中有這些數據。如何複製數據庫記錄並跟蹤其新ID

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的方法。

有沒有一個簡單的方法來做到這一點?

非常感謝。

+1

您是否可以選擇更改表格結構?看起來你沒有正常形式,這可能是這裏複雜的一部分。 – andes

+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 
+0

該解決方案適用於一些小問題(我編輯了您的答案)。不過,我很好奇如何使桌子結構更好。你能否提供更好的桌子結構? –

+0

這很大程度上取決於您的應用程序和實際數據模型。我首先從員工表中刪除職位名稱和職位名稱,然後用year,position_id,employee_id引入新表。 – AndreKR

2

我想你應該閱讀有關database normalization。複製數據導致維護問題和錯誤的報告。

如果您採用了與以下不同的設計,那麼在員工更改職位,終止職位或停止職位之前,不會插入任何內容。還有很多其他方法可以解決這個問題,但您應該儘量減少冗餘(即每個員工只有一份副本),然後分別跟蹤隨時間變化的數據。在嘗試實現像這樣的事情之前,還請閱讀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 |    | 
-------------------------------------------------------- 
+0

哇,這是非常不同的觀點。我會考慮改變我的表格結構。謝謝安第斯山脈 –