Mysql

一、数据模型

二、SQL简介

三、数据定义语言-DDL

1.DDL操作数据库

2.DDL-操作表结构

1.创建

sql 复制代码
create table db_user(
    id int comment 'id',
    username varchar(20) comment '姓名',
    age int comment '年龄',
    gender char(1) comment '性别'
)comment '用户表'



-----------------------------------------------------------------------------

create table db_user(
                        id int primary key auto_increment comment 'id',
                        username varchar(20)  not null  unique comment '姓名',
                        name varchar(20) not null ,
                        age int comment '年龄',
                        gender char(1) default '男' comment '性别'
)comment '用户表'

2.数据类型

1.数值类型
2.字符串类型
3.日期,时间类型
sql 复制代码
CREATE TABLE `tb_emp` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID\n',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  `password` varchar(32) DEFAULT '123456' COMMENT '密码',
  `name` varchar(10) NOT NULL COMMENT '姓名',
  `column_5` int DEFAULT NULL,
  `gender` tinyint unsigned NOT NULL DEFAULT '1' COMMENT '性别  1:男   2:女',
  `img` varchar(300) DEFAULT NULL COMMENT '图像的URL',
  `job` tinyint unsigned DEFAULT NULL COMMENT '职位 1.总经理 2.主管 3.经理 4.职员',
  `entrydate` date DEFAULT NULL COMMENT '入职日期',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `tb_emp_username_uindex` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

3.表操作

1.查询
2.修改
sql 复制代码
alter table tb_emp add qq varchar(10) comment 'QQ';
alter table tb_emp modify qq varchar(13) comment 'QQ';
alter table tb_emp change qq qq_num varchar(13) comment 'QQ';
alter table tb_emp drop  column  qq_num;
rename table  tb_emp
3.删除

四、数据操作语言-DML

1.INSERT 添加数据

sql 复制代码
insert into  emp (username,name,gender,create_time,update_time) values ('wuji','张无忌','1',now(),now());
insert into emp (id, username, password, name, gender, img, job, entrydate, create_time, update_time)
 values (2,'zhiruo','123','周芷若',2,'1.jpg',1,'2010-01-01',now(),now());
insert into emp
values (3,'zhiruo2','123','周芷若',2,'1.jpg',1,'2010-01-01',now(),now());

insert into  emp (username,name,gender,create_time,update_time) values ('weiyixiao','韦一笑','1',now(),now()),('wanghuifan','王哥','1',now(),now());

2.修改数据(更新)

sql 复制代码
update emp set name='张四',update_time=now()  where id=1;
update emp set entrydate='2010-01-01',update_time=now() ;

3.删除数据

sql 复制代码
delete from emp where id=1;
delete from db_emp ;

五、数据查询语言-DQL

1.基本查询

sql 复制代码
select * from tb_emp;
select name as '姓名',entrydate as '入职日期' from tb_emp;
select name  '姓名',entrydate  '入职日期' from tb_emp;
select distinct  job from tb_emp;

2.条件查询

sql 复制代码
select name,entrydate from tb_emp;
select id, username, password, name, gender, image, job, entrydate, create_time, update_time  from tb_emp;
select * from tb_emp;
select name as '姓名',entrydate as '入职日期' from tb_emp;
select name  '姓名',entrydate  '入职日期' from tb_emp;
select distinct  job from tb_emp;
select  * from tb_emp where name='杨逍';
select * from tb_emp where id<=5;
select * from tb_emp where job is null;
select * from tb_emp where job is not null;
select * from tb_emp where password !='123456';
select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01';
select * from tb_emp where entrydate>='2000-01-01' and entrydate<='2010-01-01';
select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01' and gender=2;
select * from tb_emp where job=2 or job=3 or job=4;
select * from tb_emp where job in (2,3,4);
select * from tb_emp where name like '__';
select * from tb_emp where name like '张%';

3.聚合函数

sql 复制代码
-- 1.统计该企业员工的数量 -- count
-- A.count(字段)
select  count(id) from tb_emp;
-- B.count(常量)
select count(0) from tb_emp;
-- C.count(*)
select count(*) from tb_emp;
-- 2.统计该企业最早入职的员工
select min(entrydate) from tb_emp;
-- 3.统计该企业最迟入职的员工
select max(entrydate) from tb_emp;
-- 4.统计该企业员工ID的平均值
select avg(id) from tb_emp;
-- 5.统计该企业员工ID的和
select sum(id) from tb_emp;

4.分组查询

sql 复制代码
-- 分组后select 只能返回 分组字段 和聚合函数
select gender,count(*) from tb_emp group by gender;

select job,count(*) as job_num from tb_emp where entrydate<='2015-01-01' group by job having job_num>=2;

5.排序查询

sql 复制代码
select * from tb_emp order by entrydate;
select * from tb_emp order by entrydate desc ;
-- 根据入职时间 对公司的员工进行升序排序 ,入职时间相同 ,在按照更新时间进行降序排序
select * from tb_emp order by entrydate,update_time desc ;

6.分页查询

