NULL Equals NULL?

  • Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/mcoles/2829.asp

  • 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;

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

  • 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!

  • 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.

  • 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!

  • 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

    11

    21

    32

    4NULL

    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

    11

    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

    21

    32

    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.

  • DO NOT PUT NULL VALUES IN THE FIRST PLACE IN YOUR DB.

    IGNORE THE WRITER'S ARTICLE.

  • 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?

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

    it might clear the confusion of those

    Akash

  • Well, that's your opinion, Boudjakdji.

    <>

    If you don't see what wrong in the absurd results returned by the queries, I am afraid I can not do much for you.

    <>

    If you have NO NULLS in the first place, you won't have to deal with all these problems...

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

    it might clear the confusion of those//

    People do not confuse, people are either ignorant or misled by ignorants. That the case of the person who wrote this stupid article.

  • Well everyone has the right to his own opinion.

     

    Can you take 5 minutes of your time and write an article that will "correct" the situation and share it with the world... you will also be paid for sending the article in.

  • One thing that frustrates me about how SQL Server handles NULL within unqiue constraints is that it does only allow one NULL value (as the article does a nice job of illustrating).  It makes sense that it would allow >1 NULL value because they are in fact different values.  Other RDBMS do allow this, so I kinda wish that SQL Server did also.

  • There's a workaround on this one... but it comes with a cost :

    CREATE VIEW dbo.vwTblNameForceUnique

    WITH SCHEMABINDING

    AS

    SELECT PrimaryKey, UniqueCol

    FROM dbo.TblName

    WHERE UniqueCol IS NOT NULL

    GO

    --Create clustered index for the view

    GO

    CREATE UNIQUE INDEX IX_vwTblNameForceUnique_UniqueCol ON dbo.vwTblNameForceUnique (UniqueCol)

    GO

Viewing 15 posts - 1 through 15 (of 117 total)

You must be logged in to reply to this topic. Login to reply