Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a list in a db column

Tags:

sql

database

I would like to store an object FOO in a database. Lets say FOO contains three integers and a list of "Fruits".

The list can have any length, the only thing I know is that the all the fruits allowed are stored in another table.

Can I store the fruit list in a column?

like image 568
Nifle Avatar asked Jan 14 '09 19:01

Nifle


People also ask

Can you store a list in SQL column?

So, per Mehrdad's answer to a related question, I get it that a "proper" database table column doesn't store a list. Rather, you should create another table that effectively holds the elements of said list and then link to it directly or through a junction table.

How do I store a list of values in one column in SQL Server?

In the design all users data will be stored in a series of columns in a single table but one of the columns requires to store a list of values, for example: 'column1' will store the username , 'column2' will store the userID and 'column3' will store a list of items that will change over time.

Can we store list in mysql column?

Short answer: probably not. If you will ever need to do any manipulation on that column, you will find yourself in big trouble. Simply selecting all users in a group will require some operations on a string (usually not performance-friendly).

Can a database have a list?

a single item alone is still a list, but might be mistaken as not since we cannot tell (or ask) the database that the true type is a list. This can be problematic if most rows have only one item in the list, when some have more than one: there's no way to enforce proper usage of the column as a list.


4 Answers

In a normalized relational database, such a situation is unacceptable. You should have a junction table that stores one row for each distinct ID of the FOO object and the ID of the Fruit. Existence of such a row means the fruit is in that list for the FOO.

CREATE TABLE FOO ( 
  id int primary key not null,
  int1 int, 
  int2 int, 
  int3 int
)

CREATE TABLE Fruits (
  id int primary key not null,
  name varchar(30)
)

CREATE TABLE FOOFruits (
  FruitID int references Fruits (ID),
  FooID int references FOO(id),
  constraint pk_FooFruits primary key (FruitID, FooID)
)

To add Apple fruit to the list of a specific FOO object with ID=5, you would:

INSERT FOOFruits(FooID, FruitID)
SELECT 5, ID FROM Fruits WHERE name = 'Apple'
like image 92
mmx Avatar answered Oct 04 '22 06:10

mmx


If you're quite sure of what you're doing (ie. you won't need to look up the list's values, for example), you could also serialize your object, or just the list object, and store it in a binary column.

Just character-separating the values may be fine too, and cheaper in terms of saving and loading, but be careful your data doesn't contain the separator character, or escape it (and handle the escapes accordingly while loading, etc... Your language of choice may do a better job at this than you, though. ;) )

However, for a "proper" solution, do what Mehrdad described above.

like image 28
AKX Avatar answered Oct 04 '22 05:10

AKX


Its technically possible but would be very poor design, imo.

You could do it by building the string and storing it in a nvarchar(max) field (if using sql server or its equivalent).

like image 32
E.J. Brennan Avatar answered Oct 04 '22 07:10

E.J. Brennan


You can, but it will likely treated as text, making searching in this column difficult and slow. You're better of using a related table.

like image 40
Diodeus - James MacFarlane Avatar answered Oct 04 '22 07:10

Diodeus - James MacFarlane