MySQL笔记

#知识点总结

第一章

-- 数据库管理系统:
– 数据文件
– 控制文件
– 日志文件
– 数据模型
– 网状数据库
– 层次数据库
– 关系数据库(主流数据库)
– 数据库核心是:表
列:属性*、字段、列
行:记录*、元组、行

– 数据库语序
1、数据库查询语序 -DQL
2、数据库操作语序 -DML 增加、删除、修改
3、数据库定义语序 -DDL 针对数据库的对象: 表、视图、序列、索引。。。。
4、数据库控制语序 -DCL 用户的权限、、
5、事务处理语序 -TPL
– 什么是事务
多个DML语序组成的整体,银行户头转账 *实时性
show databases; – 显示数据库, 显示所有MySQL数据库
select version(); – 使用数据库版本
use mysql; #使用数据库
show tables; – 数据库内表结构查询

第二章

1.字符串拼接 ‘字符串’/"字符串"
1. ‘字符串’+‘字符串’ (不推荐)
2. 连接函数
select concat(‘职位是:’,job) as ‘职位’ from emp;
2.别名
表示方法
1. 空格形式 (不推荐)
2. as
注意 空格(字符串)
3.运算 数值运算
+ - * / %
查询姓名年薪

 select ename ,sal*12 as 年薪 from emp;

注意: null如果参与运算 结果为null;
避免 : ifnull(); ifnull(查询列 , 替换值)
select ename ,(sal+ifnull(comm,0))*12 as 年薪 from emp;
4.条件查询
where 格式: select …from … where … order by… limit start, length;(start 起始位置可省略)
执行顺序 :from 。。。。 where 。。。 select 。。。order by…;
查询在10号部门工作的员工

select * from emp where deptno=10

逻辑关系 与 and /或 or / 非
and or结合 and 级别比or高
查询10号部门 或者工资高于2500 并且职位是manager的员工信息alter

select * from emp where (deptno=10 or sal >2500) and job = 'manager;

特殊比较运算符
between … and … 格式 : 列 between 下限 and 上限 包含两个端点值
– 查询emp 中月薪在1000-2000区间的员工信息
select * from emp where sal between 1000 and 2000;
比较数据: 数值/ 日期/ 字符
查询emp表中 在1982年之前入职的员工信息 时间放在字符串中

select * from emp where hiredate<'1982-1-1';

in :
等价于多个or组合 格式 列 in(值,值,值,。。。)
查询所有30号部门其主管编号是7902,7698,7788的员工信息

select * from emp where deptno = 30 and mgr in(7902,7698,7788);

like
模糊查询
通配字符 : 格式: 列 like’'通配字符和确定字符;
%;0-n个任意字符
_:1个任意字符
查询倒数第二字母是T 的员工信息alter

select * from emp where ename like "%t_";

is null 作用:筛选空值
查询emp表老板是谁

select * from emp where mgr is null;

not
取反 可以与之前的特殊比较运算符连用
格式
not between 。。。and。。。
not in(值、、)
not like 、、、
is not null
排序 order by…
升序(默认) order by。。。asc

select *from emp where deptno =10 order by sal asc;

降序 order by。。。desc

select *from emp where deptno =10 order by sal desc;

查询年收入高于15000的所有员工姓名 年收入 where中不能使用别名

select ename,(sal+ifnull(comm,0))*12  as 年收入 from emp 
		where (sal+ifnull(comm,0))*12>15000;

-------------------------------------------------------------------------------------------------------
多列排序
查询员工信息,按着部门升序,月薪降序排列
第二排序要依赖第一个排序基础上排列

select * from emp order by deptno asc , sal desc;

distinct 去除重复

select distinct deptno from emp;

多列去重 位置必须放在 select (distinct) 第一列 之间

select distinct deptno ,job from emp;

限制记录行数 (分页查询)
limit

select *from emp order by sal asc limit 0,5;

第三章

函数
sql函数分为 ;
单行函数、
多行函数(分组函数)
单行函数: 针对每一行进行变化,得到每一行结果。
虚表 dual

select 1 from dual;

-------------------------------------- 1 字符串 --------------------------------------
1、大小写转换 应用:验证码.

select upper('aHaaHhA') from dual; --  小写转大写alter
select lower('aHaaHhA') from dual;

2、字符串截取函数 select substring (字符串,截取起始位置(1开始),长度)

select substring('abcdefghijklmn',2,3) from dual;
select substring(job,1,3) from emp;

3、字符串查询函数 instr(str(在这个找str1),str1) 返回str1 在str中的位置

select instr('i like book' ,'bo') from dual;

4、字符拼接函数 concat(str1,str2,…) : mysql 可以实现多字符串在一起 Oracle 只能两个一起拼接

select concat('haha','heh','ehe') from dual;
select concat(('hah','aha'),('heh','ehe')) from dual;

