MySQL: NULL and ''

Discussion in 'MySQL' started by cancer10, Oct 24, 2008.

  1. #1
    Hi,

    Is there any difference between, NULL and ''?

    For example, consider this query


    select * from employees where username is null
    Code (markup):

    and

    select * from employees where username = '' 
    Code (markup):
    Please explain the diff.


    Thanx
     
    cancer10, Oct 24, 2008 IP
  2. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Null is an undefined value and '' is an empty String.
    Null specifies there is no data stored and '' specifies that the data is an empty string.

    In short they are two different things. If you don't need undefined values then you can instruct the database server to create a column as not null. This forces you to insert data since the absence of data is not accepted.
    Null values can be used for optional information like for example an telephonenumber column, if this is not required information then you can make this a null column.
     
    chisara, Oct 24, 2008 IP
  3. Lord

    Lord Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Lord, Oct 24, 2008 IP
  4. thegussey

    thegussey Peon

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yup it's true. '' means an empty string, so while you said
    select * from table where username is NULL >> it works, the program search where the username was NULL
     
    thegussey, Oct 26, 2008 IP