Is it possible to create a foreign key constraint

2019-08-03 08:09发布

Is it possible to add a foreign key constraint on a table which will allow values which do NOT exist in another table?

In the example below, both tables contain the field USER_ID. The constraint is that a customer and and an employee cannot have the same USER_ID value.

I am very limited in adding new tables or altering one of the tables in any way.

CUSTOMER
--------------------------
USER_ID     varchar2(10)

EMPLOYEE
--------------------------
USER_ID     varchar2(10)

I thought of a few workarounds, such as a view which contains the data from both tables or adding an insert trigger on the table I can modify.

2条回答
趁早两清
2楼-- · 2019-08-03 08:28

Unless you are willing to change the data model as someone else has suggested, the simplest way to proceed with the existing structure while maintaining mutual exclusion is to issue check constraints on the user_ids of both tables such that they validate only to mutually exclusive series.

For example, you could issue checks to ensure that only even numbers are assigned to customers and odd numbers to employees ( or vice-versa).

Or, since both IDS are varchar, stipulate using your check constraint that the ID begins with a known substring, such as 'EMP' or 'CUST'.

But these are only tricks and are hardly relational. Ideally, one would revise the data model. Hope this helps.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-03 08:45

No, no such thing exists, though it is possible to fake.

If you want to do this relationally (which would be a lot better than views/triggers) the easy option is to add a E to all employee IDs and a C to all customer IDs. However, this won't work if you have other attributes and you want to ensure they're not the same person (i.e. you're not just interested in the ID).

If this is the case you need to create a third table, let's call it PEOPLE:

create table people ( 
   user_id varchar2(10) not null
 , user_type varchar2(1) not null
 , constraint pk_people primary key (user_id)
 , constraint chk_people_user_types check ( user_type in ('C','E') )
   );

C would stand for customer and E for employee in the check constraint. You then need to create a unique index/constraint on PEOPLE:

create index ui_people_id_type on people ( user_id, user_type );

Personally, I'd stop here and completely drop your CUSTOMER and EMPLOYEE tables; they no longer have any use and your problem has been solved.

If you don't have the ability to add new columns/tables you need to speak to the people who do and convince them to change it. Over-complicating things only leads to errors in logic and confusion (believe me - using a view means you need a lot of triggers to maintain your tables and you'll have to ensure that someone only ever updates the view). It's a lot easier to do things properly, even if they take longer.


However, if you really want to continue you alter your CUSTOMER and EMPLOYEE tables to include their USER_TYPE and ensure that it's always the same for every row in the table, i.e.:

alter table customers add user_type default 'C' not null;
alter table customers add constraint chk_customers_type
      check ( user_type is not null and user_type = 'C' );
查看更多
登录 后发表回答