Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use 2 tables to store this data. </p> <pre><code>users: username | password | email ------------------------- items: username | item </code></pre> <p>You can then insert an unlimited number of items for a given username in the second table (each item is a new record). </p> <p>You can then select all the items for one user like this:</p> <pre><code>SELECT item FROM items WHERE username = ? </code></pre> <p>You can also combine the 2 tables in a number of ways. </p> <pre><code>/* this one selects all users who have a specific item */ SELECT u.* FROM users u JOIN items i ON u.username = i.username WHERE i.item = ? </code></pre> <p><strong>How do you wish to use the data?</strong></p> <p>You should also start to use ID values (numbers) so that the you don't have to waste space repeating text based content such as the username.</p> <hr> <blockquote> <p>I think this might be a really good way, however can you show me what you mean by each item is a new record? I'm still a bit confused. </p> </blockquote> <p>You could insert the values like this:</p> <pre><code>insert into items values ('jonathan', 'hat'); insert into items values ('jonathan', 'drink'); insert into items values ('jonathan', 'mouse'); insert into items values ('user1981730', 'hat'); insert into items values ('user1981730', 'mouse'); </code></pre> <p>Which would result in this:</p> <p><img src="https://i.imgur.com/G9cvHyS.png" /></p> <p>Then you could query for all users who have <strong>drink</strong> as an item:</p> <pre><code>SELECT u.* FROM users u JOIN items i ON u.username = i.username WHERE i.item = 'drink' </code></pre> <p><img src="https://i.imgur.com/GEtQZEZ.png" /></p> <p>Then to fetch a list of all items obtained by <strong>user1981730</strong>:</p> <pre><code>SELECT item FROM items WHERE username = 'user1981730' </code></pre> <p><img src="https://i.imgur.com/iVunMbh.png" /></p> <p>Once you are happy with this we can talk about adding ID columns. </p> <hr> <blockquote> <p>what do you mean by ID columns?</p> </blockquote> <p>Notice that in the above example the items table has a lot of duplicate information. The username <strong>jonathan</strong> is repeated 3 times, the item <strong>hat</strong> is repeated twice. This would be much worse with a real database. </p> <p>We can save space and generally increase performance by using an integer ID to represent each unique value. Each user would have an ID and so would each unique item. </p> <p>Let's start with just the users. We must add the new column to the users table. </p> <p><img src="https://i.imgur.com/8My0zAZ.png" /></p> <p>We make it a <strong>PRIMARY KEY</strong> which will ensure that all values are unique so that no 2 users can have the same ID. We can also use the <strong>AUTO_INCREMENT</strong> to have the value automatically increase with each new record. </p> <p><img src="https://i.imgur.com/5Aa3hgI.png" /></p> <p><img src="https://i.imgur.com/ECe2YIu.png" /></p> <p>We would then change our items table so that it made use of the new column instead of username. </p> <p><img src="https://i.imgur.com/3Di4F2h.png" /></p> <p>Notice how we still have the same information that we did before. User with ID number 1 has a drink, hat and a mouse and the user with ID number 2 has the hat and mouse. </p> <p>We can still do the same queries as before (with some small changes). </p> <pre><code>/* this selects all the items for a known user_id */ SELECT item FROM items WHERE user_id = ? /* this selects all the items for a user when we only know the username */ SELECT i.item FROM items i JOIN users u ON u.user_id = i.item_id WHERE u.username = ? /* this one selects all users who have a specific item */ SELECT u.* FROM users u JOIN items i ON u.user_id = i.user_id WHERE i.item = ? </code></pre> <p>So far we have only modified the tables to avoid duplicating the username. If we wanted to also avoid duplicating item names we can add an additional table. This starts to get tricky so I won't go into too much detail (for now) but the table structure would be as follows:</p> <pre><code>users: username | password | email ------------------------------- items: item_id | name ------------------------------- users_items: user_id | item_id </code></pre> <p>The <strong>users</strong> table is as before. The <strong>items</strong> table has a new role and it stores all the unique items. The <strong>users_items</strong> table now links the other 2 tables.</p> <hr> <p>I will end the SO answer here. If you have any further question you can email me with the address listed above (the one used in the example table). </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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