Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to delete data from a table in MySQL?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.84k
    Comment on it

    To delete data from a table we can use either DELETE or TRUNCATE.

    DELETE

    If we want to delete a record from table then we use DELETE FROM statement.

    Syntax:

    Below statement deletes rows from table on the basis of where condition, we can define any condition in the WHERE clause:

    DELETE FROM table_name [WHERE Clause]
    

    If WHERE clause is not specified with the statement then all the records will be deleted from the given table.

    Example:

    We have a table with the following values.

        user
    
    id  user_name     country   
    .......................................
    1   John           Canada     
    2   Chris          America        
    3   Joy            London         
    4   Jenny          Korea      
    

    DELETE Examples

    Delete the user where id is 4:

     DELETE FROM user where id = 4;
    

    Result

    user
    
    id  user_name     country   
    .......................................
    1   John           Canada     
    2   Chris          America        
    3   Joy            London
    

    TRUNCATE

    The TRUNCATE TABLE Statement

    When we want to delete all the records inside a table then, use the TRUNCATE TABLE statement.

    Syntax:

    TRUNCATE TABLE table_name;
    

    Example:

    The below statement deletes all the records of "user" table:

    TRUNCATE TABLE user;
    

    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: