Thursday, July 14, 2005
NULL is not nothing
There is a big difference between NULL and nothing. I will demonstrate one notable difference with a simple example.
First, create a table that allows NULLs and has a default value
create table test_table (col_a int default 0 null, col_b int null)
go
Now let's try inserting these rows:
1. A row where we assign neither value:
insert into test_table values ()
go
select * from test_table
go
Note: col_a got the default value, and col_b got NULL.
NULL is like the default value you get when you don't assign a default value to a column. Creating the column like this would be redundant: ... col_b int default null null)
2. A row where we assign NULL values:
insert into test_table (col_a, col_b) values (NULL, NULL)
go
select * from test_table
go
Note: Here, col_a got NULL. It did not use the default value.
Because a value was provided: NULL. NULL is a value.
Related Link: Earlier I wrote a blog about NULLs in Sybase:
http://sybasease.blogspot.com/2005/05/
nulls-in-sybase-ase.html
First, create a table that allows NULLs and has a default value
create table test_table (col_a int default 0 null, col_b int null)
go
Now let's try inserting these rows:
1. A row where we assign neither value:
insert into test_table values ()
go
select * from test_table
go
col_a col_b
----------- -----------
0 NULL
Note: col_a got the default value, and col_b got NULL.
NULL is like the default value you get when you don't assign a default value to a column. Creating the column like this would be redundant: ... col_b int default null null)
2. A row where we assign NULL values:
insert into test_table (col_a, col_b) values (NULL, NULL)
go
select * from test_table
go
col_a col_b
----------- -----------
0 NULL
NULL NULL
Note: Here, col_a got NULL. It did not use the default value.
Because a value was provided: NULL. NULL is a value.
Related Link: Earlier I wrote a blog about NULLs in Sybase:
http://sybasease.blogspot.com/2005/05/
nulls-in-sybase-ase.html