程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> sql:MySQL 6.7 表,視圖,存儲過程結構查詢,mysql6.7

sql:MySQL 6.7 表,視圖,存儲過程結構查詢,mysql6.7

編輯:MySQL綜合教程

sql:MySQL 6.7 表,視圖,存儲過程結構查詢,mysql6.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' #存儲過程

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved