简体   繁体   English

如何在两个表中具有唯一的主键?

[英]How to have unique primary key in two tables?

I have two tables in my system EMPLOYEE and EMPLOYEE_FORECAST . 我的系统EMPLOYEEEMPLOYEE_FORECAST有两个表。 Both have the same columns, entire structure is same. 两者具有相同的列,整个结构相同。 I have another archive table with same structure called EMPLOYEE_ARCHIVE table. 我还有另一个具有相同结构的存档表,称为EMPLOYEE_ARCHIVE表。

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. 由于EMPLOYEEEMPLOYEE_FORECAST记录可能具有相同的主键,例如EMPLOYEE一条记录将具有100的pk值,而EMPLOYEE_FORECAST另一条记录也将具有100的pk值,这肯定会发生,因此当将它们插入存档表时,我将拥有重复的主键。

The problem is I will also have some relation table like employee_products , employee_forecast_products and also employee_archive_products . 问题是我还将有一些关系表,例如employee_productsemployee_forecast_products以及employee_archive_products These tables will have emp_id and product_id . 这些表将具有emp_idproduct_id So with same emp_id I wont be able to figure out the exact employee. 因此,使用相同的emp_id我将无法找出确切的员工。

So, is there any way to have a unique primary key both the EMPLOYEE and EMPLOYEE_FORECAST tables. 因此,有没有办法使EMPLOYEEEMPLOYEE_FORECAST表都具有唯一的主键。

So you cannot not use the PK column of EMPLOYEE table as a PK column of the archive table. 因此,您不能将EMPLOYEE表的PK列用作存档表的PK列。

You can add a new PK column to the archive table. 您可以将新的PK列添加到存档表中。

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. 如果出于某种原因,如果您希望EMPLOYEE表的PK列成为存档表中的PK,则可以将一个标志列添加到存档表中,以指示记录来自哪个表。 And you could have a composite PK in the archive table containing the original PK and the flag column. 而且您可以在存档表中包含一个复合PK,其中包含原始PK和标志列。 (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.) (通常,我不建议使用复合PK-s,因此,即使您希望拥有此标志列,也可以在归档文件中再添加一个普通的PK列。)

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 : 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. 由于存档表PK遍历原始表和Empid列,因此它将对所有数据保持唯一。

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. 仅使用主键制作另一个表EMPLOYEE_ID。 Let both EMPLOYEE, EMPLOYEE_FORECAST and EMPLOYEE_ARCHIVE reference it. 让EMPLOYEE,EMPLOYEE_FORECAST和EMPLOYEE_ARCHIVE都引用它。

Your data model seems a tad confused. 您的数据模型似乎有点困惑。 If EMPLOYEE and EMPLOYEE_FORECAST have identical structures why have two tables? 如果EMPLOYEE和EMPLOYEE_FORECAST具有相同的结构,为什么要有两个表? 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. 我同意@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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