码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Oracle和MySQL查询所有的表信息和字段信息


    Oracle和MySQL查询所有的表信息和字段信息

    • 1. MySQL
      • 1.1 查询表
      • 1.2 查询字段
        • 1.2.1 方式1->SHOW FULL COLUMNS
        • 1.2.2 方式2->information_schema.COLUMNS
      • 1.3 查表和字段
      • 1.4 查表和字段-->转程Oracle需要的数据类型
    • 2. Oracle
      • 2.1 查表和字段的单表查询
      • 2.2 整理查表和字段的sql

    1. MySQL

    1.1 查询表

    • 如下:
      SELECT
      table_comment 表中文名,
      table_name 表英文名
      FROM information_schema.TABLES
      WHERE table_schema = 'test2022'
      ORDER BY table_name;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      在这里插入图片描述

    1.2 查询字段

    1.2.1 方式1->SHOW FULL COLUMNS

    • 如下:
      SHOW FULL COLUMNS FROM `sys_login_log`;
      
      • 1
      在这里插入图片描述

    1.2.2 方式2->information_schema.COLUMNS

    • 如下:
      select * from information_schema.COLUMNS b
      where 1=1
      and b.TABLE_SCHEMA='test2022'
      and b.table_name='sys_login_log';
      
      • 1
      • 2
      • 3
      • 4
      在这里插入图片描述

    1.3 查表和字段

    • 如下:
      SELECT
      a.table_comment 表中文名称,
      a.table_name 表英文名称,
      b.COLUMN_NAME 字段英文名,
      b.column_comment 字段中文名,
      b.column_type 字段类型,
      b.column_key 主键约束
      FROM
      information_schema. TABLES a
      LEFT JOIN information_schema. COLUMNS b ON a.table_name = b.TABLE_NAME
      WHERE 1=1
      and a.table_schema = 'test2022'
      AND a.table_name = 'sys_login_log';
      ORDER BY
      a.table_name;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      在这里插入图片描述

    1.4 查表和字段–>转程Oracle需要的数据类型

    • 如下:
      SELECT
      a.table_comment 表中文名称,
      a.table_name 表英文名称,
      b.COLUMN_NAME 字段英文名,
      b.column_comment 字段中文名,
      -- b.column_type 字段类型,
      (case b.column_type when 'bigint(20) unsigned' then 'NUMBER(20)' 
      when 'tinyint(1)' then 'NUMBER(1)'
      when 'tinyint(4)' then 'NUMBER(4)'
      when 'bigint(20)' then 'NUMBER(20)'
      when 'int(11)' then 'NUMBER(11)'
      WHEN 'json' THEN 'CLOB'
      WHEN 'text' THEN 'CLOB'
      when 'datetime' then 'DATE'
      else b.column_type end) 字段类型,
      -- b.column_key 主键约束
      (case b.column_key when 'PRI' then '是' else b.column_key end) 是否主键
      FROM
      information_schema. TABLES a
      LEFT JOIN information_schema. COLUMNS b ON a.table_name = b.TABLE_NAME
      WHERE 1=1
      and a.table_schema = 'test2022'
      AND a.table_name = 'sys_login_log';
      ORDER BY
      a.table_name;
      
      • 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
      在这里插入图片描述

    2. Oracle

    2.1 查表和字段的单表查询

    • 直接看下面sql ,不截图了,自己下去查查看
      --1.所有用户的表
      select * from all_tables; 
      
      --2.包括系统表
      select * from dba_tables; 
      
      --3.查询所有的表
      select t.* from user_tables t;
      
      --4.查询当前用户下所有的表带注释
      select * from user_tab_comments t where t.TABLE_TYPE='TABLE';
      
      --5.查询所有的字段(表名、字段、字段注释)
      select t.* from user_col_comments t;
      
      --6.根据表名查询所有的字段(带字段名、字段类型、字段长度)
      select * from all_tab_columns t where t.table_name='AC_USER';
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17

    2.2 整理查表和字段的sql

    • 如下:
      select t1.table_name,t1.comments as table_comments, 
      t2.column_name,t2.comments as column_comments,
      t3.data_type,t3.data_length,t3.column_id
      from user_tab_comments t1 
      left join user_col_comments t2 on t2.table_name = t1.table_name
      left join all_tab_columns t3 on(t3.table_name=t1.table_name and t3.column_name=t2.column_name)
      where t1.TABLE_TYPE='TABLE'
      and t1.table_name='SYS_COMPANY_DEPT';
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      在这里插入图片描述
  • 相关阅读:
    Android Studio 导入自己编译的 framework.jar
    上市公司员工及工资数据(2000-2022年)
    【数据库设计和SQL基础语法】--SQL语言概述--SQL的基本结构和语法规则(一)
    van-dialog弹窗异步关闭-校验表单
    java计算机毕业设计springboot+vue健康体检信息管理系统
    ASEMI整流桥KBJ410参数,KBJ410压降,KBJ410热阻
    服务器连接数据库mysql
    上下文管理器与with关键字为业务增加辅助功能
    docker-compose安装并简单配置Cassandra
    CPU GPU TPU NPU 的一些概念 和 使用
  • 原文地址:https://blog.csdn.net/suixinfeixiangfei/article/details/127612176
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号