Andrew
Andrew

Reputation: 238667

MySQL: what data type should I use for a string of comma separated values?

I need to store a string of comma separated values.

'1,23,47' or '1' or '' (null)

What data type would work best for comma separated values?

Note: I am only asking which data type would be appropriate for this scenario.

Upvotes: 6

Views: 19637

Answers (5)

No Refunds No Returns
No Refunds No Returns

Reputation: 8336

Create a variable character field using the following template syntax varchar(your maximum length) like

comment varchar(128)

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

If you will never need to query specific sub-items in the list of values, then use varchar. However, if you will need to query the values I strongly recommend you consider a different database design, such as a value table where each row contains a join key to the main table and a single value.

Upvotes: 1

Luke101
Luke101

Reputation: 65238

Depending on how big the string you expect world determine the size of varchar. If you have no idea how long the string would be you can just use TEXT data type.

Upvotes: 6

ari_aaron
ari_aaron

Reputation: 43

VARCHAR, it can store anything.

TEXT if you think it will be long.

Upvotes: 2

Jason S
Jason S

Reputation: 189646

Literal answer: a VARCHAR of some sort

A better answer: Don't store a list of comma separated values. Store one value per row, and use a SELECT query with GROUP_CONCAT to generate the comma separated value when you access the database.

Upvotes: 3

Related Questions