你的位置:首页 > 数据库

[数据库]sql:MySQL 6.7 表,视图,存储过程结构查询


#数据库MySQL 6.7use 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';# GetTablesSELECT TABLE_NAME, '' OWNER, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sakila' AND TABLE_TYPE = 'BASE TABLE' ORDER BY 1;#GetTableColumnsSELECT 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;#GetViewsSELECT TABLE_NAME, '' OWNER, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sakila' AND TABLE_TYPE = 'VIEW' ORDER BY 1;#GetViewColumnsSELECT 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;#GetTablePrimaryKeySELECT 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;#GetTableIndexesSELECT INDEX_NAME, COUNT(*) AS COLUMN_COUNT, MAX(NON_UNIQUE) NON_UNIQUE, CASE INDEX_NAME WHEN 'PRIMARY' THEN 1 ELSE 0 END IS_PRIMARYFROM INFORMATION_SCHEMA.STATISTICSWHERE TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'customer'GROUP BY INDEX_NAMEORDER BY INDEX_NAME;SELECT INDEX_NAME, COLUMN_NAMEFROM INFORMATION_SCHEMA.STATISTICSWHERE TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'customer'ORDER BY INDEX_NAME, SEQ_IN_INDEX;#GetMyTableKeysSELECT 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;#GetOthersTableKeysSELECT 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;#GetViewTextSELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = 'sakila' AND TABLE_NAME = 'actor_info';# GetCommandsSELECT ROUTINE_NAME, '' OWNER, CREATED FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'sakila' AND ROUTINE_TYPE = 'PROCEDURE' ORDER BY 1;#GetCommandParameters#GetCommandTextSELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'sakila' AND ROUTINE_NAME = 'film_in_stock' #存储过程