您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL精讲之二:DML数据操作语句

2024/6/8 19:00:21发布23次查看
免费学习推荐:mysql视频教程
文章目录
一、插入insert二、修改update三、删除delete/truncate四、dml语句练习题一、插入insert
方式一:insert into 表名(列名,...) values(值1,...);
方式二: insert into 表名 set 列名=值,列名=值...
比较是否支持插入多行是否支持子查询
方式一 √ ×
方式二 √ ×
【插入方式一】# 1.插入的值的类型要与列的类型一致或兼容insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)value(13,'双笙','女','1999-02-15','13868666666',null,2);# 2.列的顺序可以调换insert into beauty(name,id,sex,borndate,phone,photo,boyfriend_id)value('徐婕',14,'女','1999-02-16','13868666661',null,2);# 3.可以省略列名,列的顺序和表中列的顺序一致insert into beautyvalue(16,'金克斯','女','2000-02-16','13868666661',null,2);# 4.支持子查询insert into beauty(id,name,phone)select id,boyname,'110'from boys where id<3; # 本例需将beauty表的主键删除---------------------------------------------------------------【插入方式二】insert into beauty set id=19,name='花花',phone='666';
二、修改update
1、修改单表的记录语法:
update 表名
set 列=新值,列=新值,...
where 筛选条件;
执行顺序:update > where > set2、修改多表的记录语法:
update 表1 别名
[inner|left|right] join 表2 别名
on 连接条件
set 列=新值,列=新值,...
where 筛选条件;
【修改单表的记录】# 修改beautty表中姓周的女生电话为15888888888update beautyset phone='15888888888'where name like '%周%';【修改多表的记录】# 将张无忌的女朋友的电话号改为2333update boys b1join beauty b2 on b1.id=b2.boyfriend_idset b2.phone='2333'where b1.boyname='张无忌';
三、删除delete/truncate
delete
单表删除:delete from 表名 where 筛选条件
多表删除:
delete 表1别名,表2别名
from 表1 别名
inner|left|right join 表2 别名 on 连接条件
where 筛选条件;
truncate
清空单表:truncate table 表名;
注意:truncate删除没有返回值,delete删除有返回值。
比较是否可以加where条件是否可以回滚是否有返回值效率表中有自增长列时
delete √ √ √ 一般 用delete删除,再插入数据,自增长的值从断点开始
truncate × × × 略高于delete 用truncate删除,再插入数据,自增长的值从1开始
【delete】# 1.单表删除# 删除以9结尾的女生信息delete from beauty where phone like '%9';# 2.多表删除# 删除黄晓明的信息以及他女朋友的信息delete b1,b2from beauty b1inner join boys b2on b1.boyfriend_id=b2.idwhere b2.boyname='黄晓明';【truncate】# 仅支持单表删除全部数据truncate table boys;
四、dml语句练习题
学习了dml语言语句的增删改,尝试完成下面的习题:
答案:
1、执行下面的sql语句
create table my_employees(
id int(10),
first_name varchar(10),
last_name varchar(10),
userid varchar(10),
salary double(10,2)
);
create table users(
id int,
userid varchar(10),
department_id int
);
2.
desc my_employees
3.
方式一:
insert into my_employees
values(1,‘patel’,‘ralph’,‘rpatel’,895),
(2,‘dancs’,‘betty’,‘bdancs’,860),
(3,‘biri’,‘ben’,‘bbiri’,1100),
(4,‘newman’,‘chad’,‘cnewman’,750),
(5,‘ropeburn’,‘audrey’,‘aropebur’,1550);
truncate table my_employees;
方式一:
insert into my_employees
select 1,‘patel’,‘ralph’,‘rpatel’,895 union
select 2,‘dancs’,‘betty’,‘bdancs’,860 union
select 3,‘biri’,‘ben’,‘bbiri’,1100 union
select 4,‘newman’,‘chad’,‘cnewman’,750 union
select 5,‘ropeburn’,‘audrey’,‘aropebur’,1550;
4.
insert into users
value(1,‘rpatel’,10),
(2,‘bdancs’,10),
(3,‘bbiri’,20),
(4,‘cnewman’,30),
(5,‘aropebur’,40);
5.
update my_employees
set last_name=‘drelxer’
where id=3;
6.
update my_employees
set salary=1000
where salary<900;
7.
delete u,e
from users u
join my_employees e on u.userid=e.userid
where u.userid=‘bbiri’;
8.
delete from my_employees;
delete from users;
9.
select * from my_employees;
select * from users;
10.
truncate table my_employees;
更多相关免费学习推荐:mysql教程(视频)
以上就是mysql精讲之二:dml数据操作语句的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product