Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > Null Or ''

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-01-07, 17:21
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 69
Null Or ''

hi
i have a table with about 500,000 record, i want to add a new field to table, & for this 500,000, this field is empty, from now new field will have value.
i dont know which one is more optimized, set default value of new field to NULL, OR '' ?
Reply With Quote
  #2 (permalink)  
Old 10-01-07, 17:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
use null
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 10-02-07, 05:51
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
I also find it more helpful to use NULL just incase you need to utilise the COALESCE feature of SQL as it won't work with blank strings '' , unless you write something like :
Code:
COALESCE( CASE WHEN newfield = '' THEN NULL ELSE newfield END ,oldfield)

If you didn't do something like the above then you will get '' instead of the oldfield value.

If you use nulls you can do something a little easier and less prone to mistakes, happy in the knowledge you're going to get the intended result :
Code:
COALESCE(newfield,oldfield)

I know which is easier
Reply With Quote
  #4 (permalink)  
Old 10-02-07, 06:21
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,144
Here's an interesting link for you aschk

And for my 2 cents - go with NULLs all the way! A NULL value takes up no memory, it's the absence of data! NULLs are unknowns - therefore it is correct to use NULLs where you don't know the value of an attribute
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #5 (permalink)  
Old 10-02-07, 10:26
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Nice one george, i've used IFNULL before and seen NULLIF, just never had a use for it. Now I do!
Thanks
Reply With Quote
  #6 (permalink)  
Old 10-15-07, 03:39
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
Quote:
A NULL value takes up no memory, it's the absence of data!

According to the MySQL manual a tinyint takes 1 byte and can be any number from 0 to 255 or -127 to 128.

A byte being 8 bits, you can only store 256 different values in it. But 0 to 255 or -127 to 128 is already 256 values. Add in NULL as an alternative, and you have 257. Therefore, a tinyint column must require an extra bit *somewhere*.

Quote:
NULLs are unknowns - therefore it is correct to use NULLs where you don't know the value of an attribute

No, the correct thing isn't supported by SQL DBMSs. NULLs are fine in relational values, that is, results returned by a query. In practice, you should use them sparingly in your schema because they make queries unpredictable. The reason why follows...

Base relational variables, tables in SQL-speak, should have predicates. The predicate is the logical statement that declares the semantics of the table. A list of employees, for example, might say "An employee with SSN (ssn), first name (fname), etc, works at this company." Each row of the table is a proposition, that is it corresponds to filling in the blanks.

Put all the rows together and you have a logical statement.
Without nulls, the logic is simple: every record in the table represents the true propositions and every record that *could* be in the table but isn't represents the false propositions. So if I know that an employee with SSN 123-45-6789 has the first name Bob, etc, I know that there is definitely *not* an employee with SSN 123-45-6789 with the name John.

With nulls, you now have to contend with the notion of propositions that have the unknown truth value, but there are infinitely (strictly, a really huge finite number) many other unknown propositions that aren't listed. So now, what does the table mean? How can you reason based on it? The answer is that you can't. If you ask the database to count the number of employees whose name starts with A, it's going to happily return a number that might be wrong if someone's name is unknown. In other words, after all the work of designing that database, you now have something that spits out very official looking garbage.
Reply With Quote
  #7 (permalink)  
Old 10-15-07, 04:42
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,144
Quote:
Originally Posted by sco08y
A byte being 8 bits, you can only store 256 different values in it. But 0 to 255 or -127 to 128 is already 256 values. Add in NULL as an alternative, and you have 257. Therefore, a tinyint column must require an extra bit *somewhere*.
A NULL is the absence of data... (0 bytes)
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #8 (permalink)  
Old 10-15-07, 07:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Quote:
Originally Posted by sco08y
If you ask the database to count the number of employees whose name starts with A, it's going to happily return a number that might be wrong if someone's name is unknown.
no

this is always where the over-analyzing theorists fall on their faces

the number of employees whose name starts with an A is correct, even if there is an employee whose name is NULL in the database

the real WTF here is that the database professional did not insist that the employee name be declared NOT NULL

a gun in the hands of a child can cause havoc, but it's not the child's fault

use NULL wisely

end of
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On