Multiple references to a FK in the same column/row

Asked

Viewed 100 times

0

Good afternoon colleagues, I would like a help to model a comic, the doubt is as follows:

Is it possible to have more than one FK ID on a tuple? EX:

tabela teste1

|id_teste1|id_teste2|teste|teste01|
| 1       |1,2,3    |tes  | les   |



tabela teste2

|id_teste2|registro1|registro2|
|1        |lalala   |lelele   |
|2        |bababa   |bebebe   |
|3        |cacaca   |cecece   |

At the time of performing the query, I could visualize the data ta table teste2?

more or less this way

Any suggestions as to how I could have such a result? more or less this way:

|id_teste1|registro1|registro2|teste|teste01|
| 1       |lalala   |lelele   |tes  | les   |
| 1       |bababa   |bebebe   |tes  | les   |    
| 1       |cacaca   |cecece   |tes  | les   |

So as not to get out of context I’ll explain what I’m trying to do, is a tool to read a spreadsheet and write the data into a BD, only that the lines of some areas may vary, my idea was to make a separate table, and each row of the spreadsheet would be a record, and all these records would be referenced in the record of the "top" table, as if the table "spreadsheet" had a column where I can have the registry ids as pk, as in the example above "id_teste2" where we have 3 references.

I have limited experience in sql and would like the suggestion of colleagues ;D

Thanks for your help.

*ps: I searched a bucado and had no results D:

Edit:

The relation is: N resgistros ta table teste2 for each(1) table record teste1 ; D

  • The relationship between the table teste2 and the table teste1 is 1-n? If yes, the ideal would be to have a tuple for each record of the referenced table, in this case, 3 items in the teste1

  • @Ronaldoaraújoalves, the relation would be contrary I believe, varios registros na teste2 for a record of teste1, thanks for the reply Ronaldo ; D

  • In a properly normalized database this is not possible because a non-atomic attribute violates the normalization rules. Now there are DBMS that allow implementing such a solution type, but it will be a particular implementation and outside the SQL standard.

  • It is @anonimo, I checked the normalization rules and I saw that I was running away from the standard, if I told you how the first model was, the DBA on duty already killed me kkk, by the way, thank you very much for the answer ;D

1 answer

0


Relationships of tables can be:

- 1-1

Ex: a user table and a customer table, where each customer can have ONE user, and each user can belong to ONE customer only

- 1-n

Ex: an order table and a request table, where each order can have N items, but each item can belong to ONE order only

- n-n

Ex: a class table and a class table, where a student can take N classes, and each class can have N students


The model you want is 1-n (correct me if I misunderstood the question).

In this case, the table teste1 is equivalent to the request, and teste2 is equivalent to items.

The model is correct: your table teste1 has a id_teste2 that references the teste2. But for each "order item" (teste1) you will need to have a record, where all these items would have the same id_teste2.

Example:

Considering the models:

teste1

id_teste1 | id_teste2 | item    | quantidade
----------+-----------+---------+--------------
1         | 1         | mouse   | 2
2         | 1         | teclado | 1
teste2

id_teste2 | numeroPedido
----------+-------------
1         | 5001

That consultation:

select *
from teste2 t2
inner join teste1 t1
on t1.id_teste2 = t2.id_teste2

return this data:

id_teste2 | numeroPedido | id_teste1 | id_teste2 | item    | quantidade |
1         | 5001         | 1         | 1         | mouse   | 2          |
1         | 5001         | 2         | 1         | teclado | 1          |
  • 1

    Got it Ronaldo, could you exemplify a query in this case for kindness? teste2 can’t be a picnic, right? so every record of the table teste1 we will have several records with the same id_teste2, at the time of consulting I pass the reference of the id_teste2 and I can see all the records I have the id_teste2 referenced right?

  • Another point, I couldn’t leave as autoincrement too right? and during the Insert I would have to manually pass the ids to them to be equal after correct?

  • Thank you Ronaldo, marked as answer ; D

  • The FK field of the "dependent" table can NOT be autoincrement. And every FK has to reference a PK from another table, which in most cases is autoincrement.

  • Replying to his first comment: that’s right :)

  • Perfect, thank you very much Ronaldo, have a great day ; D

Show 1 more comment

Browser other questions tagged

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