The SQL UPDATE Statement is used to modify the existing records in a table. This statement is a part of Data Manipulation Language (DML), as it only modifies the data present in a table without affecting the table’s structure.
To filter records that needs to be modified, you can use a WHERE clause with UPDATE statement. Using a WHERE clause, you can either update a single row or multiple rows.
Update table_name set column1=value1, column2=value2…columnN=valueN where (Condition);
Example:
update Customer_Data set Cust_Country=’Afganisthan’ where Cust_Name=’Chaitanya’;
Customer_Data database is shown below below updating details
Update has been done in Customer country where customer name is ‘Chaitanya’.
-
The SQL DELETE Statement
The SQL DELETE Statement is used to delete the records from an existing table. In order to filter the records to be deleted (or, delete particular records), we need to use the WHERE clause along with the DELETE statement.
If you execute DELETE statement without a WHERE clause, it will delete all the records from the table.
Using the DELETE statement, we can delete one or more rows of a single table and records across multiple tables.
Syntax:
The basic syntax of the SQL DELETE Query with the WHERE clause is as follows –
Delete from table_name where (Condition);
You can combine N number of conditions using AND or OR operators.
Example:
delete from Customer_Data where Cust_Name=’Umesh’ or Cust_Salary=12000;
Customer database is shown below before deleting record
Record with Name ‘Umesh’ and with Salary ‘12000’ has been deleted with Delete function and the output has been shown below.
The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
Syntax:
Select top N * from table_name;
Note:Not all databse systems support the Select Top clause.MySql supports the LIMIT clause to select a limited number of records,while Oracle used Fetch First n rows only and Rownum.
Example:
select * from Customer_Data where rownum<=5;
Customer_Data database consists of 12 records before fetching first N records
Screen shot given below after fetching first 5 records using Rownum function