How to have unique primary key in two tables?

﹥>﹥吖頭↗ 提交于 2020-01-06 07:46:12

问题


I have two tables in my system EMPLOYEE and EMPLOYEE_FORECAST. Both have the same columns, entire structure is same. I have another archive table with same structure called EMPLOYEE_ARCHIVE table.

I need to put data from both tables to this archive table. Since records in EMPLOYEE and EMPLOYEE_FORECAST may have same primary key e.g. a record in EMPLOYEE will have a pk of say 100 and another record in EMPLOYEE_FORECAST may also have pk of 100 and this will definitely happen so when they are inserted into archive table I will have a duplicate primary key.

The problem is I will also have some relation table like employee_products, employee_forecast_products and also employee_archive_products. These tables will have emp_id and product_id. So with same emp_id I wont be able to figure out the exact employee.

So, is there any way to have a unique primary key both the EMPLOYEE and EMPLOYEE_FORECAST tables.


回答1:


So you cannot not use the PK column of EMPLOYEE table as a PK column of the archive table.

You can add a new PK column to the archive table.

If, for some reason, you want the EMPLOYEE table's PK column to be the PK in the archive table, then you could add a flag column to the archive table which would indicate from which table the record comes from. And you could have a composite PK in the archive table containing the original PK and the flag column. (In general, I discourage composite PK-s, so, even if you want to have this flag column, you could have an additional normal PK column in the archive as well.)




回答2:


To expand on my comment, set up you archive table as:

EMPLOYEE
EMPID   NUMBER PK
EMPNAME VARCHAR2(30)
...

EMPLOYEE_FORECAST
EMPID   NUMBER PK
EMPNAME VARCHAR2(30)
...

EMPLOYEE_ARCHIVE
ORIG_TABLE VARCHAR2(30) PK
EMPID      NUMBER       PK
EMPNAME    VARCHAR2(30)
...

Data in EMPLOYEE_ARCHIVE:

ORIG_TABLE        EMPID      EMPNAME
------------------------------------
EMPLOYEE          100        JO BLOGGS
EMPLOYEE_FORECAST 100        JO BLOGGS

As the archive table PK is across both the original table and empid columns it will remain unique for all your data.

Obviously this is just an example and you can use whatever derived column you want to enforce the uniqueness in your archive table.

Hope it helps...




回答3:


Create a Common Super-Table. Make another table EMPLOYEE_ID with only a primary key. Let both EMPLOYEE, EMPLOYEE_FORECAST and EMPLOYEE_ARCHIVE reference it.




回答4:


Your data model seems a tad confused. If EMPLOYEE and EMPLOYEE_FORECAST have identical structures why have two tables? What is the business rule here?

And if they are supposed to be two separate tables why store them in a common archive table? Why not have separate archives for each table?

I agree with @Ollie. You need to rethink your data model so it clearly expresses how your business operates. If you post your business rules here I'm sure we can help you untangle things. But here is probably the crucial question: do the following keys identify one employee (i.e one person in the real world) or two?

employee.emp_id = 100
employee_forecast.emp_id = 100


来源:https://stackoverflow.com/questions/10331265/how-to-have-unique-primary-key-in-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!