简体   繁体   中英

How to have unique primary key in two tables?

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 eg 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.

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.)

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...

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.

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 (ie one person in the real world) or two?

employee.emp_id = 100
employee_forecast.emp_id = 100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM