MySQL is a very popular relational database management system. It is widely used in various fields, such as business management, e-commerce, social networks, etc. Since MySQL supports efficient data query and management, MySQL is an excellent choice when you need to modify the database in daily work. This article will introduce how to use MySQL to modify database data.
1. Connect to the MySQL database
Before modifying the MySQL data, you need to first connect to the MySQL database through the command line or graphical tool. In the command line, you can use the following command:
mysql -h hostname -u username -p password
Among them, hostname is the MySQL server address to be connected, username is the username to log in to MySQL, and password is the password to log in to MySQL. After the connection is successful, a prompt similar to the following will appear:
mysql>
2. Select the database to be modified
After connecting to the MySQL database, you need to select the database to be modified. You can use the following command:
USE database_name;
where database_name is the name of the database to be selected.
3. Modify the data in the data table
After selecting the database to be modified, you can start modifying the data in it. You can use the UPDATE statement to modify data in the data table. The format of the UPDATE statement is as follows:
UPDATE table_name SET column1=value1, column2=value2, ... WHERE condition;
Among them, table_name is the name of the data table to be modified, column1, column2, etc. are the column names to be modified, value1, value2, etc. are the new values to be modified. The WHERE condition is optional and is used to specify filter conditions for the data to be modified. If you do not specify a WHERE condition, all rows will be modified.
For example, if you want to increase the salary of all employees with the surname Smith in the employees table by 10%, you can use the following command:
UPDATE employees SET salary = salary * 1.1 WHERE last_name = 'Smith';
This command will increase the salary of all employees with the surname Smith in the employees table. Employee wages increased by 10%.
4. Insert new data
If you need to insert new data into the data table, you can use the INSERT INTO statement. The format of the INSERT INTO statement is as follows:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Among them, table_name is the name of the data table to be inserted, column1, column2, etc. are the column names to be inserted, value1, value2, etc. are the data values to be inserted.
For example, if you want to insert a new record in the employees table, the information is as follows:
first_name: John last_name: Smith birth_date: 1980-01-01 hire_date: 2021-01-01 gender: M
You can use the following command:
INSERT INTO employees (first_name, last_name, birth_date, hire_date, gender) VALUES ('John', 'Smith', '1980-01-01', '2021-01-01', 'M');
This command will insert a new record in the employees table New record, the recorded information is as shown above.
5. Delete data
If you need to delete data in the data table, you can use the DELETE statement. The format of the DELETE statement is as follows:
DELETE FROM table_name WHERE condition;
Among them, table_name is the name of the data table to be deleted, and condition is optional and is used to specify the filtering conditions of the data to be deleted. If you do not specify a WHERE condition, all rows will be deleted.
For example, if you want to delete the records of employees with the last name Smith in the employees table, you can use the following command:
DELETE FROM employees WHERE last_name = 'Smith';
This command will delete all records of employees with the last name Smith in the employees table.
6. End the MySQL session
After completing the modification to the database, you can use the following command to end the MySQL session:
EXIT;
or
QUIT;
That’s it How to modify database data using MySQL. Remember, be very careful when modifying data to avoid data loss due to misuse. It is recommended that you make relevant backup work before making any modifications.
The above is the detailed content of mysql modify database data. For more information, please follow other related articles on the PHP Chinese website!