MySQL database - MySQL DELETE: delete data

In MySQL, you can use the DELETE statement to delete one or more rows of data in a table.

Delete data from a single table

Use the DELETE statement to delete data from a single table, the syntax format is:

DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]

The syntax is explained as follows:

  • <表名>: Specifies the name of the table whose data is to be deleted.
  • ORDER BY Clause: optional. Indicates that when deleting, each row in the table will be deleted in the order specified in the clause.
  • WHERE Clause: optional. Indicates that the deletion condition is limited for the deletion operation. If this clause is omitted, it means that all rows in the table are deleted.
  • LIMIT Clause: optional. Used to tell the server the maximum number of rows to delete before control commands are returned to the client.

Note: When not using the WHERE condition, all data will be deleted.

Delete all data in the table

[Example 1] Delete all the data in the tb_courses_new table, the entered SQL statement and execution results are as follows:

mysql> DELETE FROM tb_courses_new;
Query OK, 3 rows affected (0.12 sec)
mysql> SELECT * FROM tb_courses_new;
Empty set (0.00 sec)

Delete data from table based on condition

[Example 2] In the tb_courses_new table, delete the record whose course_id is 4, the input SQL statement and the execution result are as follows:

mysql> DELETE FROM tb_courses
    -> WHERE course_id=4;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM tb_courses;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            3 | Computer Network |
|         2 | Database    |            3 | MySQL            |
|         3 | Java        |            4 | Java EE          |
+-----------+-------------+--------------+------------------+
3 rows in set (0.00 sec)

It can be seen from the running results that the record with course_id 4 has been deleted.

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130419946