0

Specifically, I'm building an text-based RPG Application, but the question is this: what is the difference between using arrays and storing values separately.

Normally, If you want to get your 10th Monster, you say select 10 but storing that number 10 is the problem. Should I store the numbers in an array so when they select 10 it selects the 10th element in the array OR store numbers in a different table so when they select 10 it checks for the column with number 10 and which it's owner ID = the selectors' ID.

Dylee
  • 103
  • 2
  • 12
  • Possible duplicate of [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – philipxy Feb 28 '19 at 02:11

1 Answers1

1

This is a very general question.

When is it a good idea to use arrays?

  • If you would otherwise end up with very many columns.

  • If you never need to use array elements in WHERE or JOIN conditions.

  • If you never need to modify array elements, only the array as a whole.

Usually arrays are a bad idea, because they violate the first normal form.

If you need to use array elements in WHERE conditions efficiently, or you want to modify individual array members, the suffering starts.

Arrays can be a good thing when used wisely, but if you look through the questions asked here, you'll find a good many by people who suffer from data models that abuse compound data types like arrays or JSON.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • I only need to use them in WHERE conditions (such as `SELECT monsters[1], monsters[2] FROM users WHERE user_id = {user_id}`, don't need to use JOIN conditions for the elements just yet, and I don't need to modify the specific elements. All I have to do is adding and removing elements from the array. So I guess it's safe to use them. And are Arrays faster? – Dylee Dec 17 '18 at 11:19
  • Adding and removing elements from an array means rewriting the whole array. If that's ok for you, you can do it. I won't make any prediction about speed; that depends on too much and should be tested. – Laurenz Albe Dec 17 '18 at 11:30
  • Okay, Thank you :) – Dylee Dec 17 '18 at 11:43