Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Placing an array of IDs in a single database field isn't really a good idea and you lose some of the power a relational database gives you by designing your database this way.</p> <p>Rather than storing an array of ingredient IDs in your product's table I would create a third table.</p> <p>An example table schema would be like this:</p> <pre> Products id productName productDescription </pre> <pre> Ingredients id ingredientName </pre> <pre> ProductIngredients id id_Products id_Ingredients </pre> <p>Some sample data might look like:</p> <pre> Products <strong>id productName productDescription</strong> 1 Peanutbutter And Jelly Sandwich Best sandwich ever </pre> <pre> Ingredients <strong>id ingredientName</strong> 1 Peanutbutter 2 Jelly 3 Special sauce 4 Bread </pre> <pre> ProductIngredients <strong>id id_Products id_Ingredients</strong> 1 1 1 2 1 2 3 1 4 </pre> <p>You could then get a list of IDs of ingredients containing the word 'nut' like this:</p> <pre><code>SELECT id FROM Ingredients WHERE ingredientName LIKE '%nut%' </code></pre> <p>And a list of products containing ingredients which contain the word 'nuts' like this:</p> <pre><code>SELECT Products.productName FROM Products LEFT JOIN ProductIngredients ON ProductIngredients.id_Products = Products.id LEFT JOIN Ingredients ON Ingredients.id = ProductIngredients.id_Ingredients WHERE Ingredients.ingredientName LIKE '%nut%' </code></pre> <p>You could get a list of ingredients for a product like this:</p> <pre><code>SELECT Ingredients.ingredientName FROM Ingredients LEFT JOIN ProductIngredients ON ProductIngredients.id_Ingredients = Ingredients.id WHERE ProductIngredients.id_Products = 1 </code></pre> <p>This would give you a list such as this:</p> <ul> <li>Peanutbutter</li> <li>Jelly</li> <li>Bread</li> </ul> <h3>Edit</h3> <p>This is is called a <em>many-to-many</em> relationship.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload