mysql


# 数据库字段常用类型
# 整数类型 : tinyint smallint mediumint int bigint
# 字符类型 : char varchar
# 日期类型: date datetime timestamp time year
# 备注类型: text longtext mediumtext tinytext
# 浮点类型: float double decimal

# mysql 数据库操作
# 进入数据库
mysql -h host -P port -u username -p password
#或者
mysql -u username -p password -h host -P port
# 查看所有数据库
show databases;
# 查看当前数据库
select database();
# 创建数据库
create database databaseName;
# 删除数据库
drop database databaseName;
# 使用数据库
use databaseName;
# 查看所有表
show tables;
# 查看表结构
desc tableName;
# 设置中文编码
set names utf8;
# 查看表数据
select * from tableName;


# 查看数据 条件查询
# 常用运算符: > < = >= <= != <>
select * from tableName where columnName > value;
# is null : 没有值,is not null : 有值, is null 和 is not null 只能用于判断是否有值,不能用于判断是否相等
select * from tableName where columnName is null;
# between and: 在某个范围内
select * from tableName where columnName between value1 and value2;
# no between : 不在某个范围内
select * from tableName where columnName not between value1 and value2;
# in : 在某个范围内
select * from tableName where columnName in (value1, value2, ...);
# like : 模糊查询
select * from tableName where columnName like '%value%';
# and : 并且
select * from tableName where columnName1 = value1 and columnName2 = value2;
# or : 或者
select * from tableName where columnName1 = value1 or columnName2 = value2;
# order by : 排序
select * from tableName order by columnName desc; # 降序 desc 升序 asc

# msyql 分组函数
# count : 统计数量
select count(*) from tableName;
# max : 最大值
select max(columnName) from tableName;
# min : 最小值
select min(columnName) from tableName;
# sum : 求和
select sum(columnName) from tableName;
# avg : 平均值
select avg(columnName) from tableName;


# mysql 别名:as 用于给字段或者表起别名
# as
select columnName as aliasName from tableName;


# mysql 表与表之间的关系
# 一对一 : 一个表的一条数据对应另一个表的一条数据, 需要借助外键
# 例如: 一个学生对应一个身份证号,一个身份证号对应一个学生


# 一对多 : 一个表的一条数据对应另一个表的多条数据, 需要借助外键
# 例如: 一个班级对应多个学生,一个学生对应一个班级


# 多对多 : 一个表的多条数据对应另一个表的多条数据, 需要借助第三张表
# 例如: 一个学生对应多个课程,一个课程对应多个学生
# lesson 表  student 表  lesson_student 表(中间表)
# 普通查询
select * from lesson where id in (select lesson_id from lesson_student where student_id = 1);
# 笛卡尔积关联查询
select * from lesson, lesson_student where lesson.id = lesson_student.lesson_id and lesson_student.student_id = 1;
# 连表查询
select * from lesson left join lesson_student on lesson.id = lesson_student.lesson_id where lesson_student.student_id = 1;
# 内连接查询
select * from lesson inner join lesson_student on lesson.id = lesson_student.lesson_id where lesson_student.student_id = 1;


# mysql 笛卡尔积连接, 内连接, 左外连接, 右外连接
# 注意: 在项目中能不用连接查询尽量就不用连接查询,连接查询会影响性能, 笛卡尔积查询速度最慢,内连接速度最快(会用)

# 笛卡尔积连接
select * from tableName1, tableName2 where tableName1.columnName = tableName2.columnName;

# 内连接
select * from tableName1 inner join tableName2 on tableName1.columnName = tableName2.columnName;

# 左外连接
select * from tableName1 left join tableName2 on tableName1.columnName = tableName2.columnName;

# 右外连接
select * from tableName1 right join tableName2 on tableName1.columnName = tableName2.columnName;

## mysql 索引
# 索引 对表中的一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息
# 简单来说: 索引就是提高查询数据速度,数据量越大,索引的作用越明显
# mysql 索引类型: 普通索引, 唯一索引, 主键索引, 全文索引

# 普通索引
create index indexName on tableName(columnName);
create index index_name on class(name); # 创建索引

# 查看索引
show index from tableName;
show index from class;
show index from class\G; # \G 以竖行的形式显示

# 删除索引
drop index indexName on tableName;
drop index index_name on class;

# 创建唯一索引(主键是一种唯一索引)
create unique index indexName on tableName(columnName);
create unique index index_name on class(name);

# 另外一种创建和删除方式
alter table tableName add index indexName(columnName);
alert table class add unique index index_name(name);
alter table tableName drop index indexName;

# 创建主键索引
alter table tableName add primary key(columnName);

# 创建全文索引
alter table tableName add fulltext(columnName);


# mysql 事务
# 事务: 一组sql语句的集合,这组sql语句要么全部执行,要么全部不执行
# 例如: 张三给李四转账100元,张三的账户减少100元,李四的账户增加100元,这两个操作要么同时成功,要么同时失败
begin; # 开启事务
update user1 set money = money - 100 where id = 1;
update user2 set money = money + 100 where id = 2;
commit; # 提交事务
rollback; # 回滚事务


#mysql 锁
# 锁: 用于控制多个用户对数据的并发访问,锁可以在事务隔离级别上起作用
# 锁的分类: 共享锁(读锁): 允许一个事务去读一行,阻止其他事务获得相同数据集的排他锁
# 添加读锁: 可以并发读,但是不能并发写,读锁期间,没有释放前不能进行写操作
# 使用场景: 读取结果集的最新结果,同时防止其他事务对结果集进行修改,确保没有人对结果集进行修改
lock table tableName read;

unlock tables;


# 排他锁(写锁): 允许获得排他锁的事务更新数据,阻止其他事务取得相同数据集的共享读锁和排他写锁
# 只有锁表用户才能对表进行读写操作,其他用户不能对表进行读写操作
# 使用场景:并发对商品库存进行修改,防止超卖
lock table tableName write;

unlock tables;


# 添加数据
insert into tableName(columnName1, columnName2, ...) values(value1, value2, ...);
# 更新表数据
update tableName set columnName = value where columnName = value;
# 删除表数据
delete from tableName where columnName = value;
# 删除表
drop table tableName;
# 创建表
create table tableName(
    columnName1 dataType1,
    columnName2 dataType2,
    ...
);

# 指定排序
select * from tableName order by columnName desc; # 降序 desc 升序 asc
# 统计数量
select count(*) from tableName;
# limit 限制查询数量
select * from tableName limit 10; # 查询前10条数据
# 分页查询
select * from tableName limit 10, 10; # 查询第11-20条数据
# 删除表
drop table tableName;

# 查看mysql版本
select version();
# 查看mysql状态
show status;
# 查看mysql进程
show processlist;