代码之家  ›  专栏  ›  技术社区  ›  Michael Sorens

最有用的SQL元查询

  •  6
  • Michael Sorens  · 技术社区  · 16 年前

    元查询 ,我的意思是回答问题的查询 关于 具有 数据。

    在我得到许多合理的批评之前,我确实意识到元查询的方法并不理想,正如我雄辩地描述的那样 here 例如不过,我相信他们确实有自己的位置(以至于我创建了一个WinForms用户控件,它支持针对sqlserver、Oracle和MySql的参数化元查询,并详细描述了这个QueryPicker的设计和使用 three-part series 发表在Simple Talk.com上。)

    我使用元查询的动机:

    1. 我经常使用多种数据库类型(SQL Server、Oracle、MySql),并且——实践懒惰的伟大编程理想——我不想每次需要时都要查找或记住一个神秘的SQL配方。我想点击一下。

    当然还有其他(更好的?)方法来获取元信息-- . 特别是SQLServer,它提供了SQLServerManagementStudio。Oracle和MySql工具似乎没有提供相同的有用性(我坦率地承认,我是用我倾向于SQL Server的宇宙观提出这一主张的。:-)即使他们这样做了,他们也会有所不同——我想要一个统一的方法 跨数据库类型 .


    最后,问题是:

    SQL Server、Oracle或MySql元查询的作用


    汇总矩阵

    第一个视图总结了我迄今为止的收藏 数据库类型

    Query                            SQL Server    Oracle     MySql
    
    DB Version                           yes        yes        yes
    Databases with properties            yes                   yes
    Databases with space usage           yes
    National Language Support                       yes
    Procedures and functions             yes                   yes
    Primary keys                         yes                   yes
    Primary to foreign keys              yes
    Session Information/brief            yes
    Session Information/details          yes
    Session SET options                  yes
    Users and Roles                      yes
    Currently running statements         yes
    Constraints                          yes
    Indexes                              yes
    Column info/brief                    yes        yes        yes
    Column info/details                  yes        yes        yes
    Object level details                 yes
    Rows and space used                  yes
    Row/column counts                    yes
    Non-empty tables                     yes        yes        yes
    Show table schema                    yes        yes
    Seed/max values                      yes
    

    按数据库类型列出的引用

    SQL服务器

    System Category
    -----------------
        DB Version
        Databases with properties    http://www.mssqltips.com/tip.asp?tip=1033
        Databases with space usage   http://www.sqlservercentral.com/Forums/Topic261080-5-1.aspx
        Procedures and functions
        Primary keys                 http://databases.aspfaq.com/schema-tutorials/schema-how-do-i-show-all-the-primary-keys-in-a-database.html
        Primary to foreign keys      http://www.sqlservercentral.com/scripts/Miscellaneous/61481/
        Session Information/brief    http://www.sqlservercentral.com/blogs/glennberry/archive/2009/12/28/how-to-get-a-count-of-sql-connections-by-ip-address.aspx
        Session Information/details  http://www.mssqltips.com/tip.asp?tip=1817
        Session SET options
        Users and Roles              http://www.sqlservercentral.com/scripts/users/69379/
        Currently running statements http://www.sqlservercentral.com/articles/DMV/64425/
        Constraints
        Indexes                      http://www.sqlservercentral.com/scripts/Index+Management/63932/
    
    Column Category
    -----------------
        Column info/brief
        Column info/details
    
    Table Category
    -----------------
        Object level details
        Rows and space used          http://www.mssqltips.com/tip.asp?tip=1177
        Row/column counts
        Non-empty tables
    
    DDL Category
    -----------------
        Show table schema            http://www.sqlservercentral.com/scripts/Create+DDL+sql+statements/65863/
    
    Data Category
    -----------------
        Seed/max values
    

    神谕

    System Category
    -----------------
        DB Version
        National Language Support
    
    Column Category
    -----------------
        Column info/brief
        Column info/details
    
    Table Category
    -----------------
        Non-empty tables
    
    DDL Category
    -----------------
        Show table schema
    

    System Category
    -----------------
        DB Version
        Databases
        Procedures and functions
        Primary keys                 http://databases.aspfaq.com/schema-tutorials/schema-how-do-i-show-all-the-primary-keys-in-a-database.html
    
    Column Category
    -----------------
        Column info/brief
        Column info/details
    
    DDL Category
    -----------------
        Show table schema
    
    6 回复  |  直到 16 年前
        1
  •  1
  •   Janek Bogucki Darrell White    16 年前

    oraclesqldeveloper有一组包含这些类别的内置报表。我扩展了其中一个类别。

    About Your Database
    All Objects
    Application Express
    ASH and AWR
    Database Administration
      All Tables
      Cursors
      Database Parameters
      Locks
      Memory
      Sessions
      Storage
      Top SQL
      Users
      Waits and Events
    Data Dictionary
    Jobs
    PLSQL
    Security
    Streams
    Table
    XML
    

    Tables without Indexes
    Tables without Primary Keys
    Tables with Unindexed Foreign Keys
    Largest Average Row Length
    Most Rows
    Unusable Indexes
    

    还有更多可用的报告。

        2
  •  1
  •   Cade Roux    16 年前

    我经常在SQL Server上使用这些工具,包括但不限于:

    • 没有主键的表
    • 没有任何索引的表
    • 不确定的标量用户定义函数
    • 没有扩展属性“MS\u Description”的数据库对象(默认的“Description”属性,用于生成文档)
    • 没有标准文档/注释块的SQL模块(视图、过程、函数、触发器)

    系统特定:

    • 基于不能进行模式化或验证的表/视图的视图(因为它们基于另一个数据库中的视图/表)
    • 视图中系统中未使用的列
    • 某些类型的可空列没有默认值
        3
  •  0
  •   FerranB Tom    16 年前

    在甲骨文上最有用的是 v$session

        4
  •  0
  •   Jeffrey Kemp    16 年前

    Oracle有大量的元数据视图,我查询最多的可能是DBA\u对象,可以查询各种不同的对象类型。可以从其他视图获得相同的信息和更多信息(例如,有关表的更多信息可以在DBA\u表中找到)。

    可以找到Oracle数据字典的良好概述 here .

        5
  •  0
  •   Adam Musch    16 年前

    在互联网上使用一套现成的脚本的问题在于“不是你知道什么,而是你知道什么已经不是了,或者已经不是了。”当你提升脚本时,需要确保它的版本是合适的。例如,10.1或10.2版的Oracle允许将列设置为未使用。它仍然显示在DBA\u TAB\u列中,但实际上已经不存在了。

    更好地理解数据字典中的内容——特别是在Oracle中,随着越来越多的功能朝着这个方向发展(例如DBMS\U STATS package supercing ANALYZE语句),数据库引用(V$,DBA.*)和PL/SQL包和类型引用的内容会更好

    在甲骨文中,有些更为深奥但却很有用:

    DICT -- 数据字典中每个表\视图的名称和简要说明。

    DBA_TAB_MODIFICATIONS --

    V$OBJECT_USAGE -- 与一起使用时 ALTER INDEX ... MONITORING USAGE

    V$SESSION_LONGOPS --

    DBA_HISTOGRAMS -- 你的数据有什么偏差

    DBA_OBJECTS -- 它什么都有

    DBA_SOURCE (by line)/ DBA_TRIGGERS (by block)-- 系统中的所有可执行代码。

        6
  •  0
  •   Community Mohan Dere    9 年前