MySQL--DML语句(mysql语句大全实例教程)
wptr33 2025-05-03 16:58 14 浏览
介绍
数据操纵语言DML(Data Manipulation Language),用户通过它可以实现对数据库的基本操作。就是我们最经常用到的UPDATE、INSERT、DELETE。 主要用来对数据库的数据进行一些操作。
1、表记录操作-上
1.1、DML概述
DML 操作是指对数据库中表记录的操作,主要包括表记录的插入(insert)、更新(update)和删除(delete),是开发人员日常使用最频繁的操作。
1.2、插入记录
格式:
insert into 数据库表名 [字段名列表] values(字段值列表)
说明:
insert into 数据库表名:指定增加记录的表
[字段名列表]:表示要给那些字段加入字段值,没有,就为所有字段
values(字段值列表):表示为对应的字段加入对应的字段值
为所有字段插入值
格式:
insert into 数据库表名(字段名1,字段名2...,字段名n)values(字段值1,字段值2...,字段值n);
或
insert into 数据库表名 values(字段值1,字段值2...,字段值n);
案例:
#指定所有字段插入记录
mysql> insert into student(id,sname,birthday)values(1,"林志颖","1974-10-18");
Query OK, 1 rows affected (0.03 秒)
#不指定字段,默认所有字段插入记录
mysql> insert into student values(2,"郭德纲","1973-01-18");
Query OK, 1 rows affected (0.01 秒)
#查看数据库表中所有记录
mysql> select * from student;
+------+-----------+------------+
| id | sname | birthday |
+------+-----------+------------+
| 1 | 林志颖 | 1974-10-18 |
| 2 | 郭德纲 | 1973-01-18 |
+------+-----------+------------+
2 行于数据集 (0.01 秒)
插入部分字段值
格式:
insert into 数据库表名(字段名1,字段名2...)values(字段值1,字段值2...);
#注意:没有给定字段的值,为null;
案例:
#插入部分字段值
mysql> insert into student(id,sname)values(3,"柳岩");
Query OK, 1 rows affected (0.01 秒)
#查看数据库表中所有记录
mysql> select * from student;
+------+-----------+------------+
| id | sname | birthday |
+------+-----------+------------+
| 1 | 林志颖 | 1974-10-18 |
| 2 | 郭德纲 | 1973-01-18 |
| 3 | 柳岩 | NULL |
+------+-----------+------------+
3 行于数据集 (0.01 秒)
注意:
只插入部份字段值时,前面必须带字段名字。
mysql> insert into student values(4,"王宝强");
Column count doesn't match value count at row 1#列计数与第1行的值计数不匹配
2、表记录操作-下
2.1、更新记录
格式:
update 数据库表名 set 字段名1=字段值1,字段名2=字段值2...,字段名n=字段值n [where 条件表达式];
#注意:更新的数据可以是0-N条记录
说明:
update 数据库表名:指定需要更新的数据库表
set 字段名=字段值:修改指定的数据库表中字段的值
[where 条件表达式]:修改满足条件的记录的字段值,可省略
不带条件记录更新
格式:
update 数据库表名 set 字段名1=字段值1,字段名2=字段值2...,字段名n=字段值n; #表示修改表中所有记录
案例:
加入一个性别(sex varchar(2))字段,将sex的值都改为“男”
#在student表中增加字段sex
mysql> alter table student add sex varchar(2);
Query OK, 0 rows affected (0.24 秒)
#查看表结构
mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| sex | varchar(2) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 行于数据集 (0.01 秒)
#修改student表中sex字段的值为'男'
mysql> update student set sex='男';
Query OK, 3 rows affected (0.09 秒)
#查看表中所有记录
mysql> select * from student;
+------+-----------+------------+------+
| id | sname | birthday | sex |
+------+-----------+------------+------+
| 1 | 林志颖 | 1974-10-18 | 男 |
| 2 | 郭德纲 | 1973-01-18 | 男 |
| 3 | 柳岩 | NULL | 男 |
+------+-----------+------------+------+
3 行于数据集 (0.01 秒)
带条件记录更新
格式:
update 数据库表名 set 字段名1=字段值1,字段名2=字段值2...,字段名n=字段值n [where 条件表达式]; #表示修改表中满足条件的记录
案例:
修改id=3的记录,将sex改为'女'
#修改id为3记录中的sex改为'女'
mysql> update student set sex='女' where id=3;
Query OK, 1 rows affected (0.01 秒)
#查看表中所有记录
mysql> select * from student;
+------+-----------+------------+------+
| id | sname | birthday | sex |
+------+-----------+------------+------+
| 1 | 林志颖 | 1974-10-18 | 男 |
| 2 | 郭德纲 | 1973-01-18 | 男 |
| 3 | 柳岩 | NULL | 女 |
+------+-----------+------------+------+
3 行于数据集 (0.01 秒)
修改id=3的记录,将sex改为'女',birthday改为1980-11-08
#修改id为3记录中的sex改为'女',birthday改为1980-11-08
mysql> update student set sex='女',birthday='1980-11-08' where id=3;
Query OK, 1 rows affected (0.11 秒)
#查看表中所有记录
mysql> select * from student;
+------+-----------+------------+------+
| id | sname | birthday | sex |
+------+-----------+------------+------+
| 1 | 林志颖 | 1974-10-18 | 男 |
| 2 | 郭德纲 | 1973-01-18 | 男 |
| 3 | 柳岩 | 1980-11-08 | 女 |
+------+-----------+------------+------+
3 行于数据集 (0.01 秒)
2.2、删除记录
格式:
delete from 数据库表名 [where 条件表达式];
说明:
delete from 数据库表名:指定删除记录的表
[where 条件表达式]:删除满足条件的记录的字段值,可省略
带条件删除记录
格式:
delete from 数据库表名 [where 条件表达式]; #删除满足条件的记录
案例:
删除id为1的记录
#删除id=1的记录
mysql> delete from student where id=1;
Query OK, 1 rows affected (0.08 秒)
#查看所有记录
mysql> select * from student;
+------+-----------+------------+------+
| id | sname | birthday | sex |
+------+-----------+------------+------+
| 2 | 郭德纲 | 1973-01-18 | 男 |
| 3 | 柳岩 | 1980-11-08 | 女 |
+------+-----------+------------+------+
2 行于数据集 (0.02 秒)
不带条件删除记录
格式:
delete from 数据库表名; #删除表中所有的记录
或
truncate table 数据库表名; #删除表中所有的记录
案例:
删除表中所有记录
#删除所有记录
mysql> delete from student;
Query OK, 2 rows affected (0.08 秒)
#查看所有记录
mysql> select * from student;
空的数据集 (0.01 秒)
#删除所有记录
mysql> truncate table student;
Query OK, 0 rows affected (0.08 秒)
#查看所有记录
mysql> select * from student;
空的数据集 (0.01 秒)
注意:
truncate删除的是表的结构,再创建一张表;delete删除的是表的记录;
相关推荐
- SpringBoot 3 + Flutter3 实战低代码运营管理-10章
-
获课》aixuetang.xyz/5075/三天构建运营管理系统:SpringBoot3+Flutter3高效开发方法论...
- SpringBoot探针实现:从零构建应用健康监控利器
-
SpringBoot探针实现:从零构建应用健康监控利器声明本文中的所有案例代码、配置仅供参考,如需使用请严格做好相关测试及评估,对于因参照本文内容进行操作而导致的任何直接或间接损失,作者概不负责。本文...
- Spring Batch中的JobRepository:批处理的“记忆大师”是如何工作
-
一、JobRepository是谁?——批处理的“档案馆”JobRepository是SpringBatch的“记忆中枢”,负责记录所有Job和Step的执行状态。它像一位严谨的档案管理员,把任务执...
- 还在为 Spring Boot3 技术整合发愁?一文解锁大厂都在用的实用方案
-
你在使用SpringBoot3开发后端项目时,是不是常常陷入这样的困境?想提升项目性能和功能,却不知道该整合哪些技术;好不容易选定技术,又在配置和使用上频频踩坑。其实,这是很多互联网大厂后端开发...
- 一文吃透!Spring Boot 项目请求日志记录,这几招你绝对不能错过!
-
在互联网应用开发的高速赛道上,系统的稳定性、可维护性以及安全性是每一位开发者都必须关注的核心要素。而请求日志记录,就如同系统的“黑匣子”,能够为我们提供排查故障、分析用户行为、优化系统性能等关键信息...
- spring-boot-starter-actuator简单介绍
-
SpringBootActuator是SpringBoot的一个功能强大的子项目,它提供了一些有用的监控和管理SpringBoot应用程序的端点。SpringBootActuat...
- 使用SpringBoot钩子或Actuator实现优雅停机
-
服务如何响应停机信号在java中我们可以直接利用通过Runtime...
- 28-自定义Spring Boot Actuator指标
-
上篇我们学习了《27-自定义SpringBootActuator健康指示器》,本篇我们学习自定义SpringBootActuator指标(Metric)。...
- 如何在Spring Boot中整合Spring Boot Actuator进行服务应用监控?
-
监控是确保系统稳定性和性能的关键组成部分,而在SpringBoot中就提供了默认的应用监控方案SpringBootActuator,通过SpringBootActuator提供了开箱即用的应...
- 「Spring Boot」 Actuator Endpoint
-
Actuator官网地址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html目的监控并管理应用程序...
- Spring Boot Actuator监控功能全面剖析
-
SpringBootActuator监控功能全面剖析在现代企业级Java开发中,SpringBoot以其轻量化、高效率的特性深受开发者青睐。而作为SpringBoot生态系统的重要组成部分,S...
- 1000字彻底搞懂SpringBootActuator组件!
-
SpringBootActuator组件SpringBootActuator通过HTTPendpoints或者JMX来管理和监控SpringBoot应用,如服务的审计、健康检查、指标统计和...
- JavaScript数据类型(javascript数据类型介绍)
-
基本数据类型BooleanNullNumberStringSymbolUndefined对象数据类型ObjectArray定义:JavaScript数组是内置的对象之一,它可以用一个变量来存储多个同种...
- 能运行,不代表它是对的:5 个潜伏在正常功能下的 JavaScript 错误
-
JavaScript的动态性和复杂性意味着,代码虽然表面上正常运行,但一些深层次、隐蔽的陷阱往往让人意想不到,梳理了几个JavaScript开发中难以发现的隐蔽错误,旨在帮助我们写出更健壮、更可...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
-
- SpringBoot 3 + Flutter3 实战低代码运营管理-10章
- SpringBoot探针实现:从零构建应用健康监控利器
- Spring Batch中的JobRepository:批处理的“记忆大师”是如何工作
- Github霸榜的SpringBoot全套学习教程,从入门到实战,内容超详细
- 还在为 Spring Boot3 技术整合发愁?一文解锁大厂都在用的实用方案
- 一文吃透!Spring Boot 项目请求日志记录,这几招你绝对不能错过!
- spring-boot-starter-actuator简单介绍
- 使用SpringBoot钩子或Actuator实现优雅停机
- 28-自定义Spring Boot Actuator指标
- 如何在Spring Boot中整合Spring Boot Actuator进行服务应用监控?
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)