Mysql DBA Advanced Operations Learning Note-delete data from a table

Source: Internet
Author: User
Tags dba

9.11 deleting data from a table

    1. Command syntax: Delete from table name where expression

Practice:

(1) Delete the record numbered 3 in table student

mysql> use zbfDatabase changedmysql> select * from student;+----+-----------+-----+--------+| id | name  | age | dept   |+----+-----------+-----+--------+|  1 | zbf666|  29 | linux  ||  2 | lisi  |  28 | mysql  ||  3 | zhangsan  |  21 | python ||  4 | woshishei |  24 | java   |+----+-----------+-----+--------+4 rows in set (0.06 sec)mysql> delete from student where id=3;Query OK, 1 row affected (0.01 sec)mysql> select * from student;+----+-----------+-----+-------+| id | name  | age | dept  |+----+-----------+-----+-------+|  1 | zbf666|  29 | linux ||  2 | lisi  |  28 | mysql ||  4 | woshishei |  24 | java  |+----+-----------+-----+-------+3 rows in set (0.02 sec)

(2) You can also delete a row with name equal to Lisi

mysql> select * from student;+----+-----------+-----+-------+| id | name  | age | dept  |+----+-----------+-----+-------+|  1 | zbf666|  29 | linux ||  2 | lisi  |  28 | mysql ||  4 | woshishei |  24 | java  |+----+-----------+-----+-------+3 rows in set (0.02 sec)mysql> delete from student where name=‘lisi‘;Query OK, 1 row affected (0.01 sec)mysql> select * from student;+----+-----------+-----+-------+| id | name  | age | dept  |+----+-----------+-----+-------+|  1 | zbf666|  29 | linux ||  4 | woshishei |  24 | java  |+----+-----------+-----+-------+2 rows in set (0.00 sec)

(3) You can also delete lines with IDs greater than 3

mysql> select * from student;+----+-----------+-----+-------+| id | name  | age | dept  |+----+-----------+-----+-------+|  1 | zbf666|  29 | linux ||  4 | woshishei |  24 | java  |+----+-----------+-----+-------+2 rows in set (0.00 sec)mysql> delete from student where id>3;Query OK, 1 row affected (0.01 sec)mysql> select * from student;+----+--------+-----+-------+| id | name   | age | dept  |+----+--------+-----+-------+|  1 | zbf666 |  29 | linux |+----+--------+-----+-------+1 row in set (0.00 sec)

Hint: No condition is all deleted, is also very dangerous operation, here does not show up. Delete from student.

2. command syntax TRUNCATE TABLE name

Truncate table student; Clear what you want in the table

mysql> truncate table student;Query OK, 0 rows affected (0.00 sec)mysql> select * from student;Empty set (0.00 sec)

Truncate from Srudent and delete from student differences

A.truncate table student; faster, empties physical files.

B.delete from student; logical purge, by row.

Mysql DBA Advanced Operations Learning Note-delete data from a table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.