mysql中关于删除语句大全总结(上)


本文摘自PHP中文网,作者黄舟,侵删。

1.drop语句。可以用来删除数据库和表。

A用drop语句来删除数据库:

1

2

3

4

5

6

7

8

9

10

11

12

13

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用drop语句来删除表:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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语句。用来删除表中的字段:

A通过where子句来指定删除表中的某个记录:

1

2

3

4

5

6

7

8

9

10

11

12

13

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)

注意:如果delete语句中没有加入where就会把表中的所有记录全部删除:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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通过select子句来删除表中的某个记录:

1

2

3

4

5

6

7

8

9

10

11

12

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.用truncate来删除表中的所以字段:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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)

以上就是mysql中关于删除语句大全总结(上)的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

mysql怎么删除注册表

mysql怎么查看有没有死锁

mysql8和mysql5.7的区别有哪些

mysql增删改查语句操作有哪些

mysql怎么更改日志文件的路径?

详解mysql集群:一主多从架构实现

mysql如何从ibd文件恢复数据

mysql使用技巧之行类视图子查询

centos7 mysql 5.6 多主一从 解决方案与详细配置

root密码忘记该怎么办

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...