user401000
user401000

Reputation: 255

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

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

Upvotes: 1

Views: 4241

Answers (2)

Richard H
Richard H

Reputation: 39065

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.

Upvotes: 2

Delan Azabani
Delan Azabani

Reputation: 81404

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)

Upvotes: 2

Related Questions