DBA技术分享(九)- MySQL数据库中查找最常用的数据类型
wptr33 2024-12-29 06:24 17 浏览
一、概述
今天分享几个关于MySQL数据类型的查询,具体如下:
- 在 MySQL 数据库中查找最常用的数据类型
- 查找 MySQL 数据库中的所有数字列
- 查找 MySQL 数据库中的所有字符串(字符)列
- 查找 MySQL 数据库中的所有日期和时间列
- 查找 MySQL 数据库中的所有枚举列
- 查找 MySQL 数据库中的所有空间数据列
- 查找 MySQL 数据库中的所有 JSON 数据列
- 在 MySQL 数据库中查找大对象 (LOB) 数据类型列
- 在 MySQL 数据库中查找具有大对象 (LOB) 数据类型列的表
二、相关SQL
2.1 在 MySQL 数据库中查找最常用的数据类型
select data_type,
count(*) as columns,
cast(100*count(*)/sum_all.columns as decimal(36,2))
as percent_columns,
count(distinct concat(col.table_schema, '.', col.table_name))
as tables,
cast(100*count(distinct concat(col.table_schema,'.',col.table_name))
/ sum_all.tables as decimal(36,2)) as percent_tables
from information_schema.columns col
join (select count(distinct concat(c.table_schema, '.', c.table_name))
as tables,
count(*) as columns
from information_schema.columns c
join information_schema.tables t
on c.table_schema = t.table_schema
and c.table_name = t.table_name
where t.table_schema not in ('information_schema', 'mysql',
'performance_schema', 'sys')
and t.table_type = 'BASE TABLE'
) sum_all on true
join information_schema.tables tab
on col.table_schema = tab.table_schema
and col.table_name = tab.table_name
where tab.table_schema not in ('information_schema', 'mysql',
'performance_schema', 'sys')
and tab.table_type = 'BASE TABLE'
group by data_type,
sum_all.columns,
sum_all.tables
order by columns desc;
说明:
- data_type - 没有长度或精度的内置或用户数据类型,例如 int、varchar 或 datetime
- columns - 具有此数据类型的数据库(模式)中的列数
- percent_columns - 具有此数据类型的列的百分比。行总数为 100%
- tables- 数据库(模式)中具有此数据类型的表数
- percent_tables - 具有此数据类型的列的表的百分比。
2.2 查找 MySQL 数据库中的所有数字列
select col.table_schema as database_name,
col.table_name,
col.ordinal_position as col_id,
col.column_name,
col.data_type,
col.numeric_precision,
col.numeric_scale
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
and tab.table_name = col.table_name
and tab.table_type = 'BASE TABLE'
where col.data_type in ('tinyint', 'smallint', 'mediumint',
'int', 'bigint', 'decimal', 'bit',
'float', 'double')
and col.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
--and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name,
col.ordinal_position;
说明:
- database_name - 数据库的名称(模式)
- table_name - 表的名称
- column_id - 表中的列位置
- column_name - 列的名称
- data_type - 数据类型
- numeric_precision - 列的精度
- numeric_scale - 列的比例
2.3 查找 MySQL 数据库中的所有字符串(字符)列
select col.table_schema as database_name,
col.table_name,
col.ordinal_position as column_id,
col.column_name,
col.data_type,
col.character_maximum_length as maximum_length,
col.character_set_name
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
and tab.table_name = col.table_name
and tab.table_type = 'BASE TABLE'
where col.data_type in ('char', 'varchar', 'binary', 'varbinary',
'blob', 'tinyblob', 'mediumblob', 'longblob',
'text', 'tinytext', 'mediumtext', 'longtext'
'enum', 'set')
and col.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
--and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name,
col.ordinal_position;
说明:
- database_name - 数据库的名称(模式)
- table_name - 表的名称
- column_id - 表中的列位置
- column_name - 列的名称
- data_type - 数据类型
- maximum_length - 字符的最大长度
- character_set_name - 字符集名称
2.4 查找 MySQL 数据库中的所有日期和时间列
select col.table_schema as database_name,
col.table_name,
col.ordinal_position as column_id,
col.column_name,
col.data_type,
col.datetime_precision
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
and tab.table_name = col.table_name
and tab.table_type = 'BASE TABLE'
where col.data_type in ('date', 'time', 'datetime', 'year', 'timestamp')
and col.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
--and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name,
col.ordinal_position;
说明:
- database_name - 数据库的名称(模式)
- table_name - 表的名称
- column_id - 表中的列位置
- column_name - 列的名称
- data_type - 数据类型
- datetime_precision - 小数秒精度
2.5 查找 MySQL 数据库中的所有枚举列
select col.table_schema as database_name,
col.table_name,
col.ordinal_position as column_id,
col.column_name,
col.data_type,
trim(leading 'enum' from col.column_type) as enum_values
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
and tab.table_name = col.table_name
and tab.table_type = 'BASE TABLE'
where col.data_type in ('enum')
and col.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
--and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name,
col.ordinal_position;
说明:
- database_name - 数据库的名称(模式)
- table_name - 表的名称
- column_id - 表中的列位置
- column_name - 列的名称
- data_type - 数据类型
- enum_values - 声明可能的枚举值
2.6 查找 MySQL 数据库中的所有空间数据列
select col.table_schema as database_name,
col.table_name,
col.ordinal_position as column_id,
col.column_name,
col.data_type,
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 table_type = 'BASE TABLE'
where col.data_type in ('geometry', 'point', 'linestring', 'polygon',
'multipoint', 'multilinestring', 'multipolygon',
'geometrycollection')
and col.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
-- and table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name;
说明:
- database_name - 数据库的名称(模式)
- table_name - 表的名称
- column_id - 表中的列位置
- column_name - 列的名称
- data_type - 空间数据的类型:
(1)GEOMETRY
(2)POINT
(3)LINESTRING
(4)POLYGON
(5)MULTIPOINT
(6)MULTILINESTRING
(7)MULTIPOLYGON
(8)GEOMETRYCOLLECTION
- is_nullable - 指示列是否可以包含空值
2.7 查找 MySQL 数据库中的所有 JSON 数据列
select col.table_schema as database_name,
col.table_name,
col.ordinal_position as column_id,
col.column_name,
col.data_type
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
and tab.table_name = col.table_name
and tab.table_type = 'BASE TABLE'
where col.data_type in ('json')
and col.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
--and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
col.table_name,
col.ordinal_position;
说明:
- database_name - 数据库的名称(模式)
- table_name - 表的名称
- column_id - 表中的列位置
- column_name - 列的名称
- data_type - 数据类型
2.8 在 MySQL 数据库中查找大对象 (LOB) 数据类型列
select tab.table_schema as database_name,
tab.table_name,
col.column_name,
col.data_type
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_schema = 'your database name'
and tab.table_type = 'BASE TABLE'
and col.data_type in ('blob', 'mediumblob', 'longblob',
'text','mediumtext','longtext')
order by tab.table_name,
col.column_name;
说明:
- schema_name - 数据库的名称(模式)
- table_name - 表的名称
- column_name - 列的名称
- data_type - 数据类型
2.9 在 MySQL 数据库中查找具有大对象 (LOB) 数据类型列的表
select tab.table_name,
count(*) as columns
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
and col.data_type in ('blob', 'mediumblob', 'longblob',
'text', 'mediumtext', 'longtext')
where tab.table_schema = 'your database name'
and tab.table_type = 'BASE TABLE'
group by tab.table_name
order by tab.table_name;
说明:
- table_name - 表的名称
- columns - 表中的 LOB 列数
小结
后面会分享更多Linux和DBA方面内容,感兴趣的朋友可以关注下!
相关推荐
- VPS主机搭建Ghost环境:Nginx Node.js MariaDB
-
Ghost是一款个人博客系统,它是使用Node.js语言和MySQL数据库开发的,同时支持MySQL、MariaDB、SQLite和PostgreSQL。用户可以在支持Node.js的服务器上使用自己...
- centos7飞速搭建zabbix5.0并添加windows、linux监控
-
一、环境zabbix所在服务器系统为centos7,监控的服务器为windows2016和centos7。二、安装zabbix官方安装帮助页面...
- Zabbix5.0安装部署
-
全盘展示运行状态,减轻运维人员的重复性工作量,提高系统排错速度,加速运维知识学习积累。1.png...
- MariaDB10在CentOS7系统下,迁移数据存储位置
-
背景在CentOS7下如果没有默认安装MySQL数据库,可以选择安装MariaDB,最新的版本现在是10可以选择直接yum默认安装的方式yum-yinstallmariadbyum-yi...
- frappe项目安装过程
-
1,准备一台虚拟机,debian12或者ubuntusever22.04.3可以用virtualbox/qemu,或者你的超融合服务器安装一些常用工具和依赖库我这里选择server模式安装,用tab...
- 最新zabbix一键安装脚本(基于centos8)
-
一、环境准备注意:操作系统必须是centos8及以上的,因为我配的安装源是centos8的。并且必须连接互联网,脚本是基于yum安装的!!!...
- ip地址管理之phpIPAM保姆级安装教程 (原创)
-
本教程基于Ubuntu24.04LTS,安装phpIPAM(最新稳定版1.7),使用Apache、PHP8.3和MariaDB,遵循最佳实践,确保安全性和稳定性。一、环境准备1....
- centos7傻瓜式安装搭建zabbix5.0监控服务器教程
-
zabbix([`zaebiks])是一个基于WEB界面的提供分布式系统监视...
- zabbix7.0LTS 保姆级安装教程 小白也能轻松上手安装
-
系统环境:rockylinux9.4(yumupdate升级到最新版本)数据库:mariadb10.5.22第一步:关闭防火墙和selinux使用脚本关闭...
- ubuntu通过下载安装包安装mariadb10.4
-
要在Ubuntu18.04上安装MariaDB10.4.34,用的是那个tar.gz的安装包。步骤大概是:...
- 从0到1:基于 Linux 快速搭建高可用 MariaDB Galera 集群(实战指南)
-
在企业生产环境中,数据库的高可用性至关重要。今天带你从0到1,手把手在Linux系统上快速搭建一个高可用MariaDBGaleraCluster,实现数据库同步复制、故障自动恢复,保障业务...
- Windows 中安装 MariaDB 数据库
-
mariadb在Windows下的安装非常简单,下载程序双击运行就可以了。需要注意:mariadb和MySQL数据库在Windows下默认是不区分大小写的,但是在Linux下是区分...
- SQL执行顺序(SqlServer)
-
学习SQL这么久,如果突然有人问你SQL的执行顺是怎么样的?是不是很多人会觉得C#、JavaScript都是根据编程顺序来处理的,那么SQL也是根据编程顺序来执行的吗?...
- C# - StreamWriter与StreamReader 读写文件 101
-
读写文本文件的方式:1)File静态类的File.ReadAllLines();与File.WriteAllLines();方法进行读写...
- C#中的数组探究与学习
-
C#中的数组一般分为:...
- 一周热门
-
-
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)