MySQL Forums
Forum List  »  Newbie

Re: Arrays in table fields?
Posted by: fuzztrek
Date: July 04, 2005 05:53PM

I don't think an array is possible. However, if you introduce another table you should be able to get the desired result.

Say you have your users table (with a name, password, etc) and you want to store all the cards a player has. Instead of creating an array or even one field for every single card a person could ever get, create a new table with a user_id and card name. Each record in this new table will represent one card a user has collected. So you might end up with something like:

users table:
user_id: 1
username: Fuzztrek
password: *******

users_card table:
user_id: 1
card_name: "Ace of Diamonds"

user_id: 1
card_name: "Deuce of Spades"

user_id: 1
card_name: "Jack of Spades"

user_id: 1
card_name: "Queen of Hearts"

etc. etc.

If you want to select all the cards a user has, you can use the following query:

SELECT users_card.card_name FROM users, users_card WHERE users.username = 'Fuzztrek' AND users_card.user_id = users.user_id

Note that you should actually have 3 tables, one for users, one for cards (card name, etc), and one defining the relationship between cards and users. This process of splitting tables into new tables is called "normalization". There's a good article on it here: http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

For me, the concept of creating so many tables was foreign and seemed very wasteful. However, it's actually not so bad and allows for great flexibility. After you master normalization, you should spend some time learning about joins.

Hope that helped ;)

Options: ReplyQuote


Subject
Written By
Posted
Re: Arrays in table fields?
July 04, 2005 05:53PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.