It actually doesn't accept NULL values it considers it as empty string. That's because you have your server in non-strict mode. That controls how MySQL handles invalid or missing values in inserts and updates. You can read more about modes here: http://dev.mysql.com/doc/refman/5.6/en/sql-mode.html#sql-mode-strict

mysql> insert into cities(state_id) values (20);
Query OK, 1 row affected, 1 warning (0.07 sec)

mysql> show warnings;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1364 | Field 'name' doesn't have a default value |
+---------+------+-------------------------------------------+

mysql> select name is null from cities where id = LAST_INSERT_ID();
+--------------+
| name is null |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

mysql> SET sql_mode = 'STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into cities(state_id) values (20);
ERROR 1364 (HY000): Field 'name' doesn't have a default value
Answer from Károly Nagy on Stack Exchange
🌐
W3Schools
w3schools.com › mysql › mysql_null_values.asp
MySQL NULL Values - IS NULL and IS NOT NULL
We will have to use the IS NULL and IS NOT NULL operators instead. SELECT column_names FROM table_name WHERE column_name IS NULL;
🌐
W3Schools
w3schools.com › mysql › mysql_notnull.asp
MySQL NOT NULL Constraint
To define a NOT NULL constraint on an existing table, use ALTER TABLE and add NOT NULL after the data type of the column name.
🌐
PopSQL
popsql.com › learn-sql › mysql › how-to-add-a-not-null-constraint-in-mysql
How to Add a Not Null Constraint in MySQL
--Example: Products have a default stock of 0 ALTER TABLE products MODIFY stocks INT NOT NULL; Note that you MUST restate the full column definition, otherwise undeclared attributes will go back to default settings. For example, not restating the DEFAULT clause will unset the default value. To ensure that you do not miss anything, you can use the SHOW CREATE TABLE command to see the full column definition: mysql> SHOW CREATE TABLE products\G *************************** 1.
Top answer
1 of 2
13

It actually doesn't accept NULL values it considers it as empty string. That's because you have your server in non-strict mode. That controls how MySQL handles invalid or missing values in inserts and updates. You can read more about modes here: http://dev.mysql.com/doc/refman/5.6/en/sql-mode.html#sql-mode-strict

mysql> insert into cities(state_id) values (20);
Query OK, 1 row affected, 1 warning (0.07 sec)

mysql> show warnings;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1364 | Field 'name' doesn't have a default value |
+---------+------+-------------------------------------------+

mysql> select name is null from cities where id = LAST_INSERT_ID();
+--------------+
| name is null |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

mysql> SET sql_mode = 'STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into cities(state_id) values (20);
ERROR 1364 (HY000): Field 'name' doesn't have a default value
2 of 2
7

To Károly Nagy's point,

You can use

SET SQL_MODE = 'STRICT_ALL_TABLES';

I'm sure you may already know but that is equivilent to:

SET @@SESSION.SQL_MODE = 'STRICT_ALL_TABLES';

Which, must be set for every session. And you can checky this by running this after the above statement.

SELECT @@SESSION.SQL_MODE; -- 'STRICT_ALL_TABLES'

SELECT @@GLOBAL.SQL_MODE; -- 'NO_ENGINE_SUBSTITUTION'

If you have access please set this at the global level:

SET @@GLOBAL.SQL_MODE = 'STRICT_ALL_TABLES';

Which becomes the default for every new session thereafter.

Cheers, Jay ;-]

NOTE: Tested On MySQL Version 5.6.23-log & MySQL Workbench 6.2.5

🌐
MySQL
dev.mysql.com › doc › refman › 8.4 › en › working-with-null.html
MySQL :: MySQL 8.4 Reference Manual :: 5.3.4.6 Working with NULL Values
Thus it is entirely possible to insert a zero or empty string into a NOT NULL column, as these are in fact NOT NULL.
Top answer
1 of 12
541

You should use IS NOT NULL. (The comparison operators = and <> both give UNKNOWN with NULL on either side of the expression.)

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

