第10章:数据处理增删改

一、插入数据

CREATE TABLE emp1 (
  id int(11) ,
  name varchar(15) ,
  hire_date date ,
  salary double(10,2) 
)

1.添加一条数据

①没有指明添加的字段,一定按照顺序添加

insert into emp1
values(1,'wang','2000-4-4',5900)

②指明添加的字段(推荐)

insert into emp1(id,name)
values(1,'li')

没有赋值的字段,值为null

③添加多条记录—效率高

insert into emp1(id,name)
values
(1,'li'),
(2,'liu'),
(3,'rui')

字符和日期要有单引号

2.查询的结果插入到表中

insert into 表名

select 语句

插入的字段的数量和表名的数量要对应,并且查询的字段长度不能高于添加的字段长度

insert into emp1(id,name,salary,hire_date)
select employee_id,last_name,salary,hire_date
from employees
where employees.department_id in (70,60);

二、更新数据

update 表名

set 字段=xxx

where 条件

1.把id=104的员工的雇佣日期修改为当前的日期

update emp1

set hire_date=now()

where id=104

2.没有where就是批量修改数据

3.把Jim的雇佣日期改为2008-08-08,工资改为5600

update emp1

set hire_date='2008-08-08',salary=5600

where name='Austin'

4.将表中name包含字符a的工资提薪20%

update emp1

set salary=salary*1.2

where name like '%a%'

5.修改数据失败:约束的影响

三、删除数据

1.格式

delete from 表名

where 条件

2.删除数据可能因为约束的影响,导致删除失败

DML默认不回滚。执行之前设置了 set autocommit = false,可以实现回滚。到最近的commit之后。

四、MySQL9新特性:计算列

1.应用:a的列值为1,b的列值为2,c列的值是a+b

2.举例:定义数据表tb1,然后定义字段id,字段a,字段b和字段c。其中c是计算列,计算a+b的值

create table tb1(

id int,

a int,

b int,

c int generated always as (a+b) virtual

);

插入

insert into tb1(a,b)

values(100,200);

五、综合案例

# 1、创建数据库test01_library

create database if not exists test01_library character set ‘utf8’;

# 2、创建表 books,表结构如下:

 

use test01_library;

create table books (

id int,

name varchar(50),

authors varchar(100),

price float,

pubdate year,

note varchar(100),

num int

);

# 3、向books表中插入记录

 

# 1) 不指定字段名称,插入第一条记录

insert into books

values(1,'Tal of AAA','Dickes',23,'1995','novel',11);

# 2) 指定所有字段名称,插入第二记录

insert into books(id,name,authors,price,pubdate,note,num)

values(2,'EmmaT','Jane lura',35,'1993','joke',22);

# 3) 同时插入多条记录《剩下的所有记录)

insert into books(id,name,authors,price,pubdate,note,num)

values

(3,'Story of Jane','Jane Tim',40,'2001','novel',0),

(4,'Lovey Day ','George Byron',20,'2005','novel',30),

(5,'Old land','Honore Blade',35,'2010','law',0),

(6,'The Battle','Upton Sara',35,'1999','medicine',40),

(7,'Rose Hood','Richard haggard',35,'2008','cartoon',28);

# 4、将小说类型(nove1)的书的价格都增加5.

update books

set price = price+5

where note='novel'

# 5、将名称为EmmaT的书的价格改为40.并将说明改为drama.

update books

set price=40,note='drama'

where name='EmmaT'

# 6、删除库存为0的记录。

delete from books

where num=0

# 7、统计书名中包含a字母的书

select name

from books

where name like '%a%'

# 8、统计书名中包含a字母的书的数量和库存总量

select count(*),sum(num)

from books

where name like '%a%'

# 9、找出“novel"类型的书。按照价格降序排列

select name,note,price

from books

where note='novel'

order by price desc;

# 10、查询图书信息,按照库存量降序排列,如果库存量相同的按照note升序排列

select name,num,note

from books

order by num desc,note asc

# 11、按照note分类统计书的数量(种类)

select note,count(*)

from books

group by note;

# 12、按照note分类统计书的库存量,显示库存超过30本的

select note,sum(num)

from books

group by note having sum(num)>30;

# 13、查询所有图书。每页显示5本。显示第二页

limit(页码数-1)*页数,页数

select *

from books

limit 5,5;

# 14、按照note分类统计书的库存量,显示库存量最多的

select note,sum(num) as "all_num"

from books

group by note

order by all_num desc

limit 0,1

# 15、查询书名达到10个字符的书。不包括里面的空格

select *

from books

where char_length(replace(name,' ',''))>10

# 16、查询书名和类型,其中note值为novel显示小说,law显示法律,medicine显示医药,cartoon最示卡通,joke显示笑话

select name,

case note

when 'novel' then'小说'

when 'law' then'法律'

when 'medicine' then'医药'

when 'cartoon' then'卡通'

when 'joke' then'笑话'

else note

end as "note"

from books

