MySQL学习12(数据操作语言)

#数据操作语言

/*
  数据操作语言:
	插入:insert
	修改:update
	删除:delete
*/
# 插入语句
# 经典插入
/*
  语法:
	insert into 表名(字段1,字段2...) values (值1,值2,值3....);
*/
use girls;
select * from beauty;
insert into beauty(id,`name`,sex,borndate,phone,photo,boyfriend_id) 
values (13,'波老师','女','1990-4-26','15006782683',null,2);

# 不可以为null的列必须插入值,可以为null的列如何插入值
# 方式一
insert into beauty(id,`name`,sex,borndate,phone,photo,boyfriend_id) 
values (14,'东方不败',null,null,'15006782683',null,null);
# 方式二
insert into beauty(id,`name`,phone) 
values (15,'聂小倩','13276386095');

/*
  # insert语句方式二
	insert into 表名 set 列1=值,列2=值,列n=值;
*/
insert into beauty
set id = 16,`name`= '曦瑶',sex = '女',borndate = '1998-4-30',phone = '15006782683',
photo = null,boyfriend_id = 8;

/*
  区别:
	  -方式一支持批量插入,方式二不行
		-方式一支持子查询,方式二不支持
*/

/*
  修改语句
	
	修改单表记录:
	  update 表名
		set 列=新值,....
		where 筛选条件
		
	修改多表的记录
    语法:
	    -sql92语法:
		       update 表1 别名,表2 别名
					 set 列=值,....
				 	 where 连接条件
					 and 筛选条件
			-sql99语法:
           update 表1 别名
			     inner/left/right join 表2 别名
			     on 连接条件
			     set 列=值,....
			     where 筛选条件		 
*/
# 修改beauty表中姓范的女神的电话为1385555555
update beauty
set phone = '1385555555';
where `name` like '范%';

# 修改boys表中id号为30的名称为张飞,魅力值为10
update boys
set boyName = '张飞',userCP = 10
where id = 30;

# 修改黄晓明的女朋友的手机号为110
update beauty b1
inner join boys b2
on b1.boyfriend_id = b2.id
set phone = '110'
where boyName = '黄晓明';

# 修改没有男朋友的女神的男朋友编号为2号
update beauty b1
left join boys b2
on b1.boyfriend_id = b2.id
set boyfriend_id = 2
where b2.id is null;

/*
  删除语句
	方式一:delete
	  语法:
		  1.单表删除
			  delete from 表名
				where 筛选条件;
			2.多表删除
		    sql92语法:
			     delete from 表1 别名,表2 别名
					 where 连接条件
					 and 筛选条件
				sql99语法:
		       delete 表的别名 from 表1 别名
				   inner/left/right join 表2 别名
					 on 连接条件
					 where 筛选条件
					 
		方式二:truncate 语句 清空所有表记录
		  语法:
			  truncate table 表名;
		
*/
# 删除手机号以九结尾的女神信息
delete from beauty
where phone like '%9';

# 删除段誉的女朋友的信息
delete b2 from boys b1
inner join beauty b2
on b1.id = b2.boyfriend_id
where boyName = '段誉';

# 删除魅力值> 100的男神信息
truncate table boys;

/*
  truncate和delete的区别 (面试题)
	  1.delete可以加where筛选条件,truncate不能
		2.truncate删除效率比delete高一些
		3.假如要删除的表中有自增长列,如果使用delete删除后,再插入数据,自增长的列从断点开始
		而truncate删除后,自增长列从1开始
		4.truncate删除没有返回值,delete删除有返回值
		5.truncate删除不能回滚,delete可以
*/


  
#练习
# 1. 运行以下脚本创建表 my_employees
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. 显示表 my_employees 的结构
desc my_employees;

/*
  3. 向 my_employees 表中插入下列数据
    ID FIRST_NAME LAST_NAME   USERID   SALARY
     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
*/
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);

/*
  4. 向 users 表中插入数据
    1  Rpatel  10
		2  Bdancs  10
		3  Bbiri   20
		4 Cnewman  30
		5 Aropebur 40
*/
desc users;
insert into users
values (1,'Rpatel',10),
(2,'Bdancs',10),
(3,'Bbiri',20),
(4,'Cnewman',30),
(5,'Aropebur',40);

# 5. 将 3 号员工的 last_name 修改为“drelxer”
update my_employees
set last_name = 'drelxer'
where id = 3;

# 6. 将所有工资少于 900 的员工的工资修改为 1000
update my_employees
set salary = 1000
where salary < 900;

# 7. 将 userid 为 Bbiri 的 user 表和 my_employees 表的记录全部删除
delete u,m from users u
inner join my_employees m
on u.id = m.Id
where u.userid = 'Bbiri'
and m.Userid = 'Bbiri';

# 8. 删除所有数据
truncate table users;

# 9. 检查所作的修正
show global variables like '%general%' ;
set global general_log = on; # 开启监视

# 10. 清空表 my_employees
truncate table my_employees;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值