Home  >  Article  >  Database  >  How to use delete in mysql

How to use delete in mysql

(*-*)浩
(*-*)浩Original
2019-05-07 17:54:0910794browse

The delete statement in MySQL is generally used to delete one or more rows of data in a table.

Recommended courses: MySQL Tutorial.

How to use delete in mysql

The following is the general syntax of the SQL DELETE statement to delete data from a MySQL data table:

DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]
DELETE FROM table_name [WHERE Clause]
  • : Specify the table name from which data is to be deleted.

  • ORDER BY clause: optional. Indicates that when deleting, rows in the table will be deleted in the order specified in the clause.

  • WHERE clause: optional. Indicates that the deletion conditions are 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 be deleted before the control command is returned to the client.

  • The WHERE clause is very useful when you want to delete specified records in the data table. To limit the number of rows to be deleted, you can use the LIMIT clause with Order By.

    Delete data in the table based on conditions

    In the tb_courses_new table, delete the record with course_id 4, the entered SQL statement and execution results As follows.

    The following operations need to be performed on the command line.

    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          |
    |         4 | Php         |            4 | phpstudy         | 
    +-----------+-------------+--------------+------------------+
    4 rows in set (0.00 sec)
    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)

    The above is the detailed content of How to use delete 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