# 17、查询书名、库存。其中num值超过30本的,显示滞销,大于0并低于10的,显示畅销,为0的显示需要无货

select name,num,

case when num>30 then'滞销'

when num>0 and num<10 then'畅销'

when num=0 then'无货'

else '正常'

end as "num_degree"

from books

# 18、统计每一种note的库存量。 并合计总量

select IFNULL(note,'合计总数') as note,sum(num) as "库存量"

from books

group by note with rollup

# 19、统计每一种note的数量(种类),并合计总量

SELECT IFNULL(note,'合计总数') AS note,COUNT(*) FROM books GROUP BY note WITH ROLLUP;

# 20、统计库存量前三名的图书

select *

from books

order by num desc

limit 0,3

# 21、找出最早出版的一本书

select name,min(pubdate)

from books

SELECT * FROM books ORDER BY pubdate ASC LIMIT 0,1;

# 22、找出novel中价格最高的一本书

select name,max(price)

from books

where note='novel'

SELECT * FROM books WHERE note = 'novel' ORDER BY price DESC LIMIT 0,1;

# 23、找出书名中字数最多的一本节,不含空格

select name

from books

order by char_length(replace(name,' ','')) desc

limit 0,1

六、练习

练习1

#1. 创建数据库dbtest11

CREATE DATABASE IF NOT EXISTS dbtest11 CHARACTER SET 'utf8';

#2. 运行以下脚本创建表my_employees

USE dbtest11;

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

);

#3. 显示表my_employees的结构

desc my_employees

 

#4. my_employees表中插入下列数据

insert into my_employees(id,first_name,last_name,userid,salary)

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)

#5. users表中插入数据

 

insert into users(id,userid,department_id)

values

(1,'Rpatel',10),

(2,'Bdancs',10),

(3,'Bbiri',20),

(4,'Cnewman',30),

(5,'Aropebur',40)

#6. 3号员工的last_name修改为“drelxer”

update my_employees

set last_name='drelxer'

where id = 3

#7. 将所有工资少于900的员工的工资修改为1000

update my_employees

set salary = 1000

where salary<900

#8. useridBbiriuser表和my_employees表的记录全部删除

delete e,u

from my_employees e

join users u on e.userid=u.userid

where u.userid='Bbiri'

#9. 删除my_employeesusers表所有数据

delete my_employees;

delete users;

#10. 检查所作的修正

SELECT * FROM my_employees;

SELECT * FROM users;

#11. 清空表my_employees

truncate table my_employees

练习2

# 1. 使用现有数据库dbtest11

use dbtest11;

# 2. 创建表格pet

 

create table pet(

name varchar(20),

owner varchar(20),

species varchar(20),

sex char(1),

birth year,

death year

);

# 3. 添加记录

 

insert into pet(name,owner,species,sex,birth,death)

values

('Fluffy','harold','Cat','f','2003','2010'),

('Claws','gwen','Cat','m','2004',null),

('Buffy',null,'Dog','f','2009',null),

('Fang','benny','Dog','m','2000',null),

('bowser','diane','Dog','m','2003','2009'),

('Chirpy',null,'Bird','f','2008',null)

# 4. 添加字段:主人的生日owner_birth DATE类型。

alter table pet

add owner_birth DATE AFTER owner

 

# 5. 将名称为Claws的猫的主人改为kevin

update pet

set owner='kevin'

where name='Claws' and species='cat'

# 6. 将活着的狗的主人改为duck

update pet

set owner='duck'

where species='Dog' and death is null;

# 7. 查询没有主人的宠物的名字;

select name

from pet

where owner is null

# 8. 查询已经死了的cat的姓名,主人,以及去世时间;

select name,owner,species,death

from pet

where species='Cat' and death is not null;

# 9. 删除已经死亡的狗

delete from pet

where species='Dog' and death is not null

# 10. 查询所有宠物信息

select * from pet

练习3

# 1. 使用已有的数据库dbtest11

use dbtest11;

# 2. 创建表employee,并添加记录

 

create table employee(

id int,

name varchar(30),

sex varchar(2),

tel varchar(20),

addr varchar(50),

salary double(10,2)

);

insert into employee

values

(10001,'张一一','男','13456789000','山东青岛',1001.58),

(10002,'刘小红','女','13454319000','河北保定',1201.21),

(10003,'李四','男','0751-1234567','广东佛山',1004.11),

 (10004,'刘小强','男','0755-5555555','广东深圳',1501.23),

(10005,'王艳','男','020-1232133','广东广州',1405.16);

# 3. 查询出薪资在1200~1300之间的员工信息。

 select *

from employee

where salary >=1200 and salary<=1300

# 4. 查询出姓的员工的工号,姓名,家庭住址。

 select id,name,addr

from employee

where name like '%刘%'

# 5. 李四的家庭住址改为广东韶关

update employee

set addr='广东韶关'

where name='李四'

# 6. 查询出名字中带的员工

select *

from employee

where name like '%小%'

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值