1

I'm developing my first ever application with PostgreSQL.

The Scenario

This is what my table "person" looks like:

   Column   |            Type             |                      Modifiers
------------+-----------------------------+-----------------------------------------------------
 id         | bigint                      | not null default nextval('person_id_seq'::regclass)
 first_name | character varying(255)      | not null
 last_name  | character varying(255)      | not null
 email      | character varying(255)      | not null
 password   | character varying(255)      | not null
 created_at | timestamp without time zone |
 updated_at | timestamp without time zone |
Indexes:
    "person_pkey" PRIMARY KEY, btree (id)
    "person_email_unique" UNIQUE CONSTRAINT, btree (email)
    "person_id_unique" UNIQUE CONSTRAINT, btree (id)
Referenced by:
    TABLE "access" CONSTRAINT "access_person_id_foreign" FOREIGN KEY (person_id) REFERENCES person(id)

This was created using migrations in knex.schema.

If I run the following query in psql...

insert into person (first_name, last_name, email, password) values ('Max', 'Mustermann', '', '123123123');

I get back INSERT 0 1 and the row is successfully inserted:

id | first_name | last_name  |               email               |    password    |       created_at        |       updated_at
----+------------+------------+-----------------------------------+----------------+-------------------------+-------------------------
12 | Max        | Mustermann |                                   | 123123123      |                         |

My Question:

I expect the operation to fail, because no e-mail (NOT NULL) was specified. Why does it not fail?

Thank you very much for your help!

Max

5
  • null and '' are not the same thing. If you excluded email from your insert completely it would throw an error Commented Apr 29, 2014 at 0:46
  • The empty string is not the same as NULL (except in Oracle). Commented Apr 29, 2014 at 0:46
  • 2
    If you want to disallow empty strings too then add check(email <> '') or check(length(email) > 0). Commented Apr 29, 2014 at 1:23
  • "lack of a value" is not the same thing as "a value of zero"; similarly, "lack of an answer" is not the same thing as "an answer of no" en.wikipedia.org/wiki/Null_(SQL) Commented Apr 29, 2014 at 8:39
  • Ah, I see, for some reason I thought '' was NULL in SQL. Thanks for your help, guys! Commented Apr 29, 2014 at 9:50

3 Answers 3

8

Some DBMS (like Oracle) treats empty string ('') as NULL. Others (like MySQL, PostgreSQL, etc) treat empty string and NULL as different.

PostgreSQL treats '' as empty string, not NULL, so your insert statement executed successfully.

Sign up to request clarification or add additional context in comments.

1 Comment

But, postgreSQL will display a NULL where in fact the value is '' if the column type is string
2

null and an empty string are not the same values, by passing in an empty string you have satisfied the requirement.

If you query was

insert into person (first_name, last_name, password) 
values ('Max', 'Mustermann', '123123123');

then an error would be thrown because you are not passing in a value for email

Comments

1

Because you didn't pass a null value. You passed an empty string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.