Just for completeness I'll mention that in MySQL you can also negate the null safe equality operator but this is not standard SQL.

SELECT *
FROM table 
WHERE NOT (YourColumn <=> NULL);

Edited to reflect comments. It sounds like your table may not be in first normal form in which case changing the structure may make your task easier. A couple of other ways of doing it though...

SELECT val1 AS val
FROM  your_table
WHERE val1 IS NOT NULL
UNION ALL
SELECT val2 
FROM  your_table
WHERE val2 IS NOT NULL
/*And so on for all your columns*/

The disadvantage of the above is that it scans the table multiple times once for each column. That may possibly be avoided by the below but I haven't tested this in MySQL.

SELECT CASE idx
         WHEN 1 THEN val1
         WHEN 2 THEN val2
       END AS val
FROM   your_table
        /*CROSS JOIN*/
       JOIN (SELECT 1 AS idx
                   UNION ALL
                   SELECT 2) t
HAVING val IS NOT NULL  /*Can reference alias in Having in MySQL*/
2 of 12
25

You can filter out rows that contain a NULL value in a specific column:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE somecolumn IS NOT NULL

If you want to filter out rows that contain a null in any column then try this:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE col1 IS NOT NULL
AND col2 IS NOT NULL
-- ...
AND coln IS NOT NULL

Update: Based on your comments, perhaps you want this?

SELECT * FROM
(
    SELECT col1 AS col FROM yourtable
    UNION
    SELECT col2 AS col FROM yourtable
    UNION
    -- ...
    UNION
    SELECT coln AS col FROM yourtable
) T1
WHERE col IS NOT NULL

And I agre with Martin that if you need to do this then you should probably change your database design.

🌐
TechOnTheNet
techonthenet.com › mysql › is_not_null.php
MySQL: IS NOT NULL
SELECT * FROM contacts WHERE last_name IS NOT NULL; This MySQL IS NOT NULL example will return all records from the contacts table where the last_name does not contain a null value.
Find elsewhere
🌐
MySQL
dev.mysql.com › doc › refman › 9.6 › en › working-with-null.html
MySQL :: MySQL 9.6 Reference Manual :: 5.3.4.6 Working with NULL Values
Thus it is entirely possible to insert a zero or empty string into a NOT NULL column, as these are in fact NOT NULL.
🌐
MySQL
dev.mysql.com › doc › refman › 8.0 › en › working-with-null.html
MySQL :: MySQL 8.0 Reference Manual :: 5.3.4.6 Working with NULL Values
August 31, 2021 - Thus it is entirely possible to insert a zero or empty string into a NOT NULL column, as these are in fact NOT NULL.
🌐
MySQL Tutorial
mysqltutorial.org › home › mysql basics › mysql not null constraint
MySQL NOT NULL Constraint
January 30, 2024 - Third, modify the column with a NOT NULL constraint. Consider the following example. ... INSERT INTO tasks(title ,start_date, end_date) VALUES('Learn MySQL NOT NULL constraint', '2017-02-01','2017-02-02'), ('Check and update NOT NULL constraint to your database', '2017-02-01',NULL);Code language: SQL (Structured Query Language) (sql)
🌐
GeeksforGeeks
geeksforgeeks.org › mysql › mysql-not-null-constraint
MySQL NOT NULL Constraint - GeeksforGeeks
July 23, 2025 - The NOT NULL constraint in MySQL serves as a validation rule for the columns. In NOT NULL constraints the column cannot contain the NULL values.
🌐
W3Schools
w3schools.com › sql › sql_notnull.asp
SQL NOT NULL Constraint
ADD ADD CONSTRAINT ALL ALTER ALTER COLUMN ALTER TABLE ALTER VIEW AND ANY AS ASC BACKUP DATABASE BETWEEN CASE CHECK COLUMN CONSTRAINT CREATE CREATE DATABASE CREATE INDEX CREATE OR REPLACE VIEW CREATE TABLE CREATE PROCEDURE CREATE UNIQUE INDEX CREATE VIEW DATABASE DEFAULT DELETE DESC DISTINCT DROP DROP COLUMN DROP CONSTRAINT DROP DATABASE DROP DEFAULT DROP INDEX DROP TABLE DROP VIEW EXEC EXISTS FOREIGN KEY FROM FULL OUTER JOIN GROUP BY HAVING IN INDEX INNER JOIN INSERT INTO INSERT INTO SELECT IS NULL IS NOT NULL JOIN LEFT JOIN LIKE LIMIT NOT NOT NULL OR ORDER BY OUTER JOIN PRIMARY KEY PROCEDURE RIGHT JOIN ROWNUM SELECT SELECT DISTINCT SELECT INTO SELECT TOP SET TABLE TOP TRUNCATE TABLE UNION UNION ALL UNIQUE UPDATE VALUES VIEW WHERE MySQL Functions
🌐
DB Vis
dbvis.com › thetable › mysql-nullable-columns-everything-you-need-to-know
MySQL Nullable Columns: Everything You Need to Know
December 3, 2024 - While nullable columns are the ... column to allow NULL values, you can use the ALTER TABLE statement with the MODIFY keyword, followed by the column name, data type, and NULL keyword:...
🌐
Quora
quora.com › When-should-I-use-a-not-null-constraint-in-MySQL
When should I use a 'not null' constraint in MySQL? - Quora
Every column should have the Not Null constraint unless there is a good reason for allowing null. I.e., the default in your mind should always be Not Null so that you have to think about it and why there can be a Null value.
Top answer
1 of 2
7

