CodesJava

Easy learning with example program codes

UPDATE Query in MariaDB

MariaDB UPDATE
To modify the existing records in a table by changing their values, the MariaDB UPDATE statement is used.

Syntax:

UPDATE table_name
SET column_1 = value_1,  
    column_2 = value_2,  
    ...  
    column_n = value_n  
WHERE conditions;  

Parameters:
value_1, value_2, … value_n: It is used to specify the values to be updated to the respective columns.
conditions: It is used to specify the conditions to be strictly satisfied for update.

Example 1: Updating a single column of a table.
Players table before update:

ID	NAME	         SPORTS
1	Sachin	         Cricket
2	Dhoni	         Cricket
3	Sunil	         Football
4	Srikanth	 Badminton
5	Mary	         Boxing
UPDATE Players  
SET name = 'Virat'  
WHERE id = 2;

Explanation:
In the above example, the ‘name’ column will be updated as ‘Virat’ where ‘id’ is 2. Check the output using the SELECT statement.
SELECT * FROM Players;

Players table after update will be like this:

ID	NAME	        SPORTS
1	Sachin	        Cricket
2	Virat	        Cricket
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing

Example 2: Updating multiple columns of a table.
Players table before update:

ID	NAME	        SPORTS
1	Sachin	        Cricket
2	Dhoni	        Cricket
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing
UPDATE Players  
SET name = 'Virat', sports = ‘Cricket’  
WHERE id = 5;

Explanation:
In the above example, the ‘name’ column will be updated as ‘Virat’ as well as the ‘sports’ column will be updated as ‘Cricket’ where ‘id’ is 5. Check the output using the SELECT statement.
SELECT * FROM Players;

Players table after update will be like this:

ID	NAME	         SPORTS
1	Sachin	         Cricket
2	Dhoni	         Cricket
3	Sunil	         Football
4	Srikanth	 Badminton
5	Virat	         Cricket

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status