视图是一个对应关系,相当于给某个查询结果取个别名,本身并不存储数据。
CREATE VIEW 视图名称 AS 查询SQL语句
DROP VIEW 视图名称
ALTER VIEW 已存在视图名称 AS SQL语句
使用视图时,将其当作表进行操作即可,由于视图是虚拟表,所以无法使用其对真实表进行创建、更新和删除操作,仅能做查询用。
select * from 视图名称
information_schema库是MySQL自带的,它提供了访问数据库元数据的方式。是个视图库,虚拟库。
在数据库中,数据表是由元数据和数据行组成。类似于文件系统中的一个文件,文件有大小,权限等元数据信息,也有内容信息。数据表也类似,有表名,数据类型等元数据信息,也有数据行内容信息。
元数据存储在基表中,我们没办法直接访问。MySQL提供DDL、DCL来修改元数据,提供information_schema和SHOW语句来查询元数据。
3306 [(none)]>DESC information_schema.TABLES;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| TABLE_TYPE | varchar(64) | NO | | | |
| ENGINE | varchar(64) | YES | | NULL | |
| VERSION | bigint(21) unsigned | YES | | NULL | |
| ROW_FORMAT | varchar(10) | YES | | NULL | |
| TABLE_ROWS | bigint(21) unsigned | YES | | NULL | |
| AVG_ROW_LENGTH | bigint(21) unsigned | YES | | NULL | |
| DATA_LENGTH | bigint(21) unsigned | YES | | NULL | |
| MAX_DATA_LENGTH | bigint(21) unsigned | YES | | NULL | |
| INDEX_LENGTH | bigint(21) unsigned | YES | | NULL | |
| DATA_FREE | bigint(21) unsigned | YES | | NULL | |
| AUTO_INCREMENT | bigint(21) unsigned | YES | | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| CHECK_TIME | datetime | YES | | NULL | |
| TABLE_COLLATION | varchar(32) | YES | | NULL | |
| CHECKSUM | bigint(21) unsigned | YES | | NULL | |
| CREATE_OPTIONS | varchar(255) | YES | | NULL | |
| TABLE_COMMENT | varchar(2048) | NO | | | |
+-----------------+---------------------+------+-----+---------+-------+
21 rows in set (0.00 sec)
常用的列:
TABLE_SCHEMA ---->库名
TABLE_NAME ---->表名
ENGINE ---->存储引擎
TABLE_ROWS ---->表的行数
AVG_ROW_LENGTH ---->表中平均行(字节)
INDEX_LENGTH ---->索引占用空间大小(字节)
SELECT
TABLE_SCHEMA,
TABLE_NAME,
ENGINE,
TABLE_ROWS,
AVG_ROW_LENGTH,
INDEX_LENGTH
FROM
information_schema.TABLES;
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,
TABLE_NAME,
ENGINE
FROM
information_schema.TABLES
WHERE ENGINE = 'InnoDB';
SELECT
TABLE_SCHEMA,
TABLE_NAME,
(
TABLE_ROWS * AVG_ROW_LENGTH + INDEX_LENGTH
) / 1024 AS size_KB
FROM
information_schema.TABLES
WHERE TABLE_SCHEMA = 'world';
SELECT
SUM(
TABLE_ROWS * AVG_ROW_LENGTH + INDEX_LENGTH
) / 1024 AS size_KB
FROM
information_schema.TABLES;
格式:
mysqldump -uroot -p123 库名 表名 >/bak/库名_表名.sql
语句拼接:
SELECT
CONCAT(
"mysqldump -uroot -p123 ",
TABLE_SCHEMA,
" ",
TABLE_NAME,
" ",
">/bak/",
TABLE_SCHEMA,
"_",
TABLE_NAME,
".sql"
)
FROM
information_schema.TABLES
WHERE TABLE_SCHEMA = 'world'
INTO OUTFILE '/tmp/b.sh';
注意:要想 INTO OUTFILE '/tmp/b.sh’可以执行,需要在/etc/my.cnf配置文件中加入secure-FILE-priv=/tmp的配置,将/tmp目录设为安全目录
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'
SHOW DATABASES; #查看所有数据库
SHOW TABLES; #查看当前库的所有表
SHOW TABLES FROM 库名 #查看某个指定库下的表
SHOW CREATE DATABASE 库名 #查看建库语句
SHOW CREATE TABLE 库名.表名 #查看建表语句
SHOW GRANTS FOR root@'localhost' #查看用户的权限信息
SHOW charset; #查看字符集
SHOW COLLATION #查看校对规则
SHOW PROCESSLIST; #查看数据库连接情况
SHOW INDEX FROM #表的索引情况
SHOW STATUS #数据库状态查看
SHOW STATUS LIKE '%lock%'; #模糊查询数据库某些状态
SHOW VARIABLES #查看所有配置信息
SHOW VARIABLES LIKE '%lock%'; #查看部分配置信息
SHOW ENGINES #查看支持的所有的存储引擎
SHOW ENGINE INNODB STATUS\G #查看InnoDB引擎相关的状态信息
SHOW BINARY LOGS #列举所有的二进制日志
SHOW MASTER STATUS #查看数据库的日志位置信息
SHOW BINLOG evnets IN #查看二进制日志事件
SHOW SLAVE STATUS \G #查看从库状态
SHOW RELAYLOG EVENTS #查看从库relaylog事件信息
DESC (SHOW colums FROM city) #查看表的列定义信息
http://dev.mysql.com/doc/refman/5.7/en/show.html