How to Change the Column Value in SQL
In this article, you will learn how to change the value of the specific column in the Structured Query Language.
The UPDATE command in SQL allows the database users to change the value of the specific column in the table. We can also modify the data of two or more columns using one query.
The syntax for changing the value of a specific column in the table is given below:
The syntax for changing the value of one or more columns in the table is given below:
We have to use the SET keyword in the UPDATE command for modifying the value of the columns. WHERE clause specifies which row you want to change.
If you want to modify the value of the particular column in the table, you have to follow the below five steps one by one in the given order:
- Create a Database.
- Create a Table in the database, and Insert the data into the table.
- Show the table before value is updated.
- Change the value of a column in the table.
- Show the table after value is updated.
Now, we are going to explain each step with an example:
Step 1: Create a Database
In the structured query language, database creation is the first step for storing the structured tables in the database.
Use the following SQL syntax to create a database:
The following CREATE command creates the Vehicles database in the SQL database system:
Step 2: Create a Table and Insert the data
After database creation, you have to use the following syntax to create the table:
Suppose you want to create the Bikes table in the Vehicles database. For this, you have to write the following query in your SQL application:
After the table creation, you have to insert the data of bikes in the Bikes table using the following query:
Step 3: View the Table before updating the values
After table creation and data insertion, you can view the inserted data of the Bikes table by typing the following query in your SQL application:
Output:
Number | Model | Bike_Name | Color | Price | Average | |||||
---|---|---|---|---|---|---|---|---|---|---|
1 | 2019 | Apache | Black | 180000 | 49 | |||||
2 | 2020 | Pulsar | Black | 190000 | 50 | |||||
3 | 2019 | R15 | Blue | 200000 | 45 | |||||
4 | 2020 | Apache | Black | 190000 | 45 | |||||
5 | 2018 | Bullet | Grey | 200000 | 50 | |||||
6 | 2017 | Duke | Yellow | 190000 | 35 | |||||
7 | 2019 | Pulsar | Red | 90000 | 45 | |||||
8 | 2020 | FZ-s | Black | 100000 | 40 | |||||
9 | 2019 | R15 | Orange | 200000 | 40 | |||||
10 | 2020 | Bullet | Black | 190000 | 35 | |||||
11 | 2018 | Duke | Red | 128000 | 30 | |||||
12 | 2020 | Harley Davidson | Black | 400000 | 25 |
Step 4: Change the value of a particular column in the table
If you want to change the Color of any bike, you have to type the following query in SQL:
Step 5: View the Table after updating the values
To check the result of the query executed in the 4th step, you have to type the following SELECT command in SQL:
Number | Model | Bike_Name | Color | Price | Average | |||||
---|---|---|---|---|---|---|---|---|---|---|
1 | 2019 | Apache | Black | 180000 | 49 | |||||
2 | 2020 | Pulsar | Black | 190000 | 50 | |||||
3 | 2019 | R15 | Blue | 200000 | 45 | |||||
4 | 2020 | Apache | Black | 190000 | 45 | |||||
5 | 2018 | Bullet | Grey | 200000 | 50 | |||||
6 | 2017 | Duke | Yellow | 190000 | 35 | |||||
7 | 2019 | Pulsar | Red | 90000 | 45 | |||||
8 | 2020 | FZ-s | Black | 100000 | 40 | |||||
9 | 2019 | R15 | Orange | 200000 | 40 | |||||
10 | 2020 | Bullet | Black | 190000 | 35 | |||||
11 | 2018 | Duke | Red | 128000 | 30 | |||||
12 | 2020 | Harley Davidson | Black | 400000 | 25 |
As we can see, the color of Apache Bike has been successfully changed in the Cars table.
Change the value of Multiple Columns in the table
If you want to update the values of multiple columns in the Bikes table, then you have to write the below query in SQL:
UPDATE Bikes SET Color = Green, Price = 90000 WHERE Bike_Name = R15;