Table Receives 2 foreign keys - Relational Model

Asked

Viewed 59 times

0

I have a table called QR-Code, this can be sent by an employee, or by a resident, but in the table Qr-code has to have only one code of who sent it, as I can represent it in the relational model??


At times I’m not close to a conputator so this demonstration is not great, is the following the Qr-code will only receive a code , or employee or resident, but the problem is that both have cardinality 1:n inserir a descrição da imagem aqui

  • Search for entity partitioning. Basically you will have a sender that can be an employee or a resident.

  • Alvino, I suggest you make the initial DDL of your model even if you have no relationships, placing the initial tables and main columns. So it will bring more context to the question.

2 answers

1

Creating a sort of generic perspective of modeling I would imagine something like this:

  • Guard all the staff People(ID, Name);

  • Ranks employees Employees(Personal ID, License plate, [...]);

  • Ranks residents Residents(Personal, Registration, [...]);

  • Data qr. Qr_code(ID, Id_person, [...]);

Then just make a join by calling one or two tables.

SELECT
   P.ID,
   P.Nome,
   ISNULL(F.Matricula, '-') AS Matricula,
   ISNULL(R.Pessoa_ID, '-') AS Registro 
FROM Pessoas P
INNER JOIN Funcionarios F
ON F.Pessoa_ID = P.ID
INNER JOIN Residentes R 
ON R.Pessoa_ID = P.ID
  • Thank you, I understand what I have to do. VLW

0

The best thing you have to do is create a table that is the parent table of residente and of funcionário, then the table Qr-code just has a relationship with this parent table.

You need to see if there are any columns in common between residente and funcionário and place these common columns in the parent table.

In the tables residente and funcionário has a FK for the parent table.

this way can have a relationship of 1:N between parent relationship and table Qr-code, this parent table represents either a residente or a funcionário.

  • Then the keys of employees and residents would be a primary and foreign key at the same time, no??

  • Of course, foreign keys are usually primary keys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.