Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

NULL Equals NULL? Expand / Collapse
Author
Message
Posted Thursday, January 25, 2007 9:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/mcoles/2829.asp
Post #339807
Posted Thursday, January 25, 2007 9:41 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 2:45 AM
Points: 21,397, Visits: 9,610

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;

Post #339818
Posted Thursday, January 25, 2007 9:43 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 2:45 AM
Points: 21,397, Visits: 9,610

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

Post #339819
Posted Thursday, January 25, 2007 2:03 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
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!
Post #339916
Posted Thursday, January 25, 2007 2:25 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 2:45 AM
Points: 21,397, Visits: 9,610
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.
Post #339921
Posted Monday, February 26, 2007 1:05 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, May 5, 2011 1:38 AM
Points: 1,636, Visits: 604

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!




Post #347346
Posted Monday, February 26, 2007 2:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 11, 2011 3:56 PM
Points: 33, 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.




Post #347368
Posted Monday, February 26, 2007 2:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 11, 2011 3:56 PM
Points: 33, Visits: 11
DO NOT PUT NULL VALUES IN THE FIRST PLACE IN YOUR DB.

IGNORE THE WRITER'S ARTICLE.
Post #347371
Posted Monday, February 26, 2007 3:11 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, May 5, 2011 1:38 AM
Points: 1,636, Visits: 604
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?



Post #347375
Posted Monday, February 26, 2007 3:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, May 9, 2011 2:56 AM
Points: 131, Visits: 22

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

it might clear the confusion of those

Akash

Post #347377
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse