MySQL自学知识点笔记

终端基本:

进入目录 cd 目录/

显示当前目录下文件 ls

超级管理员权限 sudo -s

MySQL安装路径 whereis mysql

MySQL文件路径 which mysql

——————————————————————————————————————————

数据库字段类型:

int 数字

decimal(5,2) 释义:5 => 一共几位 ;2 => 小数后几位

1.2

1000.21

text 文本较大;例如:添加一个商品描述,预知不到包含多少个字符

char(8)‘abcd ’包含8个字符,固定(不够会填充空格)

varchar(8) ‘abcd’包含8个字符,可适应

gb-2312 中国大陆简体中文编码

utf-8 国际通用字符编码

datetime 日期时间 date日期 time时间

布尔 bit(8) 8个二进制位 0,1 male female 基于两种状态的数据 0,1,10,11,100,101,110

——————————————————————————————————————————

约束:

primary key 主键 表中只能有一个

not null 非空

unique 唯一 表中可建立多个

default 默认

foreign key 外键

——————————————————————————————————————————

逻辑删除:

isDelete 类型bit 设置默认(0,1)

DeleteFlag

——————————————————————————————————————————

MySQL基本:

连接MySQL mysql -uroot -p

获取MySQL帮助 mysql —help

退出MySQL登录 quit&exit

连接远程服务器 mysql -hip地址 -uroot -p

配置环境变量

1、cd /usr/local/mysql

2、sudo vim .bash_profile

3、i 编辑

4、插入三行

‘#mysql

PATH=$PATH:/usr/local/mysql/bin

exprot’

5、:wq 保存并退出

6、运行环境变量 source .bash_profile

——————————————————————————————————————————

数据库操作:

创建数据库 create database 数据库名 charset=utf8;

删除数据库 drop database 数据库名;

切换数据库 use 数据库名;

查看当前选择的数据库 select database();

查看数据库 show databases;

——————————————————————————————————————————

表操作:

显示数据库表 show tables;

查看表结构 desc 表名;

删除表 drop table 表名;

创建表
auto_increment 标识自动增长
create table 表名(列及类型);

如:
create table students(
id int auto_increment primary key,
sname varchar(10) not null
);

修改表
alter table 表名 add/change/drop 列名 类型;

如:
alter table students add birthday datetime;

查看表的创建语句 show create table ‘表名’;

更改表名称 rename table 原表名 to 新表名;

——————————————————————————————————————————

数据操作:

全列插入

insert into 表名 values(…);

如:
insert into students values(0,’叶可爱’,1,’1992-3-19’,0,福建);

缺省插入
insert into 表名(列1,…) values(值1,…);

如:
insert into students(name,birthday,hometown) values('林仙女','1969-7-7','福建');

同时插入多条数据
insert into 表名 values(),()…; 或 insert into 表名(列1,…) values(值1,…)…;

如:
insert into students(name,hometown) values('蔡表妹','福建'),('蔡表姐','福建’),(‘周黑 皮','福建');

修改数据
update 表名 set 列1=值1,… where 条件

如:
update students set birthday='1990-3-8' where id=5;

删除数据
delete from 表名 where 条件;

如:
delete from students where id=5;

逻辑删除
update students set isDelete=1 where 条件;

https://blog.csdn.net/potato512/article/details/80744313

——————————————————————————————————————————

数据备份:

进入超级管理员 sudo -s

进入MySQL库目录 cd /usr/local/mysql

运行mysqldump命令 mysqldump -u root -p 数据库名 > ~/Desktop/备份文件.sql;

数据恢复:

进入数据库创建新空数据库

运行 mysql -uroot -p 新数据库名 < ~/Desktop/备份文件.sql;

——————————————————————————————————————————

查询表操作:

查询表 select * from 表名;

条件查询表 select * from 表名 where 条件;

查询部分表
select 字段,字段,… from students;

如:
select id,name from students;

消除重复行 select distinct 字段 from 表名;

比较运算符 select * from 表名 where 字段?条件

等于=
大于>
大于等于>=
小于<
小于等于<=
不等于<>或!=

逻辑运算符 select * from 表名 where 字段?条件 and 字段=条件;

and
or
not
模糊查询 select * from 表名 where 字段?条件 like’条件’;
like
%表示任意多个任意字符
_表示一个任意字符

范围查询
in 非连续范围
select * from 表名 where 字段 in(条件,…);

如:
select * from students where id in(1,3,8);

between … and … 连续的范围内
select * from 表名 where 字段 between 条件 and 条件;

如:
select * from students where id between 3 and 8 and gender=1;

空判断 is null 判空

如:
select * from students where birthday is null;

is not null 判非空

如:
select * from students where birthday is not null and gender=0;

优先级
小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用

——————————————————————————————————————————

聚合:

count(*) 表示计算总行数

如:
select count() from students;
select count(
) from students where isDelete=0;

max(列) 表示此列的最大值

如:
select max(id) from students where gender=0;
min(列) 表示此列的最小值

如:
select min(id) from students where isDelete=0;
select * from students where id=(select min(id) from students where isDelete=0);

sum(列) 表示求此列的和

如:
select sum(id) from students where gender=1;

avg(列) 表示求此列的平均值

如:
select avg(id) from students where isDelete=0 and gender=0;

