MySQL

一 用户

1. 用户登录

使用终端登录

01登录.png

查看数据库

02查看数据库.png

注:
执行命令时结尾加分号(;)
退出登录命令Ctrl+c / exit


2.创建用户

create user '用户名'@'主机名' identified by '密码'
创建用户.png

使用新用户登录

新用户登录.png
新用户数据库.png

3.分配权限-Grant

grant 权限 on 数据库/表 to '用户‘@'主机名' [identified by '密码'] 
// 权限:可以是一个列表,不同的权限使用逗号分割;所有权限使用all privileges
// 数据库/表:指定的数据库/表
// [identified by '密码']:可选参数,在为新用户分配权限时设置密码
// with max_queries_pre_hour:每小时最大查询数量
// with max_connections_pre_hour:每小时最大连接数量
// with max_updates_pre_hour:每小时最大更新数量
// with max_user_connections:最大用户连接数量
分配权限.png
flush privileges; 
// 刷新MySQL的系统权限相关表

4.显示用户列表

select user from mysql.user;
显示用户列表.png

5.显示用户权限

select 用户, 主机, 权限 form 数据库.表
显示用户权限.png
show grants for '用户'@'主机名'
显示用户权限2.png

6.吊销用户权限

revoke 更新, 删除 on 数据库.表 from '用户名'@'主机名'
吊销用户权限.png

7. 重设密码与删除用户

set password for '用户名'@'主机名' = password('******');
// 重设密码

drop user '用户名'@'主机名';
// 删除用户
删除用户.png

二 数据库

1. 创建、使用、删除数据库

create database 数据库
// 创建数据库

drop database 数据库
// 删除数据库

show databases
// 显示全部数据库

2. 创建数据表

use 数据库名
// 选择一个特定的数据库

create table 数据表名(每栏数据名及属性)
// 创建数据表名

show tables;
// 显示全部数据表

describe 数据表名
// 显示指定数据表内容
创建数据表.png

3. 添加数据栏

alter table film add id INT(10) first;
// 在film数据表中添加id栏到第一栏

alter table film add film_content TEXT after film_name;
// 添加film_content栏在film_name后面
添加数据栏.png

设置主键

alter table film add PRIMARY KEY (id);
设置主键.png

4. 修改或删除数据栏和数据表

alter table film change id film_id INT(10);
// 更改id栏名称为film_id

alter table film rename to movie;
// 更改数据表名称

alter table movie drop film_content;
// 删除film_content数据栏

drop table movie;
// 删除数据表

5. 重新创建数据库与数据表

create database 数据表 charset=utf8;
// 设置数据库默认字符集

create table people() default charset=utf8;
// 设置数据表默认字符集

people_id INT(10) unsigned not null auto_increment
// unsigned:整型
// not null:不可为null
// auto_increment:自增
// primary key(people_id):设置主键
重新创建数据表.png

三 查询

1. 插入数据-insert

insert into 数据表 values(value1, value2, ...);
// 添加所有数据栏的值

insert into 数据表(attr1, attr3) values(value1, value3);
// 添加指定数据栏的值
插入数据.png

2. 选择数据-select

select * from 数据表
// 显示所有数据栏的值

select column1, column2... from 数据表
// 显示指定数据栏的值

select * from people where people_location = '美国';
// 显示通过条件的数据值

select * from people order by people_birth desc;
// 显示排序后的数据值

3. 更新与删除数据-update&delete

update 表名称 set 字段='值' where 字段='值'
// 更新数据

delete from 表名称 where 字段='值'
更新与删除数据.png

4. 限制结果的数量与偏移-limit&offset

select * from people where people_location = '美国' limit 3;
// 设置最多显示3行数据

select * from people limit 3 offset 1; 
// 设置最多显示3行数据,从开始处偏移一行

select * from people limit 1, 3
// 同上

5. 操作符

select * from people where people_birth > '1960-01-01';
// 查找出生年月在1960之后的数据值

select * from people where people_location in ('美国', '英国');
// 查找出生地在某个集合中的数据值,用操作符in;不在某个集合中用操作符not in

select * from people where people_name like ('李%');
// 查找匹配模式后的数据值

四 关系

1. 关联-join

// 将用户和评论两个表组织在一起
select user_name, review_content from user, review where user.user_id = review.user_id;
// user, review == cross join 交叉关联

select user_name, review_content from user inner join review on user.user_id = review.user_id;
// inner join 内部关联

select user_name, review_content from user inner join review on user.user_id = review.user_id where user.user_id = 1;
// 设置条件user.user_id = 1;

2. 左关联

select user_name, review_content from user left join review on user.user_id = review.user_id;
// left join 左关联:把左侧user_name的所有信息都显示出来

3. 统计、平均、分组

select count(review_id) from review;
// 从review数据表里面统计review_id的总数

select film_id, count(review_id) from review group by film_id;
// 以film_id为基础,统计每一部电影的review总数

select film_id, avg(review_rate) from review group by film_id;
// 以film_id为基础,计算每一部电影的review_rate平均数

select review.film_id, film.film_name, avg(review_rate) from review, film where review.film_id = film.film_id group by review.film_id;
// 以film_id为基础,以review.film_id=film.film_id为条件,列出对应的电影的film_id和电影的film_name,并计算出每一部电影的review_rate的平均数

4. 三个表的关联

交叉关联读取三个数据表,and多条件

select film_name, people_name, job from film, people, film_people 
// 从三个表中查找并显示film_name,people_name,job之间的关系
where 
film_people.film_id = film.film_id 
// 条件一:表film_people和表film的film_id相同
and 
film_people.people_id = people.people_id;
// 条件二:表film_people和表people的people_id相同

交叉关联读取三个数据表,and多条件,like匹配

select film_name, people_name, job from film, people, film_people 
// 从三个表中查找并显示film_name,people_name,job之间的关系
where 
film_people.film_id = film.film_id 
// 条件一:表film_people和表film的film_id相同
and 
film_people.people_id = people.people_id
// 条件二:表film_people和表people的people_id相同
and
film_name = '无间行者'
// 条件三:只显示film_name='无间行者'的相关信息
and
people_name like '马丁%';
// 条件四:将搜索结果用“马丁”来匹配

交叉关联读取三个数据表,总票房order排序、只显示job='导演'

select sum(film_box) as total_box, people_name from film, people, 
// 将计算后的票房存到别名total_box
film_people 
where
film_people.film_id = film.film_id
and
film_people.people_id = people.people_id
and
job = '导演'
group by people_name
// 以people为基础
order by total_box desc;
// 以票房我基础降序排序
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容