I got one general and probably easy question. When saving an array of int numbers into a MySQL-database (numbers are seperated by a comma), what would be the correct datatype for the database to choose? When choosing INT as type, I guess I may not use commas, may I?
So I used “text” as datatype since these arrays of numbers can get really long and I didnt want to set a limit. But is this the right way to go or is it bad to save only numbers with comma in a “text” field in my database?
Thanks for your help!
phpheini
The best datatype would be another table.
It is indeed very “bad to save only numbers with comma in a “text” field in my database”
This would be breaking First Normal Form
Doing this will cause you increase data contention on the field when modifying values.
For example lets suppose two users want to add a new value to the list of numbers associated with a record. When the table is in 1NF normally both users would get to insert a record. When not in 1NF its easy for one user to lose the addition because you’re writing back several values to a single attribute.
It also makes it very difficult to do simple kinds of queries like (which records have a 3 in the list). Yes it can be done but it won’t be able to use any indexes, because databases don’t index what’s inside a field (except for special cases like SQL Server’s XML type)