sql 复制代码
-- 1. 从起始索引为 0开始 查询员工数据,分页展示5条记录
select * from tb_emp limit 0,5;
-- 2.长训 第一页员工数据,每层展示5条记录
select * from tb_emp limit 0,5;
-- 3.查询第二页  员工数据 ,每页展示5 条
select * from tb_emp limit 5,5;
-- 4.查询第三页 员工数据 ,每页展示五条
select * from tb_emp limit 10,5;
-- 起始索引计算公式 (页码-1)*每页所展示的记录数



select * from tb_emp where name like '张%' and gender=1 and entrydate between '2000-01-01' and '2015-12-31' order by update_time desc limit 0,10;

7.流程控制函数

sql 复制代码
select * from tb_emp where name like '张%' and gender=1 and entrydate between '2000-01-01' and '2015-12-31' order by update_time desc limit 0,10;
-- if(条件表达式,true 取值 ,false 取值)
select if(gender=1,'男性员工','女性员工')as'性别', gender ,count(*)  from tb_emp group by gender;
-- case表达式  when 值1 then 结果1 ,when 值2 then 结果2...else ...end
select
      ( case job when 1 then '班主任' when 2 then'讲师' when 3 then '学工主管' when 4 then '教研主管'else '未分配职位' end) as '职位'
    ,job,count(*) from tb_emp group by job;

六、多表关系

1.一对多

sql 复制代码
create table tb_emp (
                        id int unsigned primary key auto_increment comment 'ID',
                        username varchar(20) not null unique comment '用户名',
                        password varchar(32) default '123456' comment '密码',
                        name varchar(10) not null comment '姓名',
                        gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
                        image varchar(300) comment '图像',
                        job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管',
                        entrydate date comment '入职时间',
                        dept_id int unsigned ,
                        create_time datetime not null comment '创建时间',
                        update_time datetime not null comment '修改时间'

) comment '员工表';
create table tb_dept(
    id int unsigned primary key auto_increment,
    name varchar(10) not null  unique,
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
)

2.一对多-外键

3.一对一

sql 复制代码
-- ===========================================一对一=====================================
create table tb_user(
                        id int unsigned  primary key auto_increment comment 'ID',
                        name varchar(10) not null comment '姓名',
                        gender tinyint unsigned not null comment '性别, 1 男  2 女',
                        phone char(11) comment '手机号',
                        degree varchar(10) comment '学历'
) comment '用户信息表';

insert into tb_user values (1,'白眉鹰王',1,'18812340001','初中'),
                           (2,'青翼蝠王',1,'18812340002','大专'),
                           (3,'金毛狮王',1,'18812340003','初中'),
                           (4,'紫衫龙王',2,'18812340004','硕士');


create table tb_user_card(
                             id int unsigned  primary key auto_increment comment 'ID',
                             nationality varchar(10) not null comment '民族',
                             birthday date not null comment '生日',
                             idcard char(18) not null comment '身份证号',
                             issued varchar(20) not null comment '签发机关',
                             expire_begin date not null comment '有效期限-开始',
                             expire_end date comment '有效期限-结束',
                             user_id int unsigned not null unique comment '用户ID',
                             constraint fk_user_id foreign key (user_id) references tb_user(id)
) comment '用户信息表';

insert into tb_user_card values (1,'汉','1960-11-06','100000100000100001','朝阳区公安局','2000-06-10',null,1),
                                (2,'汉','1971-11-06','100000100000100002','静安区公安局','2005-06-10','2025-06-10',2),
                                (3,'汉','1963-11-06','100000100000100003','昌平区公安局','2006-06-10',null,3),
                                (4,'回','1980-11-06','100000100000100004','海淀区公安局','2008-06-10','2028-06-10',4);

4.多对多

sql 复制代码
--  ======================================多对多=============================
create table tb_student(
                           id int auto_increment primary key comment '主键ID',
                           name varchar(10) comment '姓名',
                           no varchar(10) comment '学号'
) comment '学生表';
insert into tb_student(name, no) values ('黛绮丝', '2000100101'),('谢逊', '2000100102'),('殷天正', '2000100103'),('韦一笑', '2000100104');


create table tb_course(
                          id int auto_increment primary key comment '主键ID',
                          name varchar(10) comment '课程名称'
) comment '课程表';
insert into tb_course (name) values ('Java'), ('PHP'), ('MySQL') , ('Hadoop');


create table tb_student_course(
                                  id int auto_increment comment '主键' primary key,
                                  student_id int not null comment '学生ID',
                                  course_id  int not null comment '课程ID',
                                  constraint fk_courseid foreign key (course_id) references tb_course (id),
                                  constraint fk_studentid foreign key (student_id) references tb_student (id)
)comment '学生课程中间表';

insert into tb_student_course(student_id, course_id) values (1,1),(1,2),(1,3),(2,2),(2,3),(3,4);

七、多表查询

1.内连接

