Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Not Empty instead of Not NULL

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
like image 501
kamaci Avatar asked Oct 31 '11 07:10

kamaci


People also ask

Is not null or empty in SQL?

The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do you select non empty values in SQL?

Select non-empty column values using NOT IS NULL and TRIM() function. The syntax is as follows. SELECT * FROM yourTableName WHERE yourColumnName IS NOT NULL AND TRIM(yourColumnName) <> ' '; You can select non-empty value as well as whitespace from column using the same TRIM() function.

Should not be empty in SQL?

“sql not null or empty” Code Answer'sThe NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

Is it better to use <> or != In SQL?

If != and <> both are the same, which one should be used in SQL queries? Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.


1 Answers

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

like image 176
Avo Muromägi Avatar answered Oct 14 '22 03:10

Avo Muromägi