MySQL默认值和约束的查询方法

数据库 MySQL
本文主要介绍关于MySQL默认值和约束的查询方法,下面,我们一起来看。

一、MySQL默认值相关查询

1. 列出 MySQL 数据库中的表默认值

select table_schema as database_name,
       table_name,
       column_name,
       column_default
from information_schema.columns
where  column_default is not null
      and table_schema not in ('information_schema', 'sys',
                               'performance_schema','mysql')
--    and table_schema = 'your database name'
order by table_schema,
         table_name,
         ordinal_position;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_name - 列的名称
  • column_default - 定义此列默认值的 SQL 表达式

DBA技术分享(五)-mysql默认值和约束的查询方法

2. MySQL数据库默认值汇总

select column_default,
       count(distinct concat(col.table_schema, '.', col.table_name)) as tables,
       count(column_name) as columns
from information_schema.columns col
join information_schema.tables tab on col.table_schema = tab.table_schema
                                   and col.table_name = tab.table_name
where col.table_schema not in ('sys', 'information_schema', 
                               'mysql', 'performance_schema')
      and tab.table_type = 'BASE TABLE'
group by column_default
order by tables desc,
         columns desc;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

说明:

  • column_default -为没有默认值的列定义默认约束(公式)和NULL
  • tables- 具有此约束的表数(或具有对NULL行没有约束的列的表数)
  • columns - 具有此特定约束的列数(或NULL行没有约束)

DBA技术分享(五)-mysql默认值和约束的查询方法

二、约束

1. 列出了数据库(模式)中的所有不可为空的列

select tab.table_schema as database_name,
    tab.table_name,
    col.ordinal_position as column_id,
    col.column_name,
    col.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
join information_schema.columns as col
        on col.table_schema = tab.table_schema
        and col.table_name = tab.table_name
        and col.is_nullable = 'no'
where tab.table_schema not in ('information_schema', 'sys', 
                               'mysql','performance_schema')
      and tab.table_type = 'BASE TABLE'
      -- and tab.table_schema = 'database name'
order by tab.table_schema,
         tab.table_name,
         col.ordinal_position;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

注意:如果您需要特定数据库(模式)的信息,请取消注释 where 子句中的条件并提供您的数据库名称。

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 列在表中的位置
  • column_name - 列的名称
  • data_type - 列数据类型
  • max_length - 数据类型的最大长度
  • precision- 数据类型的精度

DBA技术分享(五)-mysql默认值和约束的查询方法

2. 检查 MySQL 数据库中的列是否可为空

select c.table_schema as database_name,
       c.table_name,
       c.column_name,
       case c.is_nullable
            when 'NO' then 'not nullable'
            when 'YES' then 'is nullable'
       end as nullable
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 c.table_schema not in ('mysql', 'sys', 'information_schema',
                             'performance_schema')
      and t.table_type = 'BASE TABLE'
      -- and t.table_schema = 'database_name' -- put your database name here
order by t.table_schema,
         t.table_name,
         c.column_name;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

说明:

  • database_name - 数据库名称(模式)
  • table_name - 表名
  • column_name - 列名
  • nullable- 列的可空性属性:is nullable- 可以为空,not nullable- 不可为空

DBA技术分享(五)-mysql默认值和约束的查询方法

责任编辑:赵宁宁 来源: 今日头条
相关推荐

2010-11-23 16:49:42

MySQL设置当前时间

2010-06-10 17:59:05

2011-05-20 11:33:06

ORACLE索引约束

2019-11-15 10:01:07

MySQL数据库数据

2010-11-25 16:40:11

MySQL大表重复字段

2021-02-25 13:40:17

MySQL数据库默认值

2009-08-19 15:08:30

C#泛型

2010-10-22 16:56:35

sql server删

2009-06-08 10:20:01

Hibernate查询

2010-10-29 11:22:23

Oracle用户会话

2009-06-17 15:52:23

Hibernate查询

2010-11-15 16:26:46

Oracle系统时间

2010-09-28 10:23:36

SQL修改字段

2009-06-29 09:03:31

Hibernate多条

2012-08-01 09:50:11

交互设计UI设计

2009-05-21 09:24:42

表空间查询Oracle

2018-09-06 16:46:33

数据库MySQL分页查询

2010-09-14 15:51:15

sql遍历

2022-11-07 08:00:00

机器学习数字孪生数据科学

2012-07-30 09:50:28

MongoDB
点赞
收藏

51CTO技术栈公众号