sql 复制代码
select tb_emp.name,tb_dept.name from  tb_emp,tb_dept where tb_emp.dept_id=tb_dept.id;
select tb_emp.name,tb_dept.name from tb_emp inner join tb_dept on tb_emp.dept_id=tb_dept.id;
select e.name,d.name from  tb_emp as e,tb_dept as d where e.dept_id=d.id;
select e.name,d.name from tb_emp as e inner join tb_dept as d on e.dept_id=d.id;

2.外连接

sql 复制代码
select e.name,d.name from tb_emp  e left join tb_dept  d on e.dept_id=d.id;
select d.name,e.name from tb_dept d right join tb_emp e on d.id=e.dept_id;

3.子查询

1.标量子查询

sql 复制代码
-- 标量子查询
-- A 查询 "教研部" 的所有员工信息
-- a.查询 教研部 的部门ID -tb_dept
select id from tb_dept where name='教研部';
-- b 再查询该部门ID下的员工信息
select * from db04.tb_emp where dept_id=2;
select * from db04.tb_emp where dept_id =(select id from tb_dept where name='教研部');


-- 查询在"方东白"入职之后的员工信息
select entrydate from db04.tb_emp where name='方东白';
select * from db04.tb_emp where entrydate>'2012-11-01';
select * from db04.tb_emp where entrydate>(select entrydate from db04.tb_emp where name='方东白');

2.列子查询

sql 复制代码
-- 列子查询
-- A 查询 教研部 和 咨询部 的所有员工信息
-- a 查询 教研部 和 咨询部的部门ID
select id from tb_dept where name='教研部' or name='咨询部';
-- b 根据部门ID 查询该部门下的员工信息
select * from db04.tb_emp where dept_id in (2,3);
select * from db04.tb_emp where dept_id in (select id from tb_dept where name='教研部' or name='咨询部');

3.行子查询

sql 复制代码
-- 行子查询
-- A.查询与"韦一笑"的入职日期 及 职位都相同的员工信息
-- a.查询"韦一笑"的入职日期及职位
select  entrydate,job from db04.tb_emp where name='韦一笑';
-- b 查询与其入职日期及职位都相同的员工信息;
select * from db04.tb_emp where entrydate='2007-01-01' and job=2;
select * from db04.tb_emp where (entrydate,job)=('2007-01-01',2);
select * from db04.tb_emp where  (entrydate,job)=(select  entrydate,job from db04.tb_emp where name='韦一笑');

4.表子查询

sql 复制代码
-- 表子查询
-- A. 查询入职日期是 2006-01-01 之后的员工信息,及其部门名称
-- a. 查询入职日期是 2006-01-01 之后的员工信息
select * from db04.tb_emp where entrydate>'2006-01-01';

-- b.查询这部分的员工信息及部门名称
select e.*,d.name from (select * from db04.tb_emp where entrydate>'2006-01-01') e ,tb_dept d where e.dept_id=d.id;

4.案例

sql 复制代码
select d.name,d.price,c.name
from dish d ,category c
where d.category_id=c.id and d.price<10;

select d.name,d.price,c.name
from dish d left join category c
on d.category_id = c.id where d.price >=10 and d.price<50 and d.status=1;

select c.name,max(price)
from dish d ,category c
where d.category_id=c.id
group by c.name;

select c.name,count(*) as  c2
from dish d ,category c
where d.category_id=c.id and d.status=1
group by c.name having c2>3;

select s.name,s.price,d.name,d.price,sd.copies
from setmeal s,setmeal_dish sd,dish d
where s.id=sd.setmeal_id and sd.dish_id=d.id and s.name='商务套餐A';

select avg(price)from dish;
select name ,price from dish where price <(select avg(price)from dish);

八、事务

1.介绍&操作

sql 复制代码
-- 开启事务
start transaction ;
-- 删除部门
delete from tb_dept where id=2;
-- 删除该 部门下的员工
delete from db04.tb_emp where id=2;
-- 提交事务
commit ;

-- 回滚事务
rollback ;

2.事务的特性

九、mySql索引

1.介绍&优缺点

2.结构

3.语法

sql 复制代码
-- 创建 :为表的name字段建立一个索引
create index idx_emp_name on tb_emp(name);

-- 查询 : 查询 表的索引信息
show index from db_03.tb_emp;

-- 删除 删除 name 字段的索引
drop index idx_emp_name on db_03.tb_emp;
相关推荐
Cikiss11 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
小小不董25 分钟前
《Linux从小白到高手》理论篇:深入理解Linux的网络管理
linux·运维·服务器·数据库·php·dba
无敌少年小旋风1 小时前
MySQL 内部优化特性:索引下推
数据库·mysql
柒小毓1 小时前
将excel导入SQL数据库
数据库
bug菌¹1 小时前
滚雪球学Oracle[2.5讲]:数据库初始化配置
数据库·oracle·数据库初始化·初始化配置
一休哥助手1 小时前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
翔云1234561 小时前
MVCC(多版本并发控制)
数据库·mysql
代码敲上天.2 小时前
数据库语句优化
android·数据库·adb
盒马盒马2 小时前
Redis:zset类型
数据库·redis
静听山水2 小时前
mysql语句执行过程
数据库·mysql