Questions tagged [null]

A database concept used to represent missing, unknown or inapplicable data.

NULL is a concept in SQL databases (exact implementation can vary) that is used to represent unknown, missing or inapplicable data. Normally it is considered to be a state of the data as opposed to the value of the data.

Ordinary equivalence comparisons do not work with NULL for the above reason. A field IS NULL (correct) as opposed to field = NULL (not correct).

NULL handling can be contentious as some believe NULL has no place in a database, and others use it for business logic. An important distinction is that NULL row values are not considered for most functions such as SUM(col_name), and correct results are obtained if NULL values are used; incorrect results are obtained if "magic" values are used to indicate no data. On some database products, the can perform better if NULL values are used correctly.

NULLs can also be dangerous and lead to unexpected results when used with the NOT IN operator.

A good analogy for the meaning of NULL is:

You are at a party with some people you know and some others you don't.

There are 3 men that you know and 4 men that are strangers. To you at that time, those 4 men have a name of NULL - you know they have names, but you do not know what they are.

If you were asked "Is that person Bill?" the answer would be I don't know - which is what most RDBMS will return as well when asked about a value being equal to NULL.

However, you would not be able to say "His name is not Bill or Jim", which is why NULL will not return TRUE (which in this case means "no match") when used with a NOT IN comparison.

352 questions
161
votes
3 answers

PostgreSQL multi-column unique constraint and NULL values

I have a table like the following: create table my_table ( id int8 not null, id_A int8 not null, id_B int8 not null, id_C int8 null, constraint pk_my_table primary key (id), constraint u_constrainte unique (id_A, id_B,…
Manuel Leduc
  • 1,721
  • 2
  • 11
  • 5
144
votes
8 answers

Why shouldn't we allow NULLs?

I remember reading this one article about database design and I also remember it said you should have field properties of NOT NULL. I don't remember why this was the case though. All I can seem to think of is that, as an application developer, you…
Thomas Stringer
  • 42,434
  • 9
  • 120
  • 155
122
votes
11 answers

When to use NULL and when to use an empty string?

I'm interested mainly in MySQL and PostgreSQL, but you could answer the following in general: Is there a logical scenario in which it would be useful to distinguish an empty string from NULL? What would be the physical storage implications for…
Maniero
  • 2,758
  • 6
  • 28
  • 29
104
votes
8 answers

Does SQL Server read all of a COALESCE function even if the first argument is not NULL?

I'm using a T-SQL COALESCE function where the first argument will not be null on about 95% of the times it is ran. If the first argument is NULL, the second argument is quite a lengthy process: SELECT COALESCE(c.FirstName ,(SELECT…
Curtis
  • 1,265
  • 2
  • 8
  • 9
62
votes
4 answers

What is the difference between select count(*) and select count(any_non_null_column)?

I seem to remember that (on Oracle) there is a difference between uttering select count(*) from any_table and select count(any_non_null_column) from any_table. What are the differences between these two statements, if any?
Martin
  • 2,420
  • 4
  • 26
  • 35
61
votes
5 answers

Why does ALTER COLUMN to NOT NULL cause massive log file growth?

I have a table with 64m rows taking 4.3 GB on disk for its data. Each row is about 30 bytes of integer columns, plus a variable NVARCHAR(255) column for text. I added a a NULLABLE column with data-type Datetimeoffset(0). I then UPDATED this column…
55
votes
7 answers

If a person's name is Null then how would it break the database?

I was reading this article on BBC. It tells a story of a person named Jenifer Null and how she faces day to day problems while using online databases like booking plane tickets, net banking etc. I am not well versed in databases and I do not use it…
Souradeep Nanda
  • 685
  • 1
  • 5
  • 8
44
votes
5 answers

Why does a UNIQUE constraint allow only one NULL in SQL Server?

In SQL Server, technically, NULL = NULL is false. By that logic, no NULL is equal to any NULL and all NULLs are distinct. Shouldn't this imply that all NULLs are unique, and a unique index should allow any number of NULLs?
43
votes
2 answers

NULL or NOT NULL by default?

In MySQL, is it better to always allow nulls unless you know a field is required, or always use Not Null unless you know a field will contain nulls? Or doesn't it matter? I know in some DBMSs they say to use Not Null as much as possible because…
BenV
  • 4,923
  • 7
  • 40
  • 38
31
votes
2 answers

PostgreSQL UPSERT issue with NULL values

I'm having an issue with using the new UPSERT feature in Postgres 9.5 I have a table that is used for aggregating data from another table. The composite key is made up of 20 columns, 10 of which can be nullable. Below I have created a smaller…
30
votes
3 answers

Why does ANSI SQL define SUM(no rows) as NULL?

The ANSI SQL standard defines (chapter 6.5, set function specification) the following behaviour for aggregate functions on empty result sets: COUNT(...) = 0 AVG(...) = NULL MIN(...) = NULL MAX(...) = NULL SUM(...) = NULL Returning NULL for AVG, MIN…
Heinzi
  • 3,210
  • 2
  • 32
  • 43
26
votes
1 answer

Why does NOT IN with a set containing NULL always return FALSE/NULL?

I had a query (for Postgres and Informix) with a NOT IN clause containing a subquery that in some cases returned NULL values, causing that clause (and the entire query) to fail to return anything. What's the best way to understand this? I thought…
newenglander
  • 1,075
  • 5
  • 13
  • 23
26
votes
4 answers

How to exclude NULL values inside CONCAT MySQL?

If I have this - tadd is the Address table: CONCAT(tadd.street_number, ' ', tadd.street_name,', ', tadd.apt_number,', ', tadd.city,', ', tadd.postal_code,', ', tadd.country) AS…
ed-ta
  • 463
  • 2
  • 6
  • 10
21
votes
5 answers

Getting SELECT to return a constant value even if zero rows match

Consider this select statement: SELECT *, 1 AS query_id FROM players WHERE username='foobar'; It returns the column query_id with value 1 along with a player's other columns. How would one make the above SQL return at least the query_id…
Nathanael Weiss
  • 375
  • 2
  • 3
  • 8
20
votes
1 answer

How to index a query with `WHERE field IS NULL`?

I have a table with lots of inserts, setting one of the fields (uploaded_at) to NULL. Then a periodic task selects all the tuples WHERE uploaded_at IS NULL, processes them and updates, setting uploaded_at to current date. How should I index the…
Kirill Zaitsev
  • 303
  • 1
  • 2
  • 7
1
2 3
23 24