I have a form on a website which has a lot of different fields. Some of the fields are optional while some are mandatory. In my DB I have a table which holds all these values, is it better practice to insert a NULL value or an empty string into the DB columns where the user didn't put any data?
MySQL, better to insert NULL or empty string
MySQLsqlsql-null
Related Topic
- Mysql – Should I use the datetime or timestamp data type in MySQL
- SQLite – UPSERT *not* INSERT or REPLACE
- Php – How to do ‘insert if not exists’ in MySQL
- Sql – What are the options for storing hierarchical data in a relational database
- Mysql – Insert into a MySQL table or update if exists
- Php – MySQL and PHP – insert NULL rather than empty string
- Mysql – How to reset AUTO_INCREMENT in MySQL
- Mysql – How to import an SQL file using the command line in MySQL
Best Answer
By using
NULL
you can distinguish between "put no data" and "put empty data".Some more differences:
A
LENGTH
ofNULL
isNULL
, aLENGTH
of an empty string is0
.NULL
s are sorted before the empty strings.COUNT(message)
will count empty strings but notNULL
sYou can search for an empty string using a bound variable but not for a
NULL
. This query:will never match a
NULL
inmytext
, whatever value you pass from the client. To matchNULL
s, you'll have to use other query: