|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:22 PM
Points: 1,276,
Visits: 1,112
|
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 3:18 AM
Points: 21,359,
Visits: 9,541
|
|
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;
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 3:18 AM
Points: 21,359,
Visits: 9,541
|
|
Forgot to mention that it's gonna be an excellent refference for nulls from now on .
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:22 PM
Points: 1,276,
Visits: 1,112
|
|
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!
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 3:18 AM
Points: 21,359,
Visits: 9,541
|
|
| 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.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 05, 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!
|
|
|
|
|
SSC 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.
|
|
|
|
|
SSC 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.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 05, 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?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, May 09, 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
|
|
|
|