下载mysql:
下载地址: MySQL::Download MySQL Community Server
download后解压到自己想存放的位置
把bin目录添加到环境变量
管理员模式打开cmd,在bin目录下操作
mysqld --initialize-insecure --user=mysql 生成data目录
mysqld install 安装服务
mysqld install时报错
The service already exists! The current server installed:
以前mysql没卸载干净 sc delete mysql
net start MySQL 启动服务
管理员登陆mysql
mysql -h localhost -P 3306 -u root -p;
- -h:mysql server host,不写时默认是localhost。
- -P:mysql server port,不写时默认是3306。
- -u:user name,-u后可以加空格也可以不加。
- -p:password,密码中可能包含空格,所以要加引号。高版本的mysql不允许在命令行中直接输入密码,此时只输入-p后面不要写密码即可。 创建账号
未设置密码,直接回车
修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
创建用户
create user 'tester' identified by '123456';
创建database
create database test;
将database的权限交予其他用户
grant all on test.* to tester;
退出后登入tester
增删查改
使用database
use test; --使用表
show databases; --展示库
show tables; --展示表
创建表
create table if not exists student(
id int not null auto_increment comment '主键自增id',
name char(10) not null comment '姓名',
province char(6) not null comment '省',
city char(10) not null comment '城市',
addr varchar(100) default '' comment '地址',
score float not null default 0 comment '考试成绩',
enrollment date not null comment '入学时间',
primary key (id), unique key idx_name (name),
key idx_location (province,city)
)default charset=utf8 comment '学员基本信息';
- if not exists 如果不存在才创建表
- not null:不能为空,即必须有值
- auto_increment:自增
- char(10) :固定长度字符串,即len=10
- varchar(10):可变长字符串
- comment:备注
- 索引用处:加快查找
- primary key:主键索引
- unique key idx_name:唯一的普通索引,idx_name为取的名字
- key idx_location:联合索引,由于省和市通常一起指定。联合索引的前缀也是索引,即只指定province也能索引,但只指定city却不能
展示表
desc student; --按表格形式
show create table student (\G); --按输入格式,\G:相当于按格式化
新增记录
insert into student (name,province,city,enrollment) values
('张三','北京','北京','2021-03-05'),
('李四','河南','郑州','2021-04-25'),
('小丽','四川','成都','2021-03-10');
查询
select id,name from student where id>0;
select province,avg(score) as avg_score from student --把avg(score)命名为avg_score
where score>0
group by province having avg_score>50 --按省分组,having相当于where,不过having后跟表里没有的字段,如avg(score)
order by avg_score desc; --按avg_score排序,desc:降序
limit
- limit N : 返回 N 条记录
- offset M : 跳过 M 条记录, 默认 M=0, 单独使用似乎不起作用
- limit N,M : 相当于 limit M offset N , 从第 N 条记录开始, 返回 M 条记录
select id,name,province from student limit 100,10
修改
update student set score=score+10,addr='海淀' where province='北京';
update student set
score=case province
when '北京' then score+10
when '四川' then score+5
else score+7
end,
addr=case province
when '北京' then '东城区'
when '四川' then '幸福里'
else '朝阳区'
end
where id>0;
删除
delete from student where city= '郑州';
delete from student; --删除表里的所有行
drop table student; --删除表
索引:基于B+树
- B+树可为m叉树,每个节点最多有m个数据,最少有m/2个数据,根节点除外。
- 将叶节点串联可顺序遍历所有数据。
- 每个节点设计成内存页的整倍数。(若内存页为4K,则当m=1000时,每一节点如90,96,99这个叶节点的数据为4B)MySQL的m=1200,树的前两层放在内存中。
- mysql索引使用B+树,为什么不适用hash table?,索引用于范围型,如where id > 1,hash table查询单个索引时间复杂度为O(n),范围型不如B+树快
- 主键默认会加索引。按主键构建的B+树里包含该键的所有数据,如id=90后跟随其所有信息。而普通索引的B+树里只存储了主键,还需要再查一次主键对应的B+树(回表)。
- 联合索引的前缀同样具有索引效果。
- sql语句前加explain可以查看索引使用情况。(会展现出可能使用的索引和实际使用的索引)
- 可在where前添加force index(索引)强行指定索引