MySQL MariaDB

How do I Insert Data into a Specific Row in MySQL?

MySQL is an RDBMS which stores and manages massive data in an organized form so that data can be managed efficiently. It also provides the facility of updating data using the “UPDATE” query with a “Where” clause that matches a specific condition or condition in the existing records.

This guide will discuss how to:

Prerequisite

To begin with, in this post you should log in to the MySQL server containing databases, and select a database to insert data. After the login, use this command to display the list of all available databases:

show databases;

The output is displaying a list of databases:

Select a database. For this post, the name of the database is “linuxhint”:

use linuxhint;

Run these commands to show all tables available in the database and then select a specific table:

show tables;

select * from employee;

Insert Data into a New Row in MySQL

To insert data into a new row, use the “INSERT INTO” command and specify the table name. Enter the names of columns and their values. For example, if you want to insert a new row in the “employee” table, use this query:

INSERT INTO employee (Id,CompanyName,ContactName,City,Country,Phone)VALUES(7,'Maroon Door','John','London','UK','(000) 123-2531');

After the successful execution of the command “Query Ok” message will display:

To verify if the new row is added or not, use this query:

select * from employee;

The new row is successfully inserted in the table:

Insert Data into a Specific Column

To insert data into a specific column using the “SET” statement. For example, if you want to update the columns “City” and “Country” to a specific value, run this query:

UPDATE employee SET City = 'London', Country = 'UK';

The new data will insert into these columns successfully:

Insert Data into a Specific Row That Meets a Condition

To insert data into a specific row that meets a condition in the “Where” clause. If you want to change the value of “City” and “Country”, “Where” the “id” is equal to “1”, run this query:

UPDATE employee SET City = 'Osaka', Country = 'Japan' where id = 1;

The message “Query OK, 1 row affected” is showing that the data is updated successfully on “1” row. To verify it displays the data of the table by typing:

select * from employee;

The data is inserted successfully:

Insert Data into a Specific Row That Meets Multiple Conditions

The “UPDATE” statement can contain multiple conditions using the Logical operators. If you want to insert data into rows that meet the condition “Where” its id is equal to “2” “AND” the “ContactName” is equal to “Saavedra”, run this query:

UPDATE employee SET City = 'Osaka', Country = 'Japan' where id > 2 AND ContactName = 'Saavedra';

One row meets this specified condition so its values will be updated for “City” and “Country”, to verify the changes display the data of the table:

You have learned how to insert data into a specific row in MySQL.

Conclusion

In MySQL to insert data in a specific row into an existing table use the “UPDATE” statement with the “Where” Clause to specify the condition. MySQL will only Insert data into a row that will fulfill the condition. To define multiple conditions, use the Logical operators. This guide demonstrated the procedure for inserting data into a specific row in MySQL.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.