How to update data with a primary key in MySQL?

To update existing data in a MySQL table, you need to use the UPDATE statement. If the table has a primary key, you can use the primary key to locate the data that needs to be updated.

Here is the basic syntax for updating existing data:

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

In the specified scenario, table_name is the name of the table to be updated, column1, column2, etc. are the names of the columns to be updated, value1, value2, etc. are the new values to be updated, and condition is used to locate the data that needs to be updated.

For example, suppose there is a table named users with columns id, name, and age, where id is the primary key. Now, if we want to update the name and age of the user with id 1 to “John” and 25, we can use the following statement:

UPDATE users
SET name = 'John', age = 25
WHERE id = 1;
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds