【mysql】mysql之数据操作语言(insert、delete、update)

2024-08-24 12:44

本文主要是介绍【mysql】mysql之数据操作语言(insert、delete、update),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  本站以分享各种运维经验和运维所需要的技能为主

《python零基础入门》:python零基础入门学习

《python运维脚本》: python运维脚本实践

《shell》:shell学习

《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战

《k8》从问题中去学习k8s

《docker学习》暂未更新

《ceph学习》ceph日常问题解决分享

《日志收集》ELK+各种中间件

《运维日常》运维日常

《linux》运维面试100问

《DBA》db的介绍使用(mysql、redis、mongodb...)

 

DML数据操作语言(insert、delete、update)

1.insert 命令

1)查看表结构
mysql> desc student;
+----------+---------------------+------+-----+-------------------+----------------+
| Field    | Type                | Null | Key | Default           | Extra          |
+----------+---------------------+------+-----+-------------------+----------------+
| id       | int(11)             | NO   | PRI | NULL              | auto_increment |
| name     | varchar(12)         | NO   |     | NULL              |                |
| age      | tinyint(3) unsigned | NO   |     | NULL              |                |
| gender   | enum('m','f')       | YES  |     | f                 |                |
| cometime | datetime            | YES  |     | CURRENT_TIMESTAMP |                |
| birthday | datetime            | YES  |     | NULL              |                |
+----------+---------------------+------+-----+-------------------+----------------+
6 rows in set (0.00 sec)
2)插入数据(不规范写法)
mysql> insert into student values(1,'邱导',78,'f',now(),'1942-07-14');
Query OK, 1 row affected (0.00 sec)mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
1 row in set (0.00 sec)
3)插入数据(规范写法)
#1.插入指定列数据
mysql> insert into student(name,age) values('曾导','84');
Query OK, 1 row affected (0.00 sec)mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
+----+--------+-----+--------+---------------------+---------------------+
3 rows in set (0.00 sec)#2.插入指定列数据
mysql> insert into student(name,age,birthday) values('曾导','84','1936-02-20');
Query OK, 1 row affected (0.00 sec)mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
4 rows in set (0.00 sec)
4)插入多条数据
mysql> insert into student(name,age,birthday) values('好大','18',1936-02-21),('好小','28','1992-01-01');
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 1mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
|  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)

2.update命令

1)查看数据
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
|  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)
2)修改数据
#使用update语句必须要加where条件
mysql> update student set age=18 where name='邱导';
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  18 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  18 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
|  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)
3)指定修改一条数据
#如果数据库有主键,一定使用主键来作为where判断
mysql> update student set age=88 where name='邱导' and cometime='2020-07-15 09:21:12';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> update student set age=88 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  88 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
|  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)

3.delete语句

1)删除数据
#1.先查看数据,确认要删除的数据,怎么确定唯一#2.使用delete语句也一定要加where条件
mysql> delete from student where id=8;
Query OK, 1 row affected (0.01 sec)mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name   | age | gender | cometime            | birthday            |
+----+--------+-----+--------+---------------------+---------------------+
|  1 | 邱导   |  88 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
|  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
7 rows in set (0.00 sec)#3.如果就是要清空表
mysql> delete from student where 1=1;
Query OK, 1 row affected (0.01 sec)
#(危险)
truncate table student;
drop table student

4.使用update代替delete

1)添加状态字段
mysql> alter table student add status enum('1','0') default 1;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+--------+
| id | name   | age | gender | cometime            | birthday            | status |
+----+--------+-----+--------+---------------------+---------------------+--------+
|  1 | 邱导   |  88 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 | 1      |
|  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 1      |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                | 1      |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1      |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1      |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1      |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1      |
+----+--------+-----+--------+---------------------+---------------------+--------+
7 rows in set (0.00 sec)
2)使用update代替delete
#相当于删除学生
mysql> update student set status='0' where id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+--------+
| id | name   | age | gender | cometime            | birthday            | status |
+----+--------+-----+--------+---------------------+---------------------+--------+
|  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 0      |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                | 1      |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1      |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1      |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1      |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1      |
+----+--------+-----+--------+---------------------+---------------------+--------+
6 rows in set (0.00 sec)#相当于学生回来
mysql> update student set status='1' where id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+--------+
| id | name   | age | gender | cometime            | birthday            | status |
+----+--------+-----+--------+---------------------+---------------------+--------+
|  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 1      |
|  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                | 1      |
|  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1      |
|  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1      |
|  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1      |
|  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1      |
+----+--------+-----+--------+---------------------+---------------------+--------+
6 rows in set (0.00 sec)

