DBA SQL基础应用

1、distinct
select distinct(cno) from score; 去除score表cno列的重复
2、别名

SELECT a.tname  ,GROUP_CONCAT(d.sname)
FROM teacher AS a
JOIN course AS b
ON a.tno = b.tno
JOIN sc as c
ON b.cno = c.cno
JOIN student AS d
ON c.sno = d.sno
WHERE a.tname='oldguo' AND c.score<60
GROUP BY a.tno;

列别名
select count(distinct(name)) as 个数  from world.city;

3、外链接

SELECT a.name,b.name ,b.surfacearea
FROM city AS a
LEFT JOIN country AS b
ON a.countrycode=b.code
WHERE   a.population<100

4、information_schema.tables

元数据
----> “基表”(无法直接查询和修改的)
----> DDL 进行元数据修改
----> show ,desc(show),information_schema(全局类的统计和查询)

use information_schema
desc tables;
TABLE_SCHEMA     表所在的库
TABLE_NAME       表名
ENGINE           表的存储引擎
TABLE_ROWS       表的行数
AVG_ROW_LENGTH   平均行长度
INDEX_LENGTH     索引的长度

-- information_schema
--- 查询整个数据库中所有的库对应的表名
例如:
world   city
world   country
oldboy  oldguo

SELECT table_schema,table_name
FROM information_schema.tables;

--- 查询world和school库下的所有表名
SELECT table_schema,table_name
FROM information_schema.tables
WHERE table_schema='world'
UNION ALL
SELECT table_schema,table_name
FROM information_schema.tables
WHERE table_schema='school';

--- 查询整个数据库中所有的库对应的表名,每个库显示成一行
SELECT table_schema,GROUP_CONCAT(table_name)
FROM information_schema.tables
GROUP BY  table_schema;

--- 统计一下每个库下的表的个数
SELECT table_schema,COUNT(table_name)
FROM information_schema.tables
GROUP BY  table_schema;

--- 统计一下每个库的真实数据量
每张表数据量=AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH
SELECT
SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS total_mb
FROM information_schema.TABLES

--- information_schema.tables+CONCAT(),拼接命令

--- 使用方法举例
mysql> SELECT CONCAT(USER,"@","'",HOST,"'") FROM mysql.user;
--- 生产需求1
mysqldump -uroot -p123  world city >/tmp/world_city.sql
--- 模仿以上命令,对整个数据库下的1000张表进行单独备份,----
--- 排除sys,performance,information_schema

mysqldump -uroot -p123  world city >/tmp/world_city.sql

SELECT CONCAT("mysqldump -uroot -p123  ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql")
FROM information_schema.tables
WHERE table_schema NOT IN('sys','performance','information_schema')
INTO OUTFILE '/tmp/bak.sh';
修改配置文件:
vim /etc/my.cnf
secure-file-priv=/tmp
/etc/init.d/mysqld restart
-----
--- 例子:模仿以下语句,批量实现world下所有表的操作语句生成
alter table world.city discard tablespace;----
select concat("alter table ",table_schema,".",table_name,"discard tablespace;")
from information_schema.tables
where table_schema='world'
into outfile '/tmp/discard.sql';

5. show命令

show databases;                             查看所有数据库名
show tables;                                查看当前库下的表名
show tables from world;                     查看world数据库下的表名
show create database                        查看建库语句
show create table                           查看建表语句
show grants for root@'localhost'            查看用户权限信息
show charset                                查看所有的字符集
show collation                              查看校对规则
show full processlist                       查看数据库连接情况
show status                                 查看数据库的整体状态
show status like '%lock%'                   模糊查看数据库的整体状态
show variables                              查看数据库所有变量情况
show variables  like '%innodb%'             查看数据库所有变量情况
show engines                                查看所有支持存储引擎
show engine innodb status                   查看所有innodb存储引擎状态情况
show binary logs                            查看二进制日志情况
show binlog events in                       查看二进制日志事件
show relaylog events in                     查看relay日志事件
show slave status                           查看从库状态
show master status                          查看数据库binlog位置信息
show index from                             查看表的索引情况

