Query in 3 tables and comparisons with sql

Asked

Viewed 115 times

0

Good night! I’m at an impasse here. I’m thinking of the best way to solve a solution. The problem is this: I have 3 tables with the names of Monsters, items and inventory. The Monster has an inventory, in this inventory has 48 spaces. Each space may or may not be filled with an item. What would be the correct way to make queries in these tables? For example if the user searches by monster he must display all items within this inventory and if the user searches for items he must display all monsters that have this item in the inventory. Could someone give me a practical example of how to do this only using sql? Grateful from now on.

  • 1

    I think it starts by arranging the inventory table and one item per line, not 48 in one line. Unless you have a very special reason for this (although the probability is low). The "inventory" table could be id, id_owner, id_item, so you search by item or by owner. Only I think you could elaborate on the question better.

  • is that the monster inventory contains 48 spaces, each space may or may not contain an item.

  • 1

    this solution that I said allows as many spaces you want, can continue being 48. Only that there would be 48 separate entrances, much easier to remove".

1 answer

2


You must respect the normal database rules.

Primeira Forma Normal (ou 1FN) requer que todos os valores de colunas em uma tabela sejam atômicos (exemplo: um número é um átomo, enquanto uma lista ou um conjunto não o são). A normalização para a primeira forma normal elimina grupos repetidos, pondo-os cada um em uma tabela separada, conectando-os com uma chave primária ou estrangeira;
Segunda Forma Normal (ou 2FN) requer que não haja dependência funcional não-trivial de um atributo que não seja a chave, em parte da chave candidata;
Terceira Forma Normal (ou 3FN) requer não haver dependências funcionais não-triviais de atributos que não sejam chave, em qualquer coisa exceto um superconjunto de uma chave candidata;
Forma Normal de Boyce-Codd (ou BCNF) requer que não exista nenhuma dependência funcional não-trivial de atributos em algo mais do que um superconjunto de uma chave candidata. Neste estágio, todos os atributos são dependentes de uma chave, de uma chave inteira e de nada mais que uma chave (excluindo dependências triviais, como A → A);
Quarta Forma Normal (ou 4FN) requer que não exista nenhuma dependência multi-valorada não-trivial de conjuntos de atributo em algo mais de que um superconjunto de uma chave candidata;
Quinta Forma Normal (ou 5FN ou PJ/NF) requer que não exista dependências de joins (associações) não triviais que não venham de restrições chave;
Domain-Key Normal Form (ou DK/NF) requer que todas as restrições sigam os domínios e restrições chave.

Bearing this in mind, we know that there are no multivalorate attributes, for example in 1 row and 1 column various information in the same column. We also know that a database is scalable vertically and not horizontally, so it is preferable for your database to grow downward and not sideways.

Then we can get to the structure +- like this:

Monster: ID, NAME, etc

Inventory: ID, Id_monster, specifications, slots, etc etc

Items: ID, Name, attributes, etc

Now how to do to unite?

Let’s see, 1 inventory can have 0 or more items 1 item can have 1 inventory 1 monster can have 1 inventory

With this we come to the conclusion that we need an associative table. It goes like this: Itensinventario Id_inventario, Id_item, Quantity? etc

Completion,

when we have a relationship of 1 to 1 as for example monster and inventory we can associate them directly in the table. but when the association is 1 for many or many for many, an associative table is necessary. Your select would look like this:

SELECT * FROM tabela_monstro
INNER JOIN tabela_inventario ON tabela_monstro.id = tabela_inventario.id_monstro
INNER JOIN tabela_associativa_inventario ON tabela_inventario.id = tabela_associativa_inventario.ID_inventario
LEFT JOIN tabela_itens ON tabela_associativa_inventario.id_item = tabela_itens.ID

Ready.

Edit. If you want a fixed amount of slots like 48, you can treat it in the code, or with some Trigger or something like that. But it is not safe to assume that this value is immutable.

Edit2. Source of normal forms https://en.wikipedia.org/wiki/Database_normalization

Browser other questions tagged

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