MySQL is a widely used relational database management system. When using MySQL, sometimes you need to delete data in a table. This article will introduce how to delete table data using MySQL.
Step 1: Log in to MySQL
First, you need to log in to MySQL with the correct user name and password. You can log in using the following command:
mysql -u username -p
where username is the MySQL username.
After entering the command, the system will prompt you to enter the password. After entering the password, press Enter to log in to MySQL.
Step 2: Select the database
After successfully logging in to MySQL, you need to select the database where you want to delete the table data. Use the following command to select a database:
use database_name;
Where database_name is the name of the database where the table data is to be deleted.
Step 3: Delete data
Next, you need to use the following command to delete the data in the table:
DELETE FROM table_name;
Where, table_name is the name of the table where the data is to be deleted.
If you want to delete all the data in the table, you can use the following command:
TRUNCATE TABLE table_name;
This command will delete all the data in the table and retain the table structure.
Note: Please make sure to back up all data before deleting it. Accidentally deleting data will cause irrecoverable damage to the system.
Example:
Suppose you want to delete the data in the database named example and the table named student, you can follow the following steps:
mysql -u root -p
use example;
DELETE FROM student;
Alternatively, use the following command to delete all data in the student table:
TRUNCATE TABLE student;
Summary:
Deleting table data in MySQL is a very important task. By following the correct steps, you can avoid unnecessary problems. Before deleting table data, be sure to back up the data.
The above is the detailed content of mysql delete table data. For more information, please follow other related articles on the PHP Chinese website!