×

SQL UPDATE

SQL UPDATE

The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables in the database. This command is worked along with WHERE clause. The condition specified in the UPDATE statement is used to decide which columns or rows of the table will be affected or modified.

The UPDATE command can also be used to update a table by another table. It can be used to update the date and time of a SQL query as well.

SQL UPDATE JOIN

The SQL UPDATE statement can also be utilized to update one table using another table which are connected by a join. This is known as the SQL UPDATE JOIN statement.

UPDATE table1, table2,
 INNER JOIN table1
 ON table1.column1 = table2.column1 
 SET table1.column1 = table2.column2
 WHERE condition

The following is the basic syntax of the UPDATE statement using the JOIN clause:

Example:

CREATE TABLE TAE1 (Col1 INT, Col2 INT, Col3 VARCHAR (100))  
 INSERT INTO TAE1 (Col1, Col2, Col3)  
 SELECT 1, 11, 'FIRST'  
 UNION ALL  
 SELECT 11,12, 'SECOND'  
 UNION ALL   
 SELECT 21, 13, 'THIRD'  
 UNION ALL   
 SELECT 31, 14, 'FOURTH'   

Let’s first create the first table TAE1.

 CREATE TABLE TAE2 (Col1 INT, Col2 INT, Col3 VARCHAR (100))  
 INSERT INTO TAE2 (Col1, Col2, Col3)  
 SELECT 1, 21, 'TWO-ONE'  
 UNION ALL  
 SELECT 11,22, 'TWO-TWO'  
 UNION ALL   
 SELECT 21, 23, 'TWO-THREE'  
 UNION ALL   
 SELECT 31, 24, 'TWO-FOUR'   

Now, let’s create the second table TAE2.

SELECT *
 FROM TAE1 
Col1Col2Col3
111First
1112Second
2113Third
3114Fourth

Output:

Now let’s see the contents of the table TAE2.

SELECT *
 FROM TAE2

Output:

Col1Col2Col3
121Two-one
1122Two-two
2123Two-three
3124Two-four

Now the following query will update the rows of TAE1 where the value of Col1 is 21 and 31 using the table TAE2 where there are similar rows and where Col1 is 21 and 31. Only the corresponding records of Col2 and Col3 of TAE1 table will be updated.

UPDATE TAE1
 SET Col2 = TAE2.Col2,  
 Col3 = TAE2.Col3  
 FROM TAE1  
 INNER JOIN TAE2 ON TAE1.Col1 = TAE2.Col1  
 WHERE TAE1.Col1 IN (21, 31); 

Output:

Col1Col2Col3
111First
1112Second
2113Two-three
3114Two-four

Now, if the contents of the table TAE1 is checked, the following output will be obtained.

However, the contents of TAE2 remains unchanged.

Col1Col2Col3
121Two-one
1122Two-two
2123Two-three
3124Two-four

This is an example of using the JOIN clause with the UPDATE statement. It has merged the above two tables.

SQL UPDATE DATE

The SQL UPDATE DATE statement is used to update the date and time field in SQL.

UPDATE table_name
 SET data_field = ‘data_value’ 
 WHERE conditions; 

The following is the general syntax of updating the date and time field in SQL:

UPDATE table_name
 SET data_field = getdate(); 

The following is the syntax of updating the date with the current date in SQL:

UPDATE table_name
 SET data_field = CURRENT_TIMESTAMP; 

The following is the syntax of updating the date and time with the current date and time in SQL:

 UPDATE table_name
 SET data_field = ‘YYYY-MM-DD HH:MM:SS’;

The following is the syntax of updating the date and time with a specific date and time in SQL:

UPDATE table_name
 SET data_field = CAST(‘date_value’ AS DATETIME); 

The following is the syntax of updating the date with a specific value when the format of the date is not known:

Example:

UPDATE Employee
 SET DOJ = ‘2021-07-05’ 
 WHERE Dept_ID = 10; 

The following is an example of updating the date in multiple rows in the given Employee table.

Emp_IDEmp_NameDesignationManager_IDDOJSalaryDept_ID
1Emp1Director 2021-07-114500010
2Emp2Director 2021-07-114000020
3Emp3ManagerEmp12021-07-112700010
4Emp4ManagerEmp22021-10-082500020
5Emp5AnalystEmp32021-07-112000010
6Emp6AnalystEmp32021-10-081800010
7Emp7ClerkEmp32021-07-111500010
8Emp8SalesmanEmp42021-09-091400020
9Emp9SalesmanEmp42021-10-081300020

Query:

Output:

Emp_IDEmp_NameDesignationManager_IDDOJSalaryDept_ID
1Emp1Director 2021-07-054500010
3Emp3ManagerEmp12021-07-052700010
5Emp5AnalystEmp32021-07-052000010
6Emp6AnalystEmp32021-07-051800010
7Emp7ClerkEmp32021-07-051500010

Thus, the DOJ column for all the employees having Dept_ID 10 has been updated.

The following is the example of updating the admission date in a student table using the CAST function:

UPDATE student
SET admission_date = CAST(‘2021-04-10’ AS DATETIME)
WHERE id = 42;

Related Topics

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.