Mysql的使用

100 阅读2分钟

mysql

创建数据库

-- 创建数据库: 创建一个名为db1的数据库
create database db1;
​
​
-- 切换数据库:
use db1;

增删改数据

所有字段插入值

insert into tb_emp values (null,'忘了爱','111','杨宁',1,'/img',1,'2002-12-21',now(),now());

批插

insert into tb_emp values (null,'忘了爱1','111','杨宁2',1,'/img',1,'2002-12-21',now(),now())
                        ,(null,'忘了爱2','111','杨宁3',1,'/img',1,'2002-12-21',now(),now());

修改

update tb_emp set name = '传智',entrydate='2022-01-01' where id =1;

删除

delete from tb_emp where id = 1;

查数据

交叉连接(笛卡尔集)

select * from emp,dept;

内连接(分为隐式和显式,但只会展示表与表之间的交集)

image.png

select * from emp e inner join dept d on e.dept_id = d.id;

外连接(分左外跟右外)

image.png

左外连接(优先匹配左表数据中的字段)
select * from emp e left join dept d on e.dept_id = d.id;
右外连接(优先匹配右表数据中的字段)
select * from emp e right join dept d on e.dept_id = d.id;

子查询

image.png

image.png

-- 1: 查询工资小于平均工资的员工有哪些?(子查询结果为一个值  标量子查询)
select * from emp where salary < (select avg(salary) from emp);
-- 2: 查询工资大于5000的员工,所在部门的名字 (子查询结果为多个值  列子查询)
select name from dept where id in(select distinct dept_id from emp where salary > 5000);
-- 3: 查询出2011年以后入职的员工信息,包括部门信息 (子查询结果为一张表   表子查询)
select * from (select * from emp where join_date >= '2011-01-01') as l
    left join dept as d on l.dept_id = d.id;

复杂查询

# 菜品表:dish、分类表:category、套餐表:setmeal
​
-- 1. 查询价格低于 10元 的菜品的名称 、价格 及其 菜品的分类名称
select d.name,d.price,c.name from dish d
    inner join category c on d.category_id = c.id where d.price <10;
​
-- 2. 查询所有价格在 10元(含)到50元(含)之间 且 状态为'起售'的菜品名称、价格 及其 菜品的分类名称
# (即使菜品没有分类, 也需要将菜品查询出来).
select d.name,d.price,c.name from dish d left join category c on d.category_id = c.id
where d.price between 10 and 50 and d.status = 1;
​
-- 3. 查询出 "商务套餐A" 中包含了哪些菜品 (展示出套餐名称、价格, 包含的菜品名称、价格、份数)
# 三表关联,重复使用join
select s.name,s.price,d.name,d.price,sd.copies
from setmeal s
         join setmeal_dish sd on s.id = sd.setmeal_id
         join dish d on d.id = sd.dish_id
where s.name = '商务套餐A';
​
-- 4. 查询出低于菜品平均价格的菜品信息 (展示出菜品名称、菜品价格)
select name,price from dish where price < (select avg(price) from dish) order by price;
​
-- 5. 查询每个分类下最贵的菜品, 展示出分类的名称、最贵的菜品的价格
select c.name,max(d.price)
from dish d
         join category c on d.category_id = c.id
group by c.name;
​
-- 6. 查询各个分类下状态为'起售' , 并且该分类下 菜品总数量大于等于3的分类名称
select c.name,count(*) count
from category c
         join dish d on c.id = d.category_id
where c.status = 1
group by c.name
having count >=3;

外键

创建表之后单独添加

​
# alter table 表名 add [constraint 约束名]  foreign key (列名)  references  主表(主键)
alter table student
    add constraint class_id_fk foreign key (class_id) references class (id);

创建表的时候,添加外键

create table user_card(
    id int unsigned primary key  auto_increment,
    nationality varchar(10) not null ,
    birthday date not null ,
    idcard char(18) not null ,
    issued varchar(20) not null ,
    expire_begin date not null ,
    expire_end date,
    user_id int unsigned comment '用户id',
#     创建表的时候,添加外键 (user_id 和 user表的id)
    constraint userid_id_fk foreign key (user_id) references user(id)
);

Mysql中的事务

image.png

image.png

Mysql中的索引

数据库中的索引可以帮助数据库高效获取数据的数据结构

索引的创建

image.png

Mysql数据库支持结构有很多:Hsah索引、B+Tree索引、Full-Text索引等 一般默认的就是B+Tree结构

image.png

image.png

image.png