5. drop,truncate,delete区别

1、drop (删除表):删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表。drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。2、truncate (清空表中的数据):删除内容、释放空间但不删除定义(保留表的数据结构)。与drop不同的是,只是清空表数据而已。truncate 不能删除行数据,要删就要把表清空。3、delete (删除表中的数据):delete 语句用于删除表中的行。delete语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。truncate与不带where的delete :只删除数据,而不删除表的结构(定义)4、truncate table 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用delete。如果要删除表定义及其数据,请使用 drop table 语句。5、对于由foreign key约束引用的表,不能使用truncate table ,而应使用不带where子句的delete语句。由于truncate table 记录在日志中,所以它不能激活触发器。6、执行速度,一般来说: drop> truncate > delete。7、delete语句是数据库操作语言(dml),这个操作会放到 rollback segement 中,事务提交之后才生效;如果有相应的 trigger,执行的时候将被触发。truncate、drop 是数据库定义语言(ddl),操作立即生效,原数据不放到 rollback segment 中,不能回滚,操作不触发 trigger。

这篇关于【mysql】mysql之数据操作语言(insert、delete、update)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1102516

相关文章

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

mysql8.0.43使用InnoDB Cluster配置主从复制

《mysql8.0.43使用InnoDBCluster配置主从复制》本文主要介绍了mysql8.0.43使用InnoDBCluster配置主从复制,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录1、配置Hosts解析(所有服务器都要执行)2、安装mysql shell(所有服务器都要执行)3、

k8s中实现mysql主备过程详解

《k8s中实现mysql主备过程详解》文章讲解了在K8s中使用StatefulSet部署MySQL主备架构,包含NFS安装、storageClass配置、MySQL部署及同步检查步骤,确保主备数据一致... 目录一、k8s中实现mysql主备1.1 环境信息1.2 部署nfs-provisioner1.2.

MySQL中VARCHAR和TEXT的区别小结

《MySQL中VARCHAR和TEXT的区别小结》MySQL中VARCHAR和TEXT用于存储字符串,VARCHAR可变长度存储在行内,适合短文本;TEXT存储在溢出页,适合大文本,下面就来具体的了解... 目录一、VARCHAR 和 TEXT 基本介绍1. VARCHAR2. TEXT二、VARCHAR

MySQL中C接口的实现

《MySQL中C接口的实现》本节内容介绍使用C/C++访问数据库,包括对数据库的增删查改操作,主要是学习一些接口的调用,具有一定的参考价值,感兴趣的可以了解一下... 目录准备mysql库使用mysql库编译文件官方API文档对象的创建和关闭链接数据库下达sql指令select语句前言:本节内容介绍使用C/

使用Java填充Word模板的操作指南

《使用Java填充Word模板的操作指南》本文介绍了Java填充Word模板的实现方法,包括文本、列表和复选框的填充,首先通过Word域功能设置模板变量,然后使用poi-tl、aspose-words... 目录前言一、设置word模板普通字段列表字段复选框二、代码1. 引入POM2. 模板放入项目3.代码

mybatis直接执行完整sql及踩坑解决

《mybatis直接执行完整sql及踩坑解决》MyBatis可通过select标签执行动态SQL,DQL用ListLinkedHashMap接收结果,DML用int处理,注意防御SQL注入,优先使用#... 目录myBATiFBNZQs直接执行完整sql及踩坑select语句采用count、insert、u