MySQL 学习笔记——基础 DML 数据操作语言

MySQL 学习笔记——基础 DML 数据操作语言


含义:DML (Data Manipulation Language) 操作是指对数据库中表记录的操作,主要包括表记录的插入(insert)、更新(update)、删除(delete)和查询(select),是开发人员日常使用最频繁的操作。

分类

  • 插入:insert
  • 修改:update
  • 删除:delete

一、插入语句

1、插入方式一

语法insert into 表名(字段名1,字段名2....) values(值1, 值2....)

特点

  • 插入的值的类型要与列的类型一致或者兼容
  • 不可以为null的列必须插入值
  • 列的顺序可以调换
  • 列数和值的个数必须一致
  • 可以省略列名,默认为所有列,而且列的顺序与表中列的顺序一致。
insert into beauty (id, name, borndate, phone, photo, boyfriend_id) values(15, '宋茜', '1997-01-01', 1809999999, null, 10);

insert into beauty (id, name, borndate, phone, photo, boyfriend_id) 
values(15, '宋茜', '1997-01-01', 1809999999, null, 10),
(16, '宋茜', '1997-01-01', 1809999999, null, 10)
(17, '宋茜', '1997-01-01', 1809999999, null, 10)

2、插入方式二

语法insert into 表名 set 列名=值, 列名=值.......

3、两种方式总结

  • 方式一支持插入多行,方式二不支持。
  • 方式一支持子查询,可以直接插入select语句,使用select查询的结果集作为values的值插入表中。

二、修改语句

1、修改单表的记录

语法update 表名 set 列名=值, 列名=值...... where 筛选条件

update beauty set phone = 19000000000 where name = '夏雪';

update beauty set phone = 19000000000, sex = '1' where name = '夏雪';

2、修改多表记录

语法

-- sql92语法
update1 别名,2 别名 set=where 连接条件 and 筛选条件;

-- sql99语法
update1 别名 inner|left|right join2 别名 on 连接条件 set=,=..... where 筛选条件;

案例

update boys bo inner join beauty b on bo.id = b.boyfriend_id set b.phone = 114 where bo.boyName = '张无忌';

三、删除语句

1、方式一 delete

单表语法delete from 表名 where 筛选条件如果不加where筛选条件,将会删除整个表的数据。

多表语法delete 别名1, 别名2 from 表1 别名1, 表2 别名2 where 筛选条件如果不加where筛选条件,将会删除整个表的数据。

-- 单表删除
delete from beauty where phone like '%9';

-- 多表删除
-- sql92 语法,如果只删除表1中的内容,在from前填写表1的别名。如果需要删除两张表中的数据,则需要两个别名都写。
delete 别名1, 别名2 from1 别名1,2 别名2 where 连接条件 and 筛选条件;
-- 案例:
delete bo, b from boys bo, beauty b where bo.id = b.boyfriend and bo.boyName = '张无忌';

-- sql99 语法,如果只删除表1中的内容,在from前填写表1的别名。如果需要删除两张表中的数据,则需要两个别名都写。
delete 别名1, 别名2 from1 别名1 inner join2 别名2 where 连接条件 and 筛选条件;
-- 案例:
delete bo, b from boys bo inner join beauty b on bo.id = b.boyfriend where bo.boyName = '张无忌';

2、方式二 truncate

语法truncate table 表名;只能删除整个表,不能加where条件。也叫清空数据,效率比delete from 表名;高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值