DBA技术分享(二)-MYSQL常用查询Columns和Views
wptr33 2024-11-17 16:44 33 浏览
概述
分享一下工作中常见的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语句的优化技巧
相关推荐
- 开发者必看的八大Material Design开源项目
-
MaterialDesign是介于拟物和扁平之间的一种设计风格,自从它发布以来,便引起了很多开发者的关注,在这里小编介绍在Android开发者当中里最受青睐的八个MaterialDesign开源项...
- 另类插这么可爱,一定是…(另类t恤)
-
IT之家(www.ithome.com):另类插图:这么可爱,一定是…OSXMavericks和Yosemite打破了苹果对Mac操作系统传统的命名方式,使用加州的某些标志性景点来替换猫...
- Android常用ADB命令(安卓adb工具是什么)
-
杀死应用①根据包名获取APP的PIDadbshellps|grep应用包名②执行kill命令...
- 微软Mac版PowerPoint测试Reading Order Pane功能
-
IT之家5月20日消息,微软公司昨日(5月19日)发布博文,邀请Microsoft365Insiders成员,测试macOS新版PowerPoint演示文稿应用,重点引入...
- Visual Studio跨平台开发实战(4):Xamarin Android控制项介绍
-
前言不同于iOS,Xamarin在VisualStudio中针对Android,可以直接设计使用者界面.在本篇教学文章中,笔者会针对Android的专案目录结构以及基本控制项进行介绍,包...
- 用云存储30分钟快速搭建APP,你信吗?
-
背景不管你承认与否,移动互联的时代已经到来,这是一个移动互联的时代,手机已经是当今世界上引领潮流的趋势,大型的全球化企业和中小企业都把APP程序开发纳入到他们的企业发展策略当中。但随着手机APP上传的...
- 谷歌P图神器来了!不用学不用教,输入一句话,分分钟给结果
-
Pine发自凹非寺量子位|公众号QbitAI当你拍照片时,“模特不好好配合”怎么办?...
- iOS文本编辑控件UITextField和UITextVie
-
记录一个菜鸟的IOS学习之旅,如能帮助正在学习的你,亦枫不胜荣幸;如路过的大神如指教几句,亦枫感激涕淋!细心的朋友可能已经注意到了,IOS学习之旅系列教程在本篇公众号的文章中,封面已经换成美女图片了,...
- Android入门图文教程集锦(android 入门教程)
-
Android入门视频教程集锦AndroidStudio错误gradientandroid:endXattributenotfound...
- 如何使用Android自定义复合视图(如何使用android自定义复合视图)
-
在最近的一个客户应用中,我遇到了一个需求,根据选定的值来生成指定数量的编辑框字段,这样用户可以输入人物信息。最初我的想法是把这些逻辑放到Fragment中,只是根据选中值的变化来向线性布局容器中增加编...
- 原生安卓开发app的框架frida常用关键代码定位
-
前言有时候可能会对APP进行字符串加密等操作,这样的话你的变量名等一些都被混淆了,看代码就可能无从下手...
- 教程10 | 三分钟搞定一个智能输入法程序
-
一案例描述1、考核知识点网格布局线性布局样式和主题Toast2、练习目标掌握网格布局的使用掌握Toast的使用掌握线性布局的使用...
- (Android 8.1) 功能与新特性(android的功能)
-
和你一起终身学习,这里是程序员AndroidAndroid8.1(API级别27)为用户和开发人员引入了各种新特性和功能。本文档重点介绍了开发人员的新功能。通过本章阅读,您将获取到以下内容:Andr...
- 怎样设置EditText内部文字被锁定不可删除和修改
-
在做项目的时候,我曾经遇到过这样的要求,就是跟百度贴吧客户端上的一样,在回复帖子的时候,在EditText中显示回复人的名字,而且这个名字不可以修改和删除,说白了就是不可操作,只能在后面输入内容。在E...
- 如何阻止 Android 活动启动时 EditText 获得焦点
-
技术背景在Android开发中,当活动启动时,EditText有时会自动获得焦点并弹出虚拟键盘,这可能不是用户期望的行为。为了提升用户体验,我们需要阻止...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
-
- 开发者必看的八大Material Design开源项目
- 另类插这么可爱,一定是…(另类t恤)
- Android常用ADB命令(安卓adb工具是什么)
- 微软Mac版PowerPoint测试Reading Order Pane功能
- Visual Studio跨平台开发实战(4):Xamarin Android控制项介绍
- 用云存储30分钟快速搭建APP,你信吗?
- 谷歌P图神器来了!不用学不用教,输入一句话,分分钟给结果
- iOS文本编辑控件UITextField和UITextVie
- Android入门图文教程集锦(android 入门教程)
- 如何使用Android自定义复合视图(如何使用android自定义复合视图)
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (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)
- c语言 switch (34)
- git commit (34)