• sql:MySQL 6.7 表,视图,存储过程结构查询


    #数据库MySQL 6.7
    
    use sakila;
    #查询表名
    show tables;
    #
    SELECT TABLE_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='sakila';
    
    select column_name from information_schema.columns where table_schema='sakila' and table_name='actor';
    #表结构 字段名, 类型,长度
    select * from information_schema.columns where table_schema='sakila' and table_name='actor';
    
    # GetTables
    SELECT TABLE_NAME, '' OWNER, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sakila' AND TABLE_TYPE = 'BASE TABLE' ORDER BY 1;
    #GetTableColumns
    SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, CASE IS_NULLABLE WHEN 'NO' THEN 0 ELSE 1 END IS_NULLABLE, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'address' ORDER BY ORDINAL_POSITION;
    #GetViews
    SELECT TABLE_NAME, '' OWNER, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sakila' AND TABLE_TYPE = 'VIEW' ORDER BY 1;
    #GetViewColumns
    SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, CASE IS_NULLABLE WHEN 'NO' THEN 0 ELSE 1 END IS_NULLABLE, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'actor_info' ORDER BY ORDINAL_POSITION;
    #GetTablePrimaryKey
    SELECT t1.CONSTRAINT_NAME, t1.COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1 INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2 ON t2.TABLE_SCHEMA = t1.TABLE_SCHEMA  AND t2.TABLE_NAME = t1.TABLE_NAME AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME WHERE t1.TABLE_SCHEMA = 'sakila' AND t1.TABLE_NAME = 'address' AND t2.CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY t1.ORDINAL_POSITION;
    #GetTableIndexes
    SELECT INDEX_NAME, COUNT(*) AS COLUMN_COUNT, MAX(NON_UNIQUE) NON_UNIQUE,  CASE INDEX_NAME WHEN 'PRIMARY' THEN 1 ELSE 0 END IS_PRIMARY
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE  TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'customer'
    GROUP BY INDEX_NAME
    ORDER BY INDEX_NAME;
    
    SELECT INDEX_NAME, COLUMN_NAME
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE  TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'customer'
    ORDER BY INDEX_NAME, SEQ_IN_INDEX;
    #GetMyTableKeys
    SELECT CONSTRAINT_NAME
                     FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t1
                     WHERE t1.TABLE_SCHEMA = 'sakila' AND t1.TABLE_NAME = 'customer'
                      AND CONSTRAINT_TYPE = 'FOREIGN KEY';
    
     SELECT t1.CONSTRAINT_NAME, t1.COLUMN_NAME, t1.POSITION_IN_UNIQUE_CONSTRAINT,t1.REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
                    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1
                      INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2
                      ON t2.TABLE_SCHEMA = t1.TABLE_SCHEMA
                     AND t2.TABLE_NAME = t1.TABLE_NAME
                      AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME
                     WHERE t1.TABLE_SCHEMA = 'sakila' AND t1.TABLE_NAME = 'customer'
                      AND t2.CONSTRAINT_TYPE = 'FOREIGN KEY'
                    ORDER BY t1.CONSTRAINT_NAME, t1.POSITION_IN_UNIQUE_CONSTRAINT;
    
    #GetOthersTableKeys
    SELECT DISTINCT CONSTRAINT_NAME
                     FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1
                     WHERE t1.TABLE_SCHEMA = 'sakila' AND t1.REFERENCED_TABLE_NAME = 'customer';
    
                     SELECT t1.CONSTRAINT_NAME, t1.TABLE_NAME, t1.COLUMN_NAME, t1.POSITION_IN_UNIQUE_CONSTRAINT, t1.REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
                     FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1
                      INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2
                      ON t2.TABLE_SCHEMA = t1.TABLE_SCHEMA
                      AND t2.TABLE_NAME = t1.TABLE_NAME
                      AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME
                     WHERE t1.TABLE_SCHEMA = 'sakila' AND t1.REFERENCED_TABLE_NAME = 'customer'
                      AND t2.CONSTRAINT_TYPE = 'FOREIGN KEY'
                     ORDER BY t1.CONSTRAINT_NAME, t1.POSITION_IN_UNIQUE_CONSTRAINT;
    #GetViewText
    SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'actor_info';
    # GetCommands
    SELECT ROUTINE_NAME, '' OWNER, CREATED FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'sakila' AND ROUTINE_TYPE = 'PROCEDURE' ORDER BY 1;
    #GetCommandParameters
    #GetCommandText
    SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'sakila' AND ROUTINE_NAME = 'film_in_stock' #存储过程
    

      

  • 相关阅读:
    4组Alpha冲刺2/6
    4组Alpha冲刺1/6
    4组Alpha冲刺3/6
    4组Alpha冲刺3/6
    4组Alpha冲刺4/6
    原创视频作品汇总(更新至2012.8.04)
    PPT全转通2.0发布
    PPT全转通1.3
    [原创]WIN8系统的远程桌面漏洞 利用QQ拼音纯净版实现提权
    原创钢琴曲 《夏季来临之前的歌》
  • 原文地址:https://www.cnblogs.com/geovindu/p/4818866.html
Copyright © 2020-2023  润新知