From the MySQL documentation (emphasis mine):

Declare columns to be NOT NULL if possible. It makes SQL operations faster, by enabling better use of indexes and eliminating overhead for testing whether each value is NULL. You also save some storage space, one bit per column. If you really need NULL values in your tables, use them. Just avoid the default setting that allows NULL values in every column.

So, on the contrary, NOT NULL saves disk space, rather than costing more.

2 of 2
2

Which Engine are you using?

At most, NULLs take 1 byte per 8 nullable columns. In the other direction, a NULL value may occupy less space. What datatype are you talking about?

See also SET. This datatype allows you to put up to 64 boolean flags in up to 8 bytes of space.

Long ago, I decided that the space considerations of NULL versus NOT NULL were so insignificant as to be not worth the time to think about it. I have a Rule of Thumb: If a tentative optimization (NULL, etc) is not going to save 10% of something (space, speed, etc), then drop the topic and look for something else to optimize.

Instead, I focus on using NOT NULL except when I have a business reason or a processing logic that can make good use of NULL:

  • Not yet set (eg, date of death)
  • Optional (eg, middle initial)
  • Not available (eg, middle initial)
  • etc.

I do insist on thinking carefully about datatypes when initially creating a table. It is painful to make schema changes later. Examples:

  • Will this fit into a 2-byte SMALLINT instead of a 4-byte INT. (Smaller saves disk space and helps speed)
  • Virtually all ints can/should be UNSIGNED.
  • Get the CHARACTER SET and COLLATION correct.
  • NULL vs NOT NULL. (For logic reasons, not speed or space)
  • etc.
🌐
CastorDoc
castordoc.com › how-to › how-to-add-a-not-null-constraint-in-mysql
How to Add a NOT NULL Constraint in MySQL?
January 24, 2024 - To ensure that every employee record has a non-null name and age, you can define the "name" and "age" columns with the NOT NULL constraint. This way, whenever you try to insert a new employee record without providing a name or age, MySQL will ...
🌐
OneUptime
oneuptime.com › home › blog › how to add a not null constraint in mysql
How to Add a NOT NULL Constraint in MySQL
3 weeks ago - A NOT NULL constraint prevents a column from storing NULL values. Any attempt to insert or update a row without providing a value for a NOT NULL column (and without a default) results in an error. In MySQL, NOT NULL is part of the column definition.