Update, Delete and Truncate Commands

Hello, and welcome to the blog. In this blog we are going to talk about "Updating and deleting data".

We learn about "Inserting and Retrieving Data" in this blog. Now its time to learn updating and deleting our mistakes. Yes with the help of the UPDATE and DELETE statements you can actually update and delete wrong entries in your database respectively.

So in this blog we are going too talk about 

  1. UPDATE statement
  2. DELETE statement
  3. TRUNCATE command
Let's start with

1) UPDATE statement :

  1. UPDATE statement is used to update a specific record or a bunch of records which exists in your database table.
  2. If you made a mistake while inserting data in your database then UPDATE statement is the loophole through which you can correct it.
  3. Let's see a example:
    All available data
  4. If you can see in our table I have made a mistake that SALARY  of "Sales Person" and "Sales Manager" is same in "Sales" department. It never happens, "Sales Manager" is always get paid more than a "Sales Person".
  5. So, let's correct the mistake, but first let's see syntax for UPDATE statement:
  6. Syntax: 
    UPDATE table_name
    SET column_name = new_value
    WHERE condition
  7. Example:
    UPDATE Employee
    SET salary = 25000
    WHERE Id=2
    Update successful message
    Updated record
  8. In the whole UPDATE statement the most important part is the "WHERE" clause. If you miss "WHERE" clause then you will end up updating all the rows with current value that you have specified in you UPDATE statement.
  9. I have personally been a victim of this scenario. Once I was trying to update a record and then I end up updating all records of that table and that table was having almost 6000 records. After that I always write "WHERE" clause first.
  10. So make sure of writing where clause.

2) DELETE statement:

  1. DELETE statement is pretty obvious. It is used for deleting records from you database table. It is pretty simple as well.
  2. Syntax:
    DELETE from table_name
    WHERE condition
  3. Example:
    DELETE from Employee
    WHERE Id=8
    Delete successful message
    Deleted record
  4. As a told you the "WHERE" clause is most important in UPDATE statement same applicable for DELETE statement for obvious reasons.
  5. Now a days DELETE command is not used that frequently or you used it if there is big issue that can be solved by a DELETE otherwise you always have a column in your table which defines a record is deleted or not. If that column contain value '1' then its not deleted, if column contains value '0' then it is said to be deleted. This we will discuss more in further blogs.

3) TRUNCATE statement:

  1. We just came to the last topic of our blog TRUNCATE statement.
  2. This statement is used to delete all the records from a table.
  3. When you want to delete all the data from your table you will use this command.
  4. Syntax : TRUNCATE table table_name
  5. Example : TRUNCATE table Employee1
    Truncate successful message
    Truncated table
Congratulations, if you you have followed the blog then now you can create a database, create a table, insert data, update data, retrieve data and delete data. All together you can perform CRUD operations(Create Read Update Delete). See you  in some another blog till then "Keep coding, keep learning because trust me you can make it big".

"Have a great day"

Comments

Post a Comment

Popular posts from this blog

SQL Joins | Inner Join | Left Join | Right Join | Full Join | Self Join

More clauses in SQL