Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


NULL Equals NULL?


NULL Equals NULL?

Author
Message
Mike C
Mike C
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1487 Visits: 1168
Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/mcoles/2829.asp
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22193 Visits: 9671

You either have an error in the Column or in the script. A not null column will NEVER allow a null to be inserted.

Want to See Something Weird?

Now that we've established the exceptions for NULL handling, let's look at something really, really strange. When a NULL value is inserted into a non-nullable column with a check constraint that doesn't check for NULL, all bets are off! Consider Listing 6.

Listing 6. Check constraint exception to the exceptions

USE SSC
GO
CREATE TABLE #test (val INT NOT NULL CONSTRAINT ck_val CHECK(val < 0 AND val = 0 AND val > 0));

INSERT INTO #test (val) VALUES (NULL);
INSERT INTO #test (val) VALUES (NULL);

/*
Serveur : Msg 515, Niveau 16, État 2, Ligne 3
Cannot insert the value NULL into column 'val', table 'tempdb.dbo.#test____________________00000000002D'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Serveur : Msg 515, Niveau 16, État 2, Ligne 4
Cannot insert the value NULL into column 'val', table 'tempdb.dbo.#test____________________00000000002D'; column does not allow nulls. INSERT fails.
The statement has been terminated
*/

SELECT val
FROM #test
ORDER BY val;

DROP TABLE #test;


Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22193 Visits: 9671

Forgot to mention that it's gonna be an excellent refference for nulls from now on .


Mike C
Mike C
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1487 Visits: 1168
Hi Ninja, that was a typo What I get for trying to type at 2 AM with no Mountain Dew in sight I actually submitted a corrected version with a couple of other improvements. I've asked Steve if he'll be kind enough to repost. Thanks!
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22193 Visits: 9671
NP... we all like to have and give accurate info. Let's just hope he'll have time to get it fixed before it goes out the a newsletter.
Vladan
Vladan
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1879 Visits: 754

Nice article. Probably nothing new for those who use SQL for years, but very handy for those new to SQL and confused from the way it is described in BOL or some other book.

So Long, And Thanks For All The NULLs!





Cimode
Cimode
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 11
Obviously the article is posted by somebody who has no clue about database fundamental theory....The writer just made up the *Four Rules* to make it sound scientific but in fact, most of what's written is just a bunch of crappola...

NULLS are the poorest possible way to handle missing data on any direct image system(SQL Server, ORACLE, DB2), which is why they should never be used in the first place. So the message which learn to use them is basically the same as saying: NULLS will mess up your database but here is how to make them less harmful. I say don't use them...

Some proofs of the absurdity of using NULLS in SQL Server.

First, let's create a simple table and fill it with data

create table table1(field1 int, field2 int)
go
insert table1
select 1, 1
insert table1
select 2, 1
insert table1
select 3, 2
insert table1
select 4, NULL

The table has now
field1 field2
1 1
2 1
3 2
4 NULL

Proof1:
run this
now run....

select sum(field1 + field2) from table1

--> it returns 10

then run...
select(field1) + sum(field2) from table1
which should produce the same righ. Wrong!!
--> it return 14 !

So use of NULLS will mess up your sum results...

Proof2:
on the above table run this...

select * table1 where field1 = field2

according to the data, it returns all matching records between field1 and field2...The query returns

field1 field2
1 1

Now run

select * from table1 where field1 <> field2
to return non matching records in column field1 and column of field2...Where on woulmd expect the three last records, the system returns

field1 field2
2 1
3 2

As you can see the system does consider that 4 = NULL a total onsense. 3VL will somebody say but 3VL does not apply in relational modeling. Only 2VL logic applies in Relation Model else it is nothing but relational.

Proof3 (the proof that 3VL logic is nothing but bullshit):
Based on the above Proofs how do we get the entire table?

well basically,

select * from table1

is equivalent to


select * from table1 where field1 = field2
union
select * from table1 where field1 <> field2
union
select * from table1 where field2 is null

which total nonsense...and adds unecessary complexity

select * from table1

should indeed equal to

select * from table1 where field1 = field2
union
select * from table1 where field1 <> field2

why use a logic that breaks pretty much all prequisites that make a system relational, get exposed to false results unless putting tons of IS NULL/IS NOT NULL conditions that will degrade performance by additional index scans when one can do without them.
Cimode
Cimode
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 11
DO NOT PUT NULL VALUES IN THE FIRST PLACE IN YOUR DB.

IGNORE THE WRITER'S ARTICLE.
Vladan
Vladan
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1879 Visits: 754
Well, that's your opinion, Boudjakdji.
Thanks for sharing it, but I don't agree with you. NULL is part of the SQL, and while one should always consider, whether allowing nulls in certain column is a good idea, I fail to see how your post proves that NULLs are bad. It just proves that if you use them wrong, you will have serious problems - but that's true about most things. Also, you did not mention how to treat NULLs that appear in resultsets as a result of OUTER JOIN... but maybe we shouldn't use OUTER JOINs either?



Akash Agarwal
Akash Agarwal
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 22

Thanks for sharing.it is very common one ,but peple even confuses.

it might clear the confusion of those

Akash


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search