SQL Not Empty instead of Not NULL

kamaci picture kamaci · Oct 31, 2011 · Viewed 20.5k times · Source

I am using postgreSQL. I have a column that:

NOT NULL

However when I want to insert a row with an empty string as like:

''

it doesn't give me an error and accepts. How can I check insert value should be not empty? (Neither empty nor null)

PS: My column defined as:

"ads" character varying(60) NOT NULL

Answer

Avo Muromägi picture Avo Muromägi · Oct 31, 2011

Add a constraint to column definition. For example something like:

ads character varying(60) NOT NULL CHECK (ads <> '')

For more, see http://www.postgresql.org/docs/current/static/ddl-constraints.html