Home  >  Article  >  Database  >  Summary of delete statements in mysql (Part 1)

Summary of delete statements in mysql (Part 1)

黄舟
黄舟Original
2017-09-07 11:39:543667browse

1.drop statement. Can be used to delete databases and tables.

A uses the drop statement to delete the database:

mysql> drop database hello;Query OK, 0 rows affected (0.19 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
| trains             |
+--------------------+
6 rows in set (0.00 sec)

B uses the drop statement to delete the table:

mysql> drop table q1;Query OK, 0 rows affected (0.39 sec)
mysql> show tables;
+------------------+
| Tables_in_trains |
+------------------+
| 4inall           |
| ava              |
| book             |
| c1score          |
| c2score          |
| course           |
| joke             |
| sc               |
| sc1              |
| student          |
| student1         |
| teacher          |
+------------------+
12 rows in set (0.00 sec)

2.delete statement. Used to delete fields in the table:

A specifies the deletion of a record in the table through the where clause:

mysql> delete from joke where gid=1;
mysql> select * from joke;
+------+-----------+-------+------+
| gid  | name      | sex   | age  |
+------+-----------+-------+------+
|    3 | xiaowan2  | male  |   22 |
|    3 | xiaowan2  | male  |   22 |
|    3 | xiaowan2  | male  |   22 |
|    3 | xiaowan22 | 1male |   22 |
|    0 | joker     | NULL  | NULL |
|    0 | joker     | NULL  | NULL |
+------+-----------+-------+------+
6 rows in set (0.00 sec)

Note: If where is not added to the delete statement, the table will be deleted. Delete all records in the table:

mysql> select * from student1;
+------+--------+------+------+
| s    | sname  | sage | ssex |
+------+--------+------+------+
|    1 | 刘一   |   18 | 男   |
|    2 | 钱二   |   19 | 女   |
|    3 | 张三   |   17 | 男   |
|    4 | 李四   |   18 | 女   |
|    5 | 王五   |   17 | 男   |
|    6 | 赵六   |   19 | 女   |
+------+--------+------+------+
6 rows in set (0.00 sec)
mysql> delete from student1;Query OK, 6 rows affected (0.19 sec)
mysql> select * from student1;Empty set (0.00 sec)

B Delete a record in the table through the select clause:

mysql> delete from student1 where s in (select s from student where sage=18 and ssex="男");Query OK, 1 row affected (0.13 sec)
mysql> select * from student1;
+------+--------+------+------+
| s    | sname  | sage | ssex |
+------+--------+------+------+
|    2 | 钱二   |   19 | 女   |
|    3 | 张三   |   17 | 男   |
|    4 | 李四   |   18 | 女   |
|    5 | 王五   |   17 | 男   |
|    6 | 赵六   |   19 | 女   |
+------+--------+------+------+
5 rows in set (0.00 sec)

3. Use truncate to delete all fields in the table:

mysql> select * from student1;
+------+--------+------+------+
| s    | sname  | sage | ssex |
+------+--------+------+------+
|    1 | 刘一   |   18 | 男   |
|    2 | 钱二   |   19 | 女   |
|    3 | 张三   |   17 | 男   |
|    4 | 李四   |   18 | 女   |
|    5 | 王五   |   17 | 男   |
|    6 | 赵六   |   19 | 女   |
+------+--------+------+------+
6 rows in set (0.00 sec)
mysql> truncate table student1;Query OK, 0 rows affected (0.28 sec)
mysql> select * from student1;Empty set (0.00 sec)

The above is the detailed content of Summary of delete statements in mysql (Part 1). 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