6、索引

6.0 学习环境准备
mysqlslap --defaults-file=/etc/my.cnf \
--concurrency=100 --iterations=1 --create-schema='oldboy' \
--query="select * from oldboy.t100w where k2='EF12'" engine=innodb \
--number-of-queries=2000 -uroot -p123 -verbose
6.1 索引作用
提供了类似于书中目录的作用,目的是为了优化查询
6.2.索引的种类(算法)
B树索引
Hash索引
R树
Full text
GIS
6.3、B树算法和普及
B-tree
B+tree
B*tree
6.4、在功能上分类
6.4.1、辅助索引(S)怎么构建B树结构的
1)、辅助索引是基于表的列进行生成的
2)、取出索引列的所有值(取出所有键值)
3)、进行所有键值的排序
4)、将所有的键值按顺序落到BTree索引的叶子节点上
5)、进而生成枝节点和根节点
6)、叶子节点除了存储键值之外,还存储了相邻叶子节点的指针,另外还会保存原表数据的指针。
6.4.2、聚集索引(C)怎么构建B树结构的
1)、建表时有主键列
2)、表中进行数据存储,会按照ID列的顺序,有序的存储一行一行的数据到数据页上(这个动作叫做聚集索引组织表)。
3)、表中的数据页被作为聚集索引的叶子节点。
4)、把叶子节点的主键值生成上层枝节点和根节点。、
6.4.3、聚集索引和辅助索引构区别总结
聚集索引只能有一个,非空唯一,一般是主键
辅助索引,可以有多个,是配合聚集索引使用的
聚集索引叶子节点,就是磁盘的数据存储的数据页
mysql是根据聚集索引,组织存储数据,数据存储时就是按照聚集索引的顺序进行存储数据
辅助索引,只会提取索引键值,进行自动排序生成B树结构
6.5、辅助索引细分
单列的辅助索引
联合多列辅助索引(覆盖索引)
唯一索引
6.6、关于索引树的高度受什么影响?
1)数据行多,分表
2)索引列的字符长度,前缀索引
3)char varchar ,表设计
4)enum 优化索引高度,能用则用

一些题

-------------------------------
--- 查询整个数据库中所有的库对应的表名,每个库显示成一行
SELECT table_schema,GROUP_CONCAT(table_name)
FROM information_schema.tables
GROUP BY  table_schema;
-------------------------------
统计一下每个库下的表的个数
select table_schema,count(table_name)
from information_schema.tables
group by table_schema;
-------------------------------
--- 统计一下每个库的真实数据量
select table_schema,count(table_name),
sum(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)
from information_schema.tables
group by table_schema;
-------------------------------
每张表数据量
SELECT
table_schema,
COUNT( table_name ),
SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS ToTAL_MB
FROM information_schema.TABLES
GROUP BY table_schema;
-------------------------------
information_schema.tables拼接命令
例子:select concat(user,"@",host) from mysql.user;
-------------------------------
备份mysql数据到tmp下,""引起来的是不变的,除了密码。
修改配置文件。
vim /etc/my.cnf
secure-file-priv=/tmp
重启下
--
SELECT CONCAT("mysqldump -uroot -p123  ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql")
FROM information_schema.tables
WHERE table_schema NOT IN('sys','performance','information_schema')
INTO OUTFILE '/tmp/bak.sh';
------------------------------------------------------------------------

mysql> select * from t100w limit 10;
mysql> select * from t100w limit 10;
mysqlslap --defaults-file=/etc/my.cnf
--concurrency=100 --iterations=1 --create-schema='oldboy'
--query="select * from oldboy.t100w where k2='QRLM'" engine=innodb
--number-of-queries=2000 -uroot -p1 -verbose


你可能感兴趣的:(DBA SQL基础应用)