Home > Database > Mysql Tutorial > body text

mysql delete table data

PHPz
Release: 2023-05-12 12:30:37
Original
1223 people have browsed it

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
Copy after login

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;
Copy after login

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;
Copy after login

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;
Copy after login

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:

  1. Use the following command to log in to MySQL :
mysql -u root -p
Copy after login
  1. Enter the password to log in to MySQL.
  2. Use the following command to select the database where you want to delete the table data:
use example;
Copy after login
  1. Use the following command to delete the data in the student table:
DELETE FROM student;
Copy after login

Alternatively, use the following command to delete all data in the student table:

TRUNCATE TABLE student;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template