Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we store List type of data to MYSQL database table?

Tags:

mysql

I need to store some data of list type into a MYSQL database.Is it possible to store it in MYSQL,if yes then what should be the data type of the field that will hold this data?

Thanks in advance.

like image 781
Aditi Avatar asked May 10 '16 10:05

Aditi


People also ask

Can we store a list in MySQL table?

You can store the representation that is actually used in the application -- or really a serialized version of it. However, you get no SQL functionality from this, such as being able to count the number of coordinates or fetching rows that only have a certain name.

Which type of data can be stored in the MySQL database?

The data type is a guideline for SQL to understand what type of data is expected inside of each column, and it also identifies how SQL will interact with the stored data. In MySQL there are three main data types: string, numeric, and date and time.

Which data type is used to store data and time in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

Which data type is used in MySQL to store text?

The four BLOB types are TINYBLOB , BLOB , MEDIUMBLOB , and LONGBLOB . These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT , TEXT , MEDIUMTEXT , and LONGTEXT . These correspond to the four BLOB types and have the same maximum lengths and storage requirements.


2 Answers

No, no, no, no, no! Never store multiple values in a single column!

Seperate the values and store each of them in another record.

For instance if you have users and want to store a list of the roles each user has then you can do it like this

users table
-----------
id
name
...


roles table
-----------
id
name


user_roles table
----------------
user_id
role_id
like image 184
juergen d Avatar answered Oct 11 '22 23:10

juergen d


You can use JSON datatype. MySQL provides JSON as column data type and provides some functions to work with JSON data.

Look at the documentation

NOTE: you must use 5.7+ version

As of MySQL 5.7.8, MySQL supports a native JSON data type that enables efficient access to data in JSON

like image 25
Wajih Avatar answered Oct 11 '22 22:10

Wajih