concat_ws() 可以实现分隔符的形式

	select concat_ws('_','haha','he','xixi') from dual;

5、字符串替换 replace(s,s1,s2) s 中使用 s2 替换 s 中 s1 的字符串

	select replace('adfadfadfadfadf','df','sdf') from dual;

6、字符串长度获取 length() 返回的是字符串的字符个数

select length('qwerdf'),length('哈哈') from dual;

mysql utf-8 通过3位表示 每3位表示一个汉字
7、字符串补齐函数 lpad/rpad(s1,len,s2) 左/右 补齐函数 s1在左侧补充s2字符串补完长度为len

select lpad('qwe',6,'rt') from dual;
        select lpad('qwe',4,'rt') from dual;

8、trim 去除空格,或者去除前后字符

 select trim('   qwer   ') from dual;
select trim(trim('kiki'from'kiki   qwer   momo') )from dual;

-------------------------------------------------- 2 数值 --------------------------------------------------------
1、 round(x,y) 四舍五入 保留小数点后面y位。

	select round(3.0415926,5),round(-3.1415,3) ,round(5.1415,-1)from dual;

– round(x) 四舍五入 保留整数

	select round(3.04159) from dual;

2、 truncate(x,y) 截取、舍弃函数

select truncate(2.3456,2),truncate(2.3456,0),truncate(2.3456,-1)from dual;

3、mod(na,n2) 取余函数 n1/ n2 的余数

select mod(4,3),mod(4,-3),mod(-4,3),mod(-4,-3) from dual;

随机数 rand();

select rand() from dual;

向上向下取整

select ceil(3.145),floor(3.145) from dual;

-------------------------------------- 3 日期时间 --------------------------------------
1.now() 当前时间 sysdate() 系统时间 cur date() 当前日期 cur time 当前分钟

select now(),sysdate(),curdate(),curtime()from dual;

2.timediff(expr1,expr2) 返回相减时间差 datediff(expr1,expr2) 返回相减日期差

select truncate(datediff(curdate(),hiredate)/365,0) from emp;
select year(sysdate()) from dual;

3.date_add(date, interval expr type) 日期加时间间隔
date_sub(date, interval expr type) 日期减间间隔

 select date_add(sysdate(),interval 4 month) from dual;

查询工龄大于35年的员工信息

select *from emp
	 where hiredate < date_sub(curdate(),interval 35 year);

4.last_day(date) 当月最后一天

select last_day(sysdate()) from dual;

5.日期之间可以做差值运算 其余没意义

select last_day(now())-curdate() from dual;

6.extract() 抽取时间中某一部分

select extract(year from now()) from dual;
select extract(month from now()) from dual;

7.时间格式化 date_format(date,format) time_format(time,format)

select date_format(curdate(),'%Y/%m/%d') from dual

控制语句

case 判断值a 和值b 相等
格式
select case 值a when 值b then 结果1
when 值 then 结果2
else 结果 end

select case 2 when 1 then 'a' when 2 then 'b' else 'c'end from dual;

if
if(a,b,c) 成立第二个值 不成立第三个

 select if(1<2,3,4) from dual;

ifnull()

余数正负跟被除数有关

多行函数: 针对每一组的记录进行处理, 得到的是一个结果。
求和
查询所有员工的月薪总和
select sum(sal) from emp;
求平均值
查询所有员工的月薪平均值
select round(avg(sal), 2)from emp;
最大值.最小值
select max(sal) from emp; select min(sal) from emp;
计数 (统计的是记录数)
select count(empno) from emp;
统计emp 中多少部门
select count(distinct deptno) from emp;
group by 依据 (有分组函数才用)
多次分组 第二次建立在第一次的基础上
查询每个部门不同岗位的平均工资
select deptno, job ,avg(sal)from emp group by deptno, job;

where 不能使用分组函数 别名

having 子语句作用等价于where, 用于分组函数条件筛选
select deptno,avg(sal) from emp group by deptno having avg(sal)> 2500;
查询平均工资在2000-2500的部门编号和其部门平均工资

select deptno,avg(sal) from emp
group by deptno
having avg(sal) between 2000 and 2500

书写:select…from …where … group by …order by …(having )…
执行: from …where … group by …(having)…select…order by …

第四章

多表查询
等值连接 , 两张表使用等号进行连接 采用两列意义相同的列,
查询员工姓名,所在部门名称
select ename , dname from emp , dept where emp.deptno = dept.deptno;
笛卡尔积 (多表连接查询基础)
lianggejihe(emp, dept),记录组合
查询月薪高于1500的员工姓名 和部门名称;
select ename ,dname from emp, dept where emp.deptno = dept.deptno and sal>1500;
多表查询*去重
查询100号员工的姓名所在部门名称 所在部门城市,所在国家,所在大区