——————————————————————————————————————————

分组 group by

select 列1,列2,聚合… from 表名 group by 列1,列2,列3…

如:
select count() from students group by gender;
select gender,count(
) from students group by gender;

分组后筛选 having

如:
select gender,count() from students group by gender having gender=0;
select gender,count(
) as rs from students group by gender having rs>2;

排序 order by

select * from 表名 order by 列1 asc|desc, 列2 asc|desc,…

如:
select * from students where isDelete=0 and gender=1 order by id desc;
select * from subjects where isDelete=0 order by id asc;

分页 limit start,count

如:
select * from students limit 1,3;

已知:每页显示m条数据,当前显示第n页
求总页数:此段逻辑后面会在python中实现
查询总条数p1
使用p1除以m得到p2
如果整除则p2为总页数
如果不整除则p2+1为总页数
求第n页的数据

如:
select * from students where isDelete=0 limit (n-1)*m,m;

——————————————————————————————————————————

完整的select语句

select distinct *
from 表名
where …
group by …
having …
order by …
limit star,count;

执行顺序为:

from 表名
where …
group by …
select distinct *
having …
order by …
limit star,count

——————————————————————————————————————————

建立关系表:

create table scores(
-> id int auto_increment primary key not null,
-> score decimal(4,1),
-> stuid int,
-> subid int,
-> foreign key(stuid) references students(id),
-> foreign key(subid) references subjects(id));

如:
插入数据 insert into scores values(0,100,1,1);

外键约束的级联操作:

restrict(限制):默认值,抛异常
cascade(级联):如果主表的记录删掉,则从表中相关联的记录都将被删除
set null:将外键设置为空
no action:什么都不做

——————————————————————————————————————————

连接查询:

select students.name,subjects.title,scores.score from scores inner join students on scores.stuid=students.id inner join subjects on scores.subid=subjects.id;

select students.name,subjects.title,scores.score from students inner join scores on scores.stuid=students.id inner join subjects on scores.subid=subjects.id;

select * from scores right join students on students.id=scores.stuid;

select * from scores left join students on students.id=scores.stuid;

select name,avg(score) from scores inner join students on scores.stuid=students.id group by stuid;

select name,avg(score) as avg1 from scores inner join students on scores.stuid=students.id group by stuid order by avg1;

select name,sum(score) from students inner join scores on students.id=scores.stuid where gender=0 group by name;

select title,avg(score) from subjects inner join scores on subjects.id=scores.subid group by subjects.title

select title,max(score),avg(score) from subjects inner join scores on subjects.id=scores.subid where subjects.isDelete=0 group by subjects.title;

select distinct 列
from 表 inner | left | right 表2 on 表1与表2的关系
where …
group by … having …
order by … asc | desc
limit start,count

——————————————————————————————————————————

自关联(设计新表):

创建新表
create table areas(id int primary key auto_increment not null, title varchar(20), pid int,foreign key(pid) references areas(id));

导入sql文件
source 表名 地址;

查询
select count(*) from areas where pid is null;

select city.* from areas as city inner join areas as province on city.pid=province.id where province.title='山西省';

select dis.* from areas as dis inner join areas as city on city.id=dis.pid where city.title='广州市';

select sheng.id as sid,sheng.title as stitle, shi.id as shiid,shi.title as shititle from areas as sheng inner join areas as shi on sheng.id=shi.pid where sheng.pid is null and sheng.title='山西省';

——————————————————————————————————————————

视图:

创建
create view v_1 as
select stu.*,sco.score,sub.title from scores as sco
inner join students as stu on sco.stuid=stu.id
inner join subjects as sub on sco.subid=sub.id;

修改
alter view v_1 as
select stu.*,sco.score,sub.title from scores as sco
inner join students as stu on sco.stuid=stu.id
inner join subjects as sub on sco.subid=sub.id
where stu.isDelete=0 and sub.isDelete=0;

——————————————————————————————————————————

事务(ACID):当数据被更改时,包括insert、update、delete

原子性(Atomicity):事务中的全部操作在数据库中时不可分割的,要么全部完成,要么均不执行

一致性(Consistency):几个并行执行的事务,其执行结果必须与按某一顺序串行执行的结果相一致

隔离性(Isolation):事务的执行不受其他事务的干扰,事务执行的中间结果对其他事务必须是透明的

持久性(Durability):对于任意已提交事务,系统必须保证该事务对数据库的改变不被丢失,即使数据库出现故障

表的类型必须是innodb或bdb类型,才可以使用事务

举例:

show create table students;

修改表类型 alter table ‘表名’ engine=innodb;

事务语句:

开启begin;

提交commit;

回滚rollback;

——————————————————————————————————————————

索引:

能用bit代替的别用int,能用int代替的别用decimal;

尽量避免NULL:应该指定列为NOT NULL,除非你想储存NULL;

查看索引 show index from students;

创建索引 create index indexName on 表名(username(length));

删除索引 drop index [indexName] on 表名;

示例

开启运行时间监测 set profiling=1;

执行查询语句 select * from areas where title=‘北京市’;

查看执行的时间 show profiles;

创建索引 create index titleIndex on areas(title(20));

你可能感兴趣的:(MySQL自学知识点笔记)