Skip to content

Disallow empty values for a column in MySQL

To avoid a NULL value in a table column in MySQL we specify NOT NULL constraint when you create a table. But NOT NULL constraint still allows empty values.

To avoid empty values while inserting a record we can have a check in the table DDL itself.

  `store_id` varchar(20) NOT NULL CHECK (store_id <> ''),

When you try to insert a record with store_id as an empty string, you would get the following error.

CONSTRAINT `store_orders.store_id` failed for `storeapp`.`store_orders`

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.