DBA技术分享(二)-MYSQL常用查询Columns和Views
wptr33 2024-11-17 16:44 24 浏览
概述
分享一下工作中常见的mysql脚本,此次分享的内容如下:
- Columns
- Views
一、Columns
1.1 列出 MySQL 数据库中的表列
select tab.table_schema as database_schema,
tab.table_name as table_name,
col.ordinal_position as column_id,
col.column_name as column_name,
col.data_type as data_type,
case when col.numeric_precision is not null
then col.numeric_precision
else col.character_maximum_length end as max_length,
case when col.datetime_precision is not null
then col.datetime_precision
when col.numeric_scale is not null
then col.numeric_scale
else 0 end as 'precision'
from information_schema.tables as tab
inner join information_schema.columns as col
on col.table_schema = tab.table_schema
and col.table_name = tab.table_name
where tab.table_type = 'BASE TABLE'
and tab.table_schema not in ('information_schema','mysql',
'performance_schema','sys')
-- uncomment line below for current database only
-- and tab.table_schema = database()
-- uncomment line below and provide specific database name
-- and tab.table_schema = 'your_database_name'
order by tab.table_name,
col.ordinal_position;
注意:要查看特定数据库中的列,请取消注释上述子句之一。
说明:
- schema_name - 数据库名称
- table_name - 表名
- column_id - 表列 id,每个表从 1 开始
- column_name - 列的名称
- data_type - 列数据类型
- max_length - 数据类型最大长度
- precision- 数据类型精度
1.2 列出 MySQL 数据库中特定表中的所有列
select ordinal_position as column_id,
column_name as column_name,
data_type as data_type,
case when numeric_precision is not null
then numeric_precision
else character_maximum_length end as max_length,
case when datetime_precision is not null
then datetime_precision
when numeric_scale is not null
then numeric_scale
else 0 end as data_precision,
is_nullable,
column_default
from information_schema.columns
where table_name = 'table name' -- put table name here
-- and table_schema = 'schema name' -- put schema name here
order by ordinal_position;
说明:
- column_id - 表中的列位置,从 1 开始
- column_name - 表中列的名称
- data_type - 列数据类型
- max_length - 数据类型最大长度
- data_precision - 数据类型精度
- is_nullable - 如果列可以为空,则为 YES,否则为 NO
- column_default - 列的默认表达式
1.3 列出 MySQL 数据库中所有包含详细信息的表列(PKs、UKs、FKs、Default、Computed 等)
select col.table_schema as database_name,
col.table_name,
col.column_name,
col.data_type,
case when col.data_type in ('datetime', 'timestamp', 'time')
then col.datetime_precision
else col.numeric_precision end as 'precision',
col.numeric_scale,
col.character_maximum_length as char_length,
col.column_default,
col.generation_expression,
case when (group_concat(constraint_type separator ', '))
like '%PRIMARY KEY%'
then 'YES' else 'NO' end as PK,
case when (group_concat(constraint_type separator ', '))
like '%UNIQUE%'
then 'YES' else 'NO' end as UQ,
case when (group_concat(constraint_type separator ', '))
like '%FOREIGN KEY%'
then 'YES' else 'NO' end as FK,
col.is_nullable
from information_schema.columns col
join information_schema.tables tab
on col.table_schema = tab.table_schema
and col.table_name = tab.table_name
and tab.table_type = 'BASE TABLE'
left join information_schema.key_column_usage kcu
on col.table_schema = kcu.table_schema
and col.table_name = kcu.table_name
and col.column_name = kcu.column_name
left join information_schema.table_constraints tco
on kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
and kcu.table_name = tco.table_name
where col.table_schema not in('information_schema', 'sys',
'performance_schema', 'mysql')
group by 1,2,3,4,5,6,7,8,9,13
order by col.table_schema,
col.table_name,
col.column_name;
说明:
- database_name - 数据库(模式)名称
- table_name - 表名
- column_name - 列名
- data_type - 数据列的类型包含
- 精度- 数字类型的精度或日期时间类型的小数位数
- numeric_scale - 数字数据类型的比例
- char_length - 最大字符长度
- column_default - 列的默认值
- PK - 指示列是否为主键
- FK - 指示列是否为外键
- UQ - 指示列是否必须在表中具有唯一值
- is_nullable - 指示列是否可以为空
1.4 列出 MySQL 数据库中所有计算(生成)的列
select table_schema as database_name,
table_name,
column_name,
data_type,
generation_expression
from information_schema.columns
where length(generation_expression) > 0
and table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
order by table_schema,
table_name,
column_name;
说明:
- database_name - 包含表的数据库(模式)名称
- table_name - 表名
- column_name - 列的名称
- data_type - 列的数据类型
- generation_expression - 计算公式
二、Views
2.1 列出 MySQL 数据库中的视图
select table_schema as database_name,
table_name as view_name
from information_schema.views
where table_schema not in ('sys','information_schema',
'mysql', 'performance_schema')
-- and table_schema = 'database_name' -- put your database name here
order by table_schema,
table_name;
说明:
- database_name - 包含视图的数据库(模式)的名称
- view_name - 视图名称
2.2 列出 MySQL 中的视图及其定义
select vw.table_schema as database_name,
vw.table_name as view_name,
vw.view_definition as definition,
tb.table_comment as description
from information_schema.views as vw
inner join information_schema.tables as tb
on tb.table_name = vw.table_name
-- where vw.table_schema = 'your database name'
order by database_name, view_name;
注意:如果您需要特定数据库(模式)的信息,请取消注释 table_schema 行并提供您的数据库名称。
说明:
- schema_name - 视图的数据库(模式)名称
- view_name - 视图的名称
- 定义- 视图的定义脚本
- 描述- 视图的描述
2.3 列出 MySQL 数据库中的视图中的所有列
select col.table_schema as database_name,
col.table_name as view_name,
col.ordinal_position,
col.column_name,
col.data_type,
case when col.character_maximum_length is not null
then col.character_maximum_length
else col.numeric_precision end as max_length,
col.is_nullable
from information_schema.columns col
join information_schema.views vie on vie.table_schema = col.table_schema
and vie.table_name = col.table_name
where col.table_schema not in ('sys','information_schema',
'mysql', 'performance_schema')
-- and vie.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name,
col.ordinal_position;
说明:
- database_name - 数据库(模式)名称
- view_name - 视图名称
- column_name - 列名
- data_type - 列数据类型
- max_length
- - 列长度:
- 对于字符串列,以字符为单位的最大长度。
- 对于数值列,数值精度。
- is_nullable - 指示列是否允许空值的标志
2.4 列出 MySQL 数据库中视图使用的表
查询在8.0.13 MySQL 版本下执行。
select vtu.view_schema as database_name,
vtu.view_name as view_name,
vtu.table_schema as referenced_database_name,
vtu.table_name as referenced_object_name,
tab.table_type as object_type
from information_schema.view_table_usage vtu
join information_schema.tables tab on vtu.table_schema = tab.table_schema
and vtu.table_name = tab.table_name
where view_schema not in ('sys','information_schema',
'mysql', 'performance_schema')
-- and tab.table_schema = 'database_name' -- put your database name here
order by vtu.view_schema,
vtu.view_name;
说明:
- database_name - 查看数据库(模式)名称
- view_name - 视图名称
- referenced_database_name - 视图引用的表数据库名称
- referenced_object_name - 视图引用的表名
- object_type
- - 引用对象的类型:
- BASE TABLE
- VIEW
- 上一篇:MySQL 查询数据
- 下一篇:MySQL索引、关联子查询与SQL语句的优化技巧
相关推荐
- Linux高性能服务器设计
-
C10K和C10M计算机领域的很多技术都是需求推动的,上世纪90年代,由于互联网的飞速发展,网络服务器无法支撑快速增长的用户规模。1999年,DanKegel提出了著名的C10问题:一台服务器上同时...
- 独立游戏开发者常犯的十大错误
-
...
- 学C了一头雾水该咋办?
-
学C了一头雾水该怎么办?最简单的方法就是你再学一遍呗。俗话说熟能生巧,铁杵也能磨成针。但是一味的为学而学,这个好像没什么卵用。为什么学了还是一头雾水,重点就在这,找出为什么会这个样子?1、概念理解不深...
- C++基础语法梳理:inline 内联函数!虚函数可以是内联函数吗?
-
上节我们分析了C++基础语法的const,static以及this指针,那么这节内容我们来看一下inline内联函数吧!inline内联函数...
- C语言实战小游戏:井字棋(三子棋)大战!文内含有源码
-
井字棋是黑白棋的一种。井字棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、三子旗等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时...
- C++语言到底是不是C语言的超集之一
-
C与C++两个关系亲密的编程语言,它们本质上是两中语言,只是C++语言设计时要求尽可能的兼容C语言特性,因此C语言中99%以上的功能都可以使用C++完成。本文探讨那些存在于C语言中的特性,但是在C++...
- 在C++中,如何避免出现Bug?
-
C++中的主要问题之一是存在大量行为未定义或对程序员来说意外的构造。我们在使用静态分析器检查各种项目时经常会遇到这些问题。但正如我们所知,最佳做法是在编译阶段尽早检测错误。让我们来看看现代C++中的一...
- ESL-通过事件控制FreeSWITCH
-
通过事件提供的最底层控制机制,允许我们有效地利用工具箱,适时选择使用其中的单个工具。FreeSWITCH是一个核心交换与混合矩阵,它周围有几十个模块提供各种功能特性。我们完全控制了所有的即时信息,这些...
- 物理老师教你学C++语言(中篇)
-
一、条件语句与实验判断...
- C语言入门指南
-
当然!以下是关于C语言入门编程的基础介绍和入门建议,希望能帮你顺利起步:C语言入门指南...
- C++选择结构,让程序自动进行决策
-
什么是选择结构?正常的程序都是从上至下顺序执行,这就是顺序结构...
- C++特性使用建议
-
1.引用参数使用引用替代指针且所有不变的引用参数必须加上const。在C语言中,如果函数需要修改变量的值,参数必须为指针,如...
- C++程序员学习Zig指南(中篇)
-
1.复合数据类型结构体与方法的对比C++类:...
- 研一自学C++啃得动吗?
-
研一自学C++啃得动吗?在开始前我有一些资料,是我根据网友给的问题精心整理了一份「C++的资料从专业入门到高级教程」,点个关注在评论区回复“888”之后私信回复“888”,全部无偿共享给大家!!!个人...
- C++关键字介绍
-
下表列出了C++中的常用关键字,这些关键字不能作为变量名或其他标识符名称。1、autoC++11的auto用于表示变量的自动类型推断。即在声明变量的时候,根据变量初始值的类型自动为此变量选择匹配的...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mysql max (33)
- vba instr (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)