2
I’m trying to create a conditional "or" just using the relational paradigm:
Having the tables pessoa
, cliente
, funcionario
and considering that a person may be a client or an employee (a 1:1 ratio, not null), it would be correct to reference the table cliente
and funcionario
with the same foreign key from the table pessoa
? Example:
The foreign key fk_tipo
reference Funcionario.pk_funcionario
and Cliente.pk_cliente
.
The only problem I found in this mode is that the table Cliente
cannot have a primary key equal to the other primary key in the table Funcionario
and vice versa. Then you would have to review the mode of generation of these keys.
Is there any other way to accomplish what I want by making the most of the relational model?
Ps.: is not duplicate of "A foreign key can reference more than one table?".
I don’t understand what you want, could you give an example? By the way, if "every customer is a person" and "every employee is a person" you can have the table
pessoa
, normal, and in the tablescliente
andfuncionario
the primary key is also a foreign key for the tablepessoa
. This guarantees oneness, but nay prevents against: a) the existence of a person who is neither client nor employee; b) the existence of a person who is both client and employee.– mgibsonbr
@mgibsonbr, so I put the foreign key in
pessoa
.– ptkato
In this case, what is the difference between this question and the other linked? If you refer to having two foreign keys in
pessoa
- one forcliente
and another tofuncionario
- and wants to ensure that one and only one of them isNULL
, maybe you can do it usingcheck
(I can give an answer with example, if this is what you are looking for). Personally, I still consider my initial suggestion better - one way or another one of the tables will be without referential integrity, and I prefer it to be the basis.– mgibsonbr
P.S. According to the same article linked in Wikipedia, can not use the
check
to validate a single column - ensure that it is a foreign key for one or another table - because this type of restriction usually only validates data in the same row of the same table. And to reiterate, I don’t see the difference between that question and the other one that you say is "not duplicate".– mgibsonbr
The other question is about whether a foreign key can reference more than one table, here is about how it would be possible to create an "or" using relational. I think it was a little implied that I emphasized my attempt using a foreign key referencing two tables. I’ll try to adjust the question.
– ptkato
Okay, I think I get it now. If I may mention the motivation behind this question (even if it is "just because I’m curious to know") will help a lot - because in practice, I do not see what benefits this would bring in relation to my suggestion (which, I just saw now, is the same thing was suggested in the answer to the related question). Anyway, +1 to the question, and I’ll wait for someone with more knowledge to answer (in fact the restriction
check
doesn’t fit here).– mgibsonbr
The other question was really out of curiosity, I was trying to figure out new ways to use the relational model. This is more to try to solve problems by simple means and not fill the bank with procedures, functions or triggers.
– ptkato
You need both tables for what reason? Have a table
pessoa
with all attributes offuncionario
andcliente
, and two columns to indicate the type the entity can assume (is_cliente
andis_funcionario
) resolves the question of its application in my view.– gmsantos
@gmsantos, specification. All the repeated lines in the two tables would be placed in
Pessoa
, the remaining in their due scales.– ptkato
That’s exactly what I suggested. The difference is that the unique attributes of each entity would be null depending on the type, and you wouldn’t have to
joins
unnecessary to obtain information from an Employee or Customer– gmsantos
For clarity: http://sqlfiddle.com/#! 2/5afb6d/3
– gmsantos
Yes, I could, but what I aimed for in doing so was to avoid null columns. Besides that this would allow a person to be both client and employee at the same time, this is the reason for "or".
– ptkato
The best normalization is the answer to the linked question. I didn’t understand why it didn’t solve the whole relationship problem: you wouldn’t have null columns, nor repeated records, and a person could be either employee, or client, or both.
– Caffé