Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is NULL value in MySQL?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 589
    Comment on it

    When we don't define any data for a column then the column holds the NULL value. In MySQL, NULL values are used to represent data that is missing.

    NULL Values

    If you have created a table with optional columns then you can insert and update records for that table without inserting/updating data into optional columns in that case those columns will be saved with NULL value.

    Example:

    We have a table "user" as below:

    user
    
    id  first_name            last_name       country
    .......................................................
    1   John                     Simp         Canada
    2   Chris                    Hely         Korea
    3   Joy                      Roy          
    4   Jenny                    Mill     
    

    Suppose that the "country" column into the "user" table is optional, that means if we insert a new record with no value in "country" column then NULL value will be saved in "country" column.

    Now the main question is how can we test column for NULL values? We can not test for NULL values with comparison operators (=,<,<>). For that we'll have to use IS NULL and IS NOT NULL operators provided by MySQL.

    IS NULL

    Suppose we want to select the records with NULL values only in the "country" column, for that we'll use IS NULL operator as below:

    SELECT first_name,last_name,country FROM user
    WHERE country IS NULL;
    

    Result:

    first_name      last_name      country
    
    ........................................................
    
    Joy             Roy            
    Jenny           Mill   
    

    IS NOT NULL

    Suppose we want to select the records with no NULL values in the "country" column, for that we'll use IS NOT NULL operator as below:

    SELECT first_name,last_name,country FROM user
    WHERE country IS NOT NULL;
    

    Result:

    first_name      last_name      country
    ..............................................
    John            Simp           Canada
    Chris           Hely           Korea
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: