DATA with BARAA

SQL Update

SQL UPDATE

In this tutorial, you will learn how to modify your existing rows in the database table using the SQL UPDATE statement.

Syntax

The basic syntax of the UPDATE statement to modify rows in table, can be given with:

				
					UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition
				
			

Examples

To understand the UPDATE statement in a better way, let’s look at the following customers and orders tables in our tutorial database:

Now, let’s check out some examples that demonstrate how it actually works.

We have the following task be to solve using SQL statements

Change the country of customer ID 7 to Germany

The following Query will returns all the rows from customers table.

				
					UPDATE customers
SET country = 'Germany'
WHERE customer_id = 7
				
			

After executing the above query, you’ll get the result set something like this:

Change the score of the customer Anna to 100 and change her country from UK to USA
				
					UPDATE customers
SET country = 'USA',
    score = 100
WHERE customer_id = 6
				
			

As you can see the output contains everything the whole customers tables including all rows and columns.

Let’s say we are only interested in getting only specific columns from a table, then we could use the following  syntax of the SELECT statement:

We have the following task to be solve using SQL statements

Increase the score of the all customer by adding 50 points

In the task we dont reuqire all the data, we need only specific columns. The following SQL statement selcts only first_name and country from table customers.

				
					UPDATE customers
SET score = score + 50
				
			

After executing the above query, you’ll get the result set something like this:

As you can see the result contains only the columns that we specified after SELECT.

SELECT * helps you to examine the content of table that you are not familiar with. But be careful using it specially with big tables because database will retrieve everything which costs a lot of data movement across the network and might slow down the application

Share it !
Exit mobile version