当前位置:网站首页>Mysql数据库基础:DML数据操作语言
Mysql数据库基础:DML数据操作语言
2022-06-28 00:40:00 【持久的棒棒君】
DML语言
1、insert插入语句
语法
- 第一种方式:经典插入
insert into 表名(列名1,列名2,...) values (值1,值2,...)
注意点:
1、插入的值的类型要与列的类型一致或兼容
2、不可以为null的列必须插入值,可以为null的列(nullable)要么将null作为值插入,要么可以同时省略列和列对应的值不写。
3、列的顺序是可以调换的,但要求对应的值也同时匹配调换
4、列数和值的个数必须一致
5、可以省略列名,但要求列的顺序和表中列的顺序一致,并且值为null的不可以省略
- 第二种方式
insert into 表名
set 列名=值,列名=值,...
对比这两种插入方式
1、方式一可以支持插入多行,方式2不支持insert into 表名 values (值1,值2,...) ,(值1,值2,...) ,(值1,值2...);2、方式一支持子查询,方式二不支持子查询
insert into 表名(列名1,列名2,...) select语句所以方式一使用的场景更多
2、update更新/修改语句
2.1 修改单表的记录
- 语法
update 表名
set 列=新值,列=新值,...
where 筛选条件;
- 实例
# 修改beauty表中姓唐的女生的电话为13887888881
update beauty set phone = '13887888881'
where name like '唐%';
# 修改boys表中id号为2的名称为张飞,年龄为22
update boys set name = '张飞',age = 22
where id = 2;
2.2 修改多表的记录
- 语法
update 表1 别名
inner|left|right join 表2 别名
on 连接条件
set 列=值,...
where 筛选条件
- 案例
# 修改张无忌的女朋友的手机号为114
update boys b
inner join beauty b
on bo.`id`=b.`boyfrind_id`
set b.`phone`='114'
where bo.`boyName`='张无忌';
3、delete/truncate删除语句
语法
- delete语句
作用:删除表中的数据
1、单表的删除
# 删除表中所有数据
delete from 表名;
# 删除表中指定条件的数据
delete from 表名 where 筛选条件;
例题:
# 删除手机号以9结尾的女生信息
delete from beauty where phone like '%9';
2、多表的删除
# 删除张无忌的女朋友的信息
delete 表1的别名,表2的别名
from 表1,表2
inner|left|right join 表2 别名
on 连接条件
where 筛选条件;
例题:
# 删除张无忌的女朋友的信息
delete b
from beauty b
inner join boys bo
on b.`boyfriend_id`=bo.`id`
where bo.`boyName`='张无忌';
- truncate语句
作用:删除表中所有数据
truncate table 表名;
delete和truncate语句的区别
1、delete可以加where条件,truncate不能加
2、truncate删除效率比delete高
3、如果要删除的表中有自增长列,如果用delete删除后,再插入数据,自增长列的值从断点开始,而truncate删除后,再插入数据,自增长列的值从1开始
4、truncate删除没有返回值,delete删除有返回值
5、truncate删除不能回滚,delete删除可以回滚
边栏推荐
- [JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes
- Adding text labels to cesium polygons the problem of polygon center point offset is solved
- JS 数组随机取值(随机数组取值)
- Fundamentals of scala (3): operators and process control
- Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)
- Jenkins - 邮件通知 Email Notification 插件
- ScheduledThreadPoolExecutor源码解读(二)
- New choice for database Amazon Aurora
- Leetcode topic [array] -228- summary interval
- 数智学习 | 流批一体实时数仓建设路径探索
猜你喜欢

架构高可靠性应用知识图谱 ----- 架构演进之路

文件傳輸協議--FTP

General timer and interrupt of stm32

MySQL collection, here are all the contents you want

How to handle computer security certificate errors

Embedded must learn, detailed explanation of hardware resource interface -- Based on arm am335x development board (Part 1)

Interpretation of the source code of scheduledthreadpoolexecutor (II)

Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case

批阅2022春季学期课程小论文提交情况

低代码DSL里面在数仓中的实践
随机推荐
General timer and interrupt of stm32
JS 随机数(随机数 小数)
Jenkins - access the Jenkins user-defined parameter variable, and handle the variable value containing spaces
How to handle computer security certificate errors
Numpy----np. reshape()
如何系统学习LabVIEW?
SQL injection Bypass (2)
JS implementation clock
如何以数据驱动「客户全生命周期管理」,提高线索转化率及客户满意度?
云原生(三十) | Kubernetes篇之应用商店-Helm
Appium automation test foundation ADB common commands (I)
[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes
SQL 注入绕过(四)
OS module and os Learning of path module
Appium自动化测试基础— 补充:App的包名(appPackage)和启动名(appActivity)
ScheduledThreadPoolExecutor源码解读(二)
ROS+Gazebo中红绿黄交通灯如何实现?
The interviewer asked: can you simulate the new operator of JS
TD Hero online conference on July 2
Locust performance test - parameterization, no repetition of concurrent cyclic data sampling