数据库

95 阅读2分钟

数据库操作

1、查 show database;
2、创 create database 表名;
3、用 use 表名;
4、删 drop database 表名;

表操作

1、创建表 create table 表名( 字段1 ,字段2 ,字段32、复制表 create table 表名 select * from 原表名 
3、临时表 create temporary table 表名(字段1 , 字段2 , 字段34、查看所有表 show tables
5、查看表结构 ① describe 表名    ② show columns in 表名
6、删表 drop table if exists 表名           注:if exists是存在时候删,不存在拉倒
7、重命名 rename table 原名 to 新名

表修改

改结构 
1alter table 表名 add 新字段
2alter table 表名 drop 原字段
3alter table 表名 change 更改字段 更改后的字段信息

插入数据

1insert into 表名(字段1 ,字段2 ,字段3 等) values (字段1信息 , 字段2信息 ,字段3信息),可以同时加几条
2、插入查询的数据 insert into1(字段1 , 字段2 ) select 字段1 , 字段2 from2

更新数据

update1 set 字段*=*** where 条件

删除数据

delete from1 where 条件

条件控制

1where 
2having
3、符号 : =<><>inbetween 左范围 and 右范围, notandor, linke(), is null              注:linke %任意,_一个字符

常见函数

1、字符串连接 concat
    select concat(字段1,'=>',字段2) from 表12、数字函数 avg 、 sum 、 max 、 min 、 count
3、文本处理 trim 、 locate 、 upper 、 lower 、 substring
4、时间函数 date() 、 curtime() 、 day() 、 year() 、 now() 

分组查询

1group byselect * from1 group by 字段1        后面还可以跟上having对分组后的数据进行条件判断,order by desc/asc 升降序排序

数据库基础操作优先级

image.png

索引小概括

1、体系结构:连接层、服务层、引擎层、存储层
2、存储引擎简介:①show engines ②create table xx(...)engine=innodb
3、存储引擎特点:    INNODB和MyISAM:事务、外键、行级锁
4、存储引擎应用:
    ①INNODB:存储业务系统中对事务、数据完整性要求较高的核心数据
    ②MyISAM:存储业务系统中的非核心事务

变量定义

1、局部变量 👉 declare
2、用户变量 👉 select @变量名
3、会话变量 👉 session 当前会话有效,仅限于本次连接
4、全局变量 👉 global 在整个生命周期有效

SQL性能分析

1、查看访问频次:show global status like 'Com_______'
2、慢查询日志:先配置日查询日志的开启和限时时间,记得重启;在 /var/lib/mysql/localhost-slow.log 中查看超时操作的日志
3、profile详情:开启profilie后,
    ①用 show profiles 可以查看当前会话中的所有sql操作用时;
    ②用 show profile for query id(上面查出来的id)  可以看本次sql在各处用时
    ③用 show profile cpu for query id(上面查出来的id) 可以查看该sql语句cpu耗时