MySQL的表、字段信息查询

我不是女神ヾ 2022-04-24 08:28 381阅读 0赞

1.查询表信息

  1. SELECT
  2. *
  3. FROM
  4. information_schema. TABLES
  5. WHERE
  6. table_schema = '数据库名'

2.查询表字段详细信息

  1. select * from information_schema.columns
  2. where table_schema = '数据库名'
  3. and table_name = '表名';

3.查询表字段(列ID/列名/数据类型/长度/精度/是否可以为null/默认值/是否自增/是否是主键/列描述)

  1. select ORDINAL_POSITION as Colorder,Column_Name as ColumnName,data_type as TypeName,COLUMN_COMMENT as DeText,
  2. (case when data_type = 'float' or data_type = 'double' or data_type = 'decimal' then NUMERIC_PRECISION else CHARACTER_MAXIMUM_LENGTH end ) as length,
  3. NUMERIC_SCALE as Scale,( case when EXTRA='auto_increment' then 1 else 0 end) as IsIdentity,(case when COLUMN_KEY='PRI' then 1 else 0 end) as IsPK,
  4. (case when IS_NULLABLE = 'NO' then 0 else 1 end)as CanNull,COLUMN_DEFAULT as DefaultVal
  5. from information_schema.columns where table_schema = '数据库名' and table_name = '表名' order by ORDINAL_POSITION asc;

转自于https://www.cnblogs.com/xuesheng/p/9413258.html

4.查询表和主键

  1. select table_schema, table_name,column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE t where t.table_schema='数据库名';

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读