Site icon Software Testing Class

SQL UPDATE and DELETE

SQL update and delete query

In the SQL server, the SQL DELETE & UPDATE keywords are more frequently used after the SQL SELECT keyword.

What is the UPDATE keyword?

The UPDATE command is used to modify the records in the table. Upon executing this command the record values are modified based on values passed in the query. Along with WHERE clause you can update the specific records from the table.

SQL UPDATE Syntax:
Let’s see the basic syntax of DELETE command:

UPDATE Table_Name
SET ‘Column_Name’ = ‘New_value’
[WHERE CONDITION];

So here we see what use of each object:

Let’s go ahead & learn to update queries with practical examples. In the Employee table, each record has a Phone & one Employee wants to change the phone number in the employee record & the second employee wants to update the Full name. So let’s see how to update the Phone number using the UPDATE query.

SELECT * FROM Employee WHERE Employee_Id IN (1,2);
Employee_IdFull_NameGenderDate_Of_BirthPhone

1

Tanya SmithFemale1982-09-309123456789

2

Janet TampiFemale1980-07-109876543210

3

Nilsen PhilMaleNULL9112233445

4

Nilsen JonesMale1983-09-179988776655

5

Pettersen WillamsMale1980-07-129944552299

6

Angels DavidMale1981-02-109955566611

The Employee “Tanya Smith” wants to change phone number from “9123456789” to “9198657777” & “Janet Tampi” want to update the Full name to “Janet Tampi – Simson” & Date of birth to “1980-07-10”, so you can use the following update queries to modify the records.

UPDATE Employee
SET Phone = ‘9198657777’
WHERE Employee_Id  = 1;
UPDATE Employee
SET Full_Name = ‘Janet Tampi - Simson’, Date_Of_Birth = ‘1980-08-10’
WHERE Employee_Id  = 2;

Note: If you want to update the multiple values in the same condition then you can use the comma operator in the “SET” & pass the multiple values.

Executing the above script the values in the Employee table are updated based on the values specified in the update query.

SELECT * FROM Employee WHERE Employee_Id IN (1,2);
Employee_IdFull_NameGenderDate_Of_BirthPhone

1

Tanya SmithFemale1982-09-309198657777

2

Janet Tampi – SimsonFemale1980-07-109876543210

3

Nilsen PhilMaleNULL9112233445

4

Nilsen JonesMale1983-09-179988776655

5

Pettersen WillamsMale1980-07-129944552299

6

Angels DavidMale1981-02-109955566611

What is the SQL DELETE keyword?

As it name suggests that the DELETE keyword is used to delete the record(s) from the database when they are no longer be used. Using the DELETE keyword you can delete single or multiple records from the database. So this command is used more powerfully with WHERE keyword while removing the unnecessary records from the database.

Upon executing this command the deleted data cannot be recovered, so prior to executing this command you should make sure with where condition in the query is correct. To be on the safer side, take the backups of the database or tables from where you are executing the delete query, so it will help you to compare the records after executing the query.

SQL DELETE Syntax
Let’s see the basic syntax of DELETE command:

DELETE from Table_Name
[WHERE CONDITION];

In the above case, we have specified WHERE condition, so the records will be deleted from the table based on the criteria you specified.

You can also use the following command to delete all the data from the table, so the only difference is don’t specify the WHERE condition:

DELETE from Table_Name;

So here we see what use of each object:

Let’s see upon executing following query on Employee table resulting following list:

SELECT * FROM Employee

Following is the result after executing the above SQL query:

Employee_IdFull_NameGenderDate_Of_BirthPhone

1

Tanya SmithFemale1982-09-309123456789

2

Janet TampiFemale1980-07-109876543210

3

Nilsen PhilMaleNULL9112233445

4

Nilsen JonesMale1983-09-179988776655

5

Pettersen WillamsMale1980-07-129944552299

6

Angels DavidMale1981-02-109955566611

Let take an example of Employee where the “Nilsen Jones” employee left the company and they want to remove the employee record from the database. Its Employee_Id is 4, so we can write down the delete query on Employee table where pass the Employee_Id in the WHERE condition to delete the specific employee record.

DELETE FROM Employee WHERE Employee_Id = 4;

Once we execute the above script then the “Nilsen Jones” record will be deleted from Employee list and Employee table will look like as follows:

Employee_IdFull_NameGenderDate_Of_BirthPhone

1

Tanya SmithFemale1982-09-309123456789

2

Janet TampiFemale1980-07-109876543210

3

Nilsen PhilMaleNULL9112233445

5

Pettersen WillamsMale1980-07-129944552299

6

Angels DavidMale1981-02-109955566611

If we check the employee id record 4, then this record is deleted from the Employee table & not returned in the result set. Also, keep in mind the column can be deleted using delete command, the entire row will be deleted.

If you want to delete multiple records from the table then this can be accomplished using IN wildcard. Only you have to provide the list of record which you want to delete in WHERE clause.

DELETE FROM Employee WHERE Employee_Id IN (3,5);

Once we execute the above query then the employee IDs 3 & 5 entries from the employee table will be removed.

Summary


⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 
Exit mobile version