码农知识堂 - 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
      在这里插入图片描述
  • 相关阅读:
    强大博客搭建全过程(1)-hexo博客搭建保姆级教程
    全网最完整php 禁止eval函数讲解
    力扣(LeetCode)809. 情感丰富的文字(C++)
    集成电路模拟版图入门-转行模拟版图基础学习笔记(二)
    单片机论文参考:1、基于单片机的电子琴
    Java毕业设计之spring boot疫情数据查看系统-课设大作业
    pytest合集(10)— 兼容unittest框架
    [云原生] k8s之存储卷
    【Golang开发面经】深信服(两轮技术面)
    Dockerfile的使用-利用docker构建包含jdk ,vim centos
  • 原文地址:https://blog.csdn.net/suixinfeixiangfei/article/details/127612176
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号