Query in 3 tables and comparisons with sql

0

Good evening! I'm stuck here. I'm thinking of the best way to solve a solution. The problem is as follows: I have 3 tables with the names of monsters, items and inventory. The Monster has an inventory, this inventory has 48 spaces. Each space may or may not be filled with an item. What would be the correct way to query these tables? For example if the user searches for a monster he should display all items within this inventory and if the user searches for items he should display all the monsters that have this item in inventory. Could anyone give me a practical example of how to do this only using sql? Thank you in advance.

    
asked by anonymous 14.12.2015 / 23:19

1 answer

2

You must adhere to 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.

With this in mind, we know that there are no multivalued attributes, for example in 1 row and 1 column of several information in the same column. We also know that a database is scalable vertically and not horizontally, so it is preferable that your database grow down and not sideways.

Here we can arrive at the structure + - thus:

Monster: ID, NAME, etc etc

Inventory: ID, Monster Id, specifications, slots, etc etc.

Items: ID, Name, Attributes, etc. etc.

Now how do you unite?

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

With this we come to the conclusion that we need an associative table. Stays like this: Inventory Items Inventory_ID, Quantity_Item, Quantity? etc.

Conclusion,

When we have a relationship from 1 to 1 such as 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 handle this in code, or with some trigger or something of the genre. But it is not safe to assume that this value is unchangeable.

Edit2. Source of normal forms link

    
14.12.2015 / 23:48