Home  >  Article  >  Database  >  Statement to delete table records in mysql

Statement to delete table records in mysql

下次还敢
下次还敢Original
2024-05-01 22:48:47891browse

The statement to delete table records in MySQL is DELETE, and the syntax is DELETE FROM table_name WHERE condition, which is used to specify deletion conditions. Use caution when using the DELETE statement as it permanently deletes records. Additionally, there are other syntax variations including using LIMIT to limit the number of records deleted, using ORDER BY to sort to delete records, and using TRUNCATE to quickly delete all records.

Statement to delete table records in mysql

MySQL statement to delete table records

The statement to delete table records in MySQL is DELETE. Its basic syntax is as follows:

<code>DELETE FROM table_name
WHERE condition;</code>

Where:

  • table_name is the name of the table from which records are to be deleted.
  • condition is an optional condition that specifies which records to delete.

Usage Example

For example, to delete all users with user_type = 'guest' from the users table For records, you can use the following statement:

<code>DELETE FROM users
WHERE user_type = 'guest';</code>

Note

  • ##DELETE statement will permanently delete the record, so please use it Be careful.
  • If the
  • WHERE clause is not specified, all records in the table will be deleted. The
  • DELETE statement does not automatically commit changes. Changes need to be manually committed or rolled back using COMMIT or ROLLBACK.

Other syntax variations

In addition to the basic syntax, the

DELETE statement has other syntax variations, including:

  • Use LIMIT to limit the number of deleted records:
<code>DELETE FROM table_name
WHERE condition
LIMIT number_of_rows;</code>
  • Use ORDER BY Sort the records to be deleted:
<code>DELETE FROM table_name
WHERE condition
ORDER BY column_name DESC
LIMIT number_of_rows;</code>
  • Use TRUNCATE to quickly delete all records:
<code>TRUNCATE TABLE table_name;</code>

TRUNCATE is more efficient than DELETE because it does not perform line-by-line deletion. However, it cannot recover deleted records.

The above is the detailed content of Statement to delete table records in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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