January 25, 2007 at 9:20 am
Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/mcoles/2829.asp
January 25, 2007 at 9:41 am
You either have an error in the Column or in the script. A not null column will NEVER allow a null to be inserted.
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;
January 25, 2007 at 9:43 am
Forgot to mention that it's gonna be an excellent refference for nulls from now on .
January 25, 2007 at 2:03 pm
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!
January 25, 2007 at 2:25 pm
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.
February 26, 2007 at 1:05 am
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!
February 26, 2007 at 2:50 am
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.
February 26, 2007 at 2:56 am
DO NOT PUT NULL VALUES IN THE FIRST PLACE IN YOUR DB.
IGNORE THE WRITER'S ARTICLE.
February 26, 2007 at 3:11 am
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?
February 26, 2007 at 3:35 am
Thanks for sharing.it is very common one ,but peple even confuses.
it might clear the confusion of those
Akash
February 26, 2007 at 5:51 am
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...
February 26, 2007 at 5:54 am
//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.
February 26, 2007 at 6:15 am
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.
February 26, 2007 at 6:41 am
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.
February 26, 2007 at 6:56 am
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