1

how to define integer array as a field when creating new table in mySQL

1
  • Do you need to query individual elements of the array? Commented Aug 21, 2010 at 10:13

2 Answers 2

2

There is currently no way to store an array of integers in MySQL, so you should implement this by yourself. You could choose one of a few things, these two approaches included:

  • serialise the data with a separator (e.g. LONGTEXT: 123|4|65|864)
  • pack the integers into a blob (e.g. LONGBLOB: 0x0000007b000000040000004100000360)
Sign up to request clarification or add additional context in comments.

Comments

2

You can't. You could do something like convert your array to a comma-separated string, and store this. Or you could define a normalised table structure and have each integer in it's own row. Each row for a given array would also contain some kind of array key as a separate field. This also has the advantage that you can easily query for individual array elements.

Edit In my view the first option is not very elegant. Unless you define your field as TEXT you're going to have issues with varying string lengths, and defining your field as VARCHAR(10000) or whatever is not very efficient. Certainly if your array lengths are long you should consider a normalised solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.