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:
- Insert Data into a New Row in MySQL
- Insert Data into a Specific Column
- Insert Data into a Specific Row That Meets a Condition
- Insert Data into a Specific Row That Meets Multiple Conditions
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:
The output is displaying a list of databases:
Select a database. For this post, the name of the database is “linuxhint”:
Run these commands to show all tables available in the database and then select a specific table:
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:
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:
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:
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:
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:
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:
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.