
本文共 2073 字,大约阅读时间需要 6 分钟。
DML语言
数据操作语言包括插入、修改和删除三种操作。
一、插入语句
插入语句用于向数据库表中插入新的记录。其语法如下:
insert into 表名(列名,…) values(值1,…);
示例
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id) values(13,‘migo’,‘女’,‘1990-4-23’,‘18988888888’,NULL,2);
注意事项
- 方式一:经典插入法
- 方式二:使用set语法
示例
方式一: insert into beauty(id,NAME,sex,photo,boyfriend_id) values(14,‘offset’,‘女’,9);
方式二: insert into beauty(id,NAME,sex,photo,boyfriend_id) set id=14, NAME=‘offset’,photo=9;
常见问题
示例
省略列名方式: insert into beauty values(18,‘travisscott’,‘女’,NULL,‘999’,NULL,NULL);
二、修改语句
修改语句用于更新数据库中现有记录的值。其语法如下:
单表修改
update 表名 set 列=新值,列=新值,… where 筛选条件;
多表修改
支持两种语法:
- SQL92语法
- SQL99语法
示例
SQL92语法: update boys bo set b.boyfriend_id=2 where bo.id is null;
SQL99语法: update boys inner join beauty b on bo.id = b.boyfriend_id set b.phone=‘222’ where bo.boyName=‘张无忌’;
三、删除语句
删除语句用于从数据库中删除记录。其语法如下:
单表删除
delete from 表名 where 筛选条件;
多表删除
支持两种语法:
- SQL92语法
- SQL99语法
示例
SQL92语法: delete bo, b from boys bo join beauty b on bo.id = b.boyfriend_id where bo.boyName=‘黄晓明’;
SQL99语法: delete boys from boys b inner join beauty bb on b.id = bb.boyfriend_id where b.boyName=‘张无忌’;
注意事项
delete
语句可以加where条件,但truncate
不能。truncate
删除效率高,但无法回滚。truncate
适合清空表中所有数据。
示例: truncate table boys;
技术案例
use myemployees;
- 查看表结构
- 插入数据(方式一)
- 插入数据(方式二)
- 修改员工信息
- 调整薪资
- 删除相关数据
- 删除所有数据
- 检查数据
desc my_employees;
insert into my_employees values(1,‘patel’,‘Ralph’,‘Rpatel’,895), (2,‘Dancs’,‘Betty’,‘Bdancs’,860), (3,‘Biri’,‘Ben’,‘Bbiri’,1100), (4,‘Newman’,‘Chad’,‘Cnewman’,1550), (5,‘Ropeburn’,‘Audrey’,‘Aropebur’,1550);
insert into my_employees select 1,‘patel’,‘Ralph’,‘Rpatel’,895union select 2,‘Dancs’,‘Betty’,‘Bdancs’,860...
update my_employees set last_name=‘drelxer’ where id=3;
update my_employees set salary=1000 where salary<900;
delete u, e from users u join my_employees e on u.userid=e.userid where userid=‘Bbiri’;
delete from my_employees;delete from users;
select * from my_employees;select * from users;
10.清空表
truncate table my_employees;
发表评论
最新留言
关于作者