select  concat(first_name,last_name),
   		department_name,city,country_name,region_name
   		from employees e ,departments d,locations l, countries c,regions r
   		where e.department_id = d.department_id 
   		and d.location_id = l.location_id
   		and l.country_id = c.country_id 
   		and c.region_id = r.region_id
   		and employee_id = 100;

非等值连接 不使用等号的连接
查询员工的姓名月薪工资级别
select e.ename, e.sal,s.grade from emp e ,salgrade s where e.sal between s.losal and s.hisal;
自身连接 (范围 层次关系查询、公司的组织结构 国家的行政体系)
查询入职日期比其经理入职日期早的员工姓名和经理姓名

select  e0.ename as 员工,e1.ename as 经理 from emp e0, emp e1
            where e0.mgr = e1.empno
			and e0.hiredate < e1.hiredate;

SQL标准语法

多表连接查询 join … on (…)
格式 :表0 lefe/rigth [outer] join 表1 on(连接条件) where 筛选条件
等值连接
查询月薪高于1500的员工姓名 和部门名称 工作地点;

select ename ,dname 
  	from emp join dept on emp.deptno =  dept.deptno  
    where sal>1500 ;
                
select  concat(first_name,last_name), department_name,city,country_name,region_name
	from employees e join departments d on (e.department_id = d.department_id)
	join locations l on (d.location_id = l.location_id)
	join countries c on (l.country_id = c.country_id) 
	join regions r on (c.region_id = r.region_id)
	where employee_id = 100 	.

不等值连接
查询员工的姓名月薪工资级别

select e.ename, e.sal,s.grade 
	 from emp e join salgrade s
	 on (e.sal between s.losal and s.hisal);

自连接 (内连接)
查询员工姓名和其领导的姓名 inner 可省略

select  e0.ename as 员工,e1.ename as 经理 
        from emp e0  left join emp e1 on (e0.mgr = e1.empno);

外链接 分左外连接 右外连接
查询员工姓名 部门名称 包括没有部门的员工

select first_name ,department_name 
     	from employees e left join departments d
     	on(e.department_id = d.department_id);

-------------------------选择方式 要查询信息是左边表多余还是右表多余 --------------------------
全连接 : mysql 不支持全连接
查询部门名称,和其部门经理的姓名 包含没有部门经理的部门

select  department_name as 部门名, e.first_name as 经理名
 		from departments d left join employees e
 		on(d.manager_id = e.employee_id);

自然连接 两张表利用同名列进行连接
要求 列名 列中的数据也是一样的(内容 数据类型)
交叉连接
笛卡尔积
using 子语序
利用同名列连接

select * from employees join departments using (department_id);

-----------------------------------------------------PM 高级查询-------------------------------------------------
(嵌套查询)
注意: 逻辑 格式
单行单列 子查询
查询与scott同部门,同经理位的其他员工信息
select * from emp where deptno = (select deptno from emp where ename = 'scott') and mgr =(select mgr from emp where ename = 'scott') and ename != 'scott';
查询原则: 效率
单行多列子查询 对应列一致
查询与scott同部门,同经理位的其他员工信息

select  * from emp 
	where (deptno,mgr)  = (select deptno , mgr from emp where ename = 'scott')
	and ename != 'scott';

多行子查询 (了解)
多行多列(不用)、多行单列
多行单列
查询与scott 或king 同一个部门的员工信息.

 select * from emp 
 	where  deptno in (select deptno from emp  where ename in('scott','king'));

嵌套查询

四种形式
1.where 下嵌套 (常用、重要)
同上
2.from 下嵌套 (常用、重要 万能查询)
#查询员工姓名 部门名称 员工的工资 和其所在部门的平均工资…

1.select dname,avg(sal) from emp join dept using(deptno) group by deptno ;
2.select ename,t.dname,sal,t.asle from emp
			join (select deptno,dname,avg(sal) as asle
			from emp join dept using(deptno)
			group by deptno ) as t using(deptno);

查询月薪比自己职位平均月薪高的员工信息

select * from emp e join (select job, avg(sal) as savg from emp 
group by job)as t using(job) 
where e.sal >t.savg

3.having 下嵌套(少用)
查询部门平均工资高于’martin’的部门id.

select deptno from emp group by deptno 
  		having (select sal from emp where ename = 'martin') < avg(sal);

4.select 下子查询(基本不用)
查询部门编号,部门名称,部门所在地,部门的人数,部门的平均月薪

select d.deptno, d.dname,d.loc ,(select count(empno) 
    		from emp
    		where emp.deptno = d.deptno group by deptno),(select avg(sal) from emp 
    		where emp.deptno = d.deptno   -- 相关子查询  
            group by deptno) 
            from dept d;

第5章

