Dark Mode
Image

SQL Tutorial

SQL Database

SQL Injection

PL/SQL Tutorial

Sql Interview Question

SQl Quiz

Change the Column Value

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:

UPDATE Table_Name SET Column_Name = New_Value WHERE Condition;  

The syntax for changing the value of one or more columns in the table is given below:

UPDATE Table_Name SET Column_Name_1 = New_Value_1, Column_Name_2 = New_Value_2, ......, Column_Name_N = New_Value_N WHERE Condition;  

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:

  1. Create a Database.
  2. Create a Table in the database, and Insert the data into the table.
  3. Show the table before value is updated.
  4. Change the value of a column in the table.
  5. 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:

CREATE DATABASE Database_Name;  

The following CREATE command creates the Vehicles database in the SQL database system:

CREATE DATABASE Vehicles;  

Step 2: Create a Table and Insert the data

After database creation, you have to use the following syntax to create the table:

CREATE TABLE table_name  
(  
column_Name_1 data type (size of the column_1),    
column_Name_2 data type (size of the column_2),    
column_Name_3 data type (size of the column_3),    
...    
column_Name_N data type (size of the column_N)  
);    

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:

CREATE TABLE Bikes   
(  
Number Int,  
Model Int,    
Bike_Name Varchar (20),      
Color Varchar (20),    
Price Int,  
Average Int,   
) ;   

After the table creation, you have to insert the data of bikes in the Bikes table using the following query:

INSERT INTO Cars VALUES (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 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:

SELECT * FROM Bikes;  

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:

UPDATE Bikes SET Color = Red WHERE Bike_Name = Apache;  

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:

SELECT * FROM Bikes;  

 

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;

To check the result of the above query, you have to type the following SELECT command in SQL:

SELECT * FROM Bikes;  

 

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 that the color and price of the R15 bike have been successfully changed.

 

Comment / Reply From