下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

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

作者:匿名     来源: 互联网点击数:2549发布时间: 2022-06-21 20:21:57

标签: MySQL查询数据库

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

  一、MySQL默认值相关查询

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

  复制

  1. select table_schema as database_name,

  2. table_name,

  3. column_name,

  4. column_default

  5. from information_schema.columns

  6. where column_default is not null

  7. and table_schema not in ('information_schema', 'sys',

  8. 'performance_schema','mysql')

  9. -- and table_schema = 'your database name'

  10. order by table_schema,

  11. table_name,

  12. ordinal_position;

  说明:

  database_name - 数据库的名称(模式)

  table_name - 表的名称

  column_name - 列的名称

  column_default - 定义此列默认值的 SQL 表达式

  2. MySQL数据库默认值汇总

  复制

  1. select column_default,

  2. count(distinct concat(col.table_schema, '.', col.table_name)) as tables,

  3. count(column_name) as columns

  4. from information_schema.columns col

  5. join information_schema.tables tab on col.table_schema = tab.table_schema

  6. and col.table_name = tab.table_name

  7. where col.table_schema not in ('sys', 'information_schema',

  8. 'mysql', 'performance_schema')

  9. and tab.table_type = 'BASE TABLE'

  10. group by column_default

  11. order by tables desc,

  12. columns desc;

  说明:

  column_default -为没有默认值的列定义默认约束(公式)和NULL

  tables- 具有此约束的表数(或具有对NULL行没有约束的列的表数)

  columns - 具有此特定约束的列数(或NULL行没有约束)

  二、约束

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

  复制

  1. select tab.table_schema as database_name,

  2. tab.table_name,

  3. col.ordinal_position as column_id,

  4. col.column_name,

  5. col.data_type,

  6. case when col.numeric_precision is not null

  7. then col.numeric_precision

  8. else col.character_maximum_length end as max_length,

  9. case when col.datetime_precision is not null

  10. then col.datetime_precision

  11. when col.numeric_scale is not null

  12. then col.numeric_scale

  13. else 0 end as 'precision'

  14. from information_schema.tables as tab

  15. join information_schema.columns as col

  16. on col.table_schema = tab.table_schema

  17. and col.table_name = tab.table_name

  18. and col.is_nullable = 'no'

  19. where tab.table_schema not in ('information_schema', 'sys',

  20. 'mysql','performance_schema')

  21. and tab.table_type = 'BASE TABLE'

  22. -- and tab.table_schema = 'database name'

  23. order by tab.table_schema,

  24. tab.table_name,

  25. col.ordinal_position;

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

  说明:

  database_name - 数据库的名称(模式)

  table_name - 表的名称

  column_id - 列在表中的位置

  column_name - 列的名称

  data_type - 列数据类型

  max_length - 数据类型的最大长度

  precision- 数据类型的精度

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

  复制

  1. select c.table_schema as database_name,

  2. c.table_name,

  3. c.column_name,

  4. case c.is_nullable

  5. when 'NO' then 'not nullable'

  6. when 'YES' then 'is nullable'

  7. end as nullable

  8. from information_schema.columns c

  9. join information_schema.tables t

  10. on c.table_schema = t.table_schema

  11. and c.table_name = t.table_name

  12. where c.table_schema not in ('mysql', 'sys', 'information_schema',

  13. 'performance_schema')

  14. and t.table_type = 'BASE TABLE'

  15. -- and t.table_schema = 'database_name' -- put your database name here

  16. order by t.table_schema,

  17. t.table_name,

  18. c.column_name;

  说明:

  database_name - 数据库名称(模式)

  table_name - 表名

  column_name - 列名

  nullable- 列的可空性属性:is nullable- 可以为空,not nullable- 不可为空

  来源: 今日头条

  >>>>>>点击进入数据库专题

赞(14)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程