数据定义语句 DDL
建表
数据库创建 [if not exists] 可省略

	create database if not exists tase1;
	show create database tase1;				-- 显示新建数据库

#删除数据库

drop database tasetest1;  					--  无法找回

数据类型分类
数值(int/bigint、float/double)、字符串(char(长度)、varchar/text)、日期(date、datetime、
timestamp、year)
数据类型(建表使用 插入数据)
建表 create

 create table if not exists test (
		tno int,
        tname varchar(10)
    );     
	select* from test;

更新表 针对表结构(列) column 列 row 行
增加

 alter table test add column year int ; 

删除

 alter table test drop column year; 		 -- 结构修改先备份

修改

alter table test change tname class  varchar(10); 

修改表名

 rename table test to ename_table;

删除表

drop table ename_table;

约束 对表中数据一种规范(限制数据)
约束
主键约束 primary key n能做主键 一定是 没有实际意义的列
唯一、非空的结合

create table test(
            id int primary key,
            name varchar(10)
            );
drop table test;  										-- 多列 联合主键
create table test(
            id int ,
            name varchar(10),
            primary key(id,name)
            );   

删除主键约束

alter table test drop primary key; 

增加主键

alter table test add  primary key(name);

修改主键

alter table test modify id int primary key;

如果修改约束 数据不符合修改后的规范则无法修改

外键约束 保证两张表之间的参考完整性
前提两张表
主表 dept

create table test(
			id int primary key,
			tname varchar(10)
            
        );   
        drop table test;

附表 emp

create table test1(
				id int primary key,
				tname varchar(10),
                classid int,
                test_id int,
                foreign key (test_id) references test(id)
			);

唯一约束 不能重复 空值可以 一个表可以有多个唯一约束

  create table test(
			id int ,
            tname varchar(20),
            unique(tname)
		); 

非空约束
not null非空 对应默认值
只能在列上定义

create table test(
				id int ,
                tname varchar(20) not null
			);

非增约束
默认(不考虑)

笔试

1,数据库语句分类: DDL、DQL、DML、DCL、TPL
2.数据库有哪些约束: 主键、外键、唯一、非空、检查性(条件)
3.什么事事务?事物的特性
4.范式
数据库设计规范 (三范式)
建表原则
目的: 解决数据库中关于数据的存储和优化,尽量少出现重复数据,减少冗余数据
第一范式:要求列中数据具有原子特性不可再分割
第二范式:解决表中的数据不允许出现部分依赖 、必须全部依赖
第三范式:防止数据出现传递依赖问题
传递依赖:多次依赖

数据库操作语句 DML mysql 默认自动提交、其他需要手动提交(提交或者回滚)
插入(新增)
insert into
方式 1 使用列和值进行插入操作

insert into test(tno,tname) values (0,'aa');

方式 2 不使用对应列 直接用值

insert into test values (1,'aa');

修改(更新)

update test set tname = 'ss' where tno = 1; 			-- 一定有条件

删除 一定有条件

delete 	from test where tno= 1;

截断表
alter

truncate table test;

删除
delete DML 删除标的数据 结构没有变 可以根据数据的日志文件实现数据回滚
truncate 是一个DDL 语句 清空表中的数据, 表结构不变,通过日志文件是不能够恢复的
drop DDL 删除表或者数据库,包括表结构、 无法恢复。

复制表

create table emp1 select * from emp; 			 -- 复制全部

create table emp2 like emp; 					 -- 只复制结构

第6章

事务
特性
1.原子性 DML不可再分割
2.一致性 多个DML同时完成或者同时终止
3.隔离性 不同的事务之间不能互相影响
4.持久性 事务被提交后 结果存入数据库**

set autocommit  = false;					--手动提交
set autocommit = true;						--自动提交
start transaction ; 						--开启事务
commit ; 									--提交事务
rollback; 									--事务回滚

视图 、索引 、 序列 (对象)DDL

概念:有结构(列、行),但没有结果的虚拟表
视图的数据来源于表
格式:

create view test as select * from emp;
    drop view test;

应用:多表查询

create view test as select  ename from emp,dept 
     where emp.deptno = dept.deptno; 

不常用数据不要作为视图信息
单纯创建的视图只能查询不可以修改

create or replace view  test as select *from emp1; 

练习
显示以下 employees 和 dapartments 中数据 视图名为enp_dept 去掉同列的信息,没有部门的显示出来

create or replace view emp_dept as 
            select e *,d.department_name ,d.manager_id as dept_mang_id , 
            			d.location_id from employees e
		left join departments d using(department_id);

索引
可以快速查询表中的数据
降低了插入和删除的效率,提高了查询效率
看做类似目录结构
创建方式:
创建表时
在已存在表创建索引
使用alter tabler 语句创建索引
drop index 索引 名 on 表名;
权限
账户管理
grant 赋予用户权限

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值