设为首页 加入收藏

TOP

MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard)(一)
2017-10-12 18:13:04 】 浏览:7143
Tags:MySQL and Sql Server:Getting metadata using sql script SQL-92 standard

MySQL:

use sakila;

-- show fields from table_name;
-- show keys from table_name;

SELECT `REFERENCED_TABLE_NAME` 
 FROM `information_schema`.`KEY_COLUMN_USAGE` 
 WHERE 
     `TABLE_NAME` = '[table_containing_foreign_key]' AND 
     `COLUMN_NAME` = '[foreign_key]';
 
 
SHOW COLUMNS FROM City; 

select * from information_schema.tables;

select * from information_schema.tables
where table_schema='sakila';


select * from information_schema.tables;

select * from information_schema.KEY_COLUMN_USAGE;
select * from information_schema.PARAMETERS;
select * from information_schema.VIEWS;
select * from information_schema.TRIGGERS;


-- get Table/Fields metadata
SELECT table_schema, table_name, column_name, ordinal_position, data_type, 
       numeric_precision, column_type, column_default, is_nullable, column_comment 
  FROM information_schema.columns 
 -- WHERE (table_schema='sakila' and table_name = 'city') -- 显示指定表
  WHERE table_schema='sakila'
  order by ordinal_position;
  
-- get Foregn Keys referenced table
SELECT `REFERENCED_TABLE_NAME` 
   FROM `information_schema`.`KEY_COLUMN_USAGE`
   WHERE `TABLE_NAME` = 'city';   
       -- `TABLE_NAME` = 'table_name' AND
       -- `COLUMN_NAME` = 'Column_Name'

-- get indexes (primary and foreign) for a table


-- get All indexes and referreced table
SELECT *
  FROM `information_schema`.`KEY_COLUMN_USAGE`
  WHERE
       `TABLE_NAME` = 'city' AND
       `TABLE_SCHEMA` = 'sakila';
	
SELECT *
  FROM `information_schema`.`REFERENTIAL_CONSTRAINTS`
   WHERE
       `TABLE_NAME` = 'city' AND
       `CONSTRAINT_SCHEMA` = 'sakila';
	
      
--  get STORED PROCEDURES
SELECT * 
  FROM `information_schema`.`ROUTINES`
  WHERE
     `ROUTINE_SCHEMA` = 'sakila';

-- get TRIGGERS

SELECT * 
  FROM `information_schema`.`TRIGGERS`
  WHERE 
     `TRIGGER_SCHEMA` = 'sakila';
     
-- get EVENTS

SELECT * 
  FROM `information_schema`.`EVENTS`
  WHERE 
     `EVENT_SCHEMA` = 'sakila';
     
     
--  get VIEWS
SELECT *
  FROM `information_schema`.`VIEWS`
  WHERE
      `TABLE_NAME` = 'city' AND
      `TABLE_SCHEMA` = 'sakila';
      
-- 'COLUMNS' 'EVENTS'  'FILES'  'TABLES'
SELECT table_schema, table_name, column_name, ordinal_position, data_type, numeric_precision, column_type FROM information_schema.columns 
WHERE table_name = 'TABLES';

  sql server :

--SQL-92 standard

select TABLE_CATALOG, TABLE_SCHEMA,	TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH from DuVehicle.information_schema.columns;

SELECT * FROM DuVehicle.INFORMATION_SCHEMA.PARAMETERS;
GO

GO
/*INFORMATION_SCHEMA views:

View Name        Description 
CHECK_CONSTRAINTS Holds information about constraints in the database 
COLUMN_DOMAIN_USAGE Identifies which columns in which tables are user-defined datatypes 
COLUMN_PRIVILEGES Has one row for each column level permission granted to or by the current user 
COLUMNS Lists one row for each column in each table or view in the database 
CONSTRAINT_COLUMN_USAGE Lists one row for each column that has a constraint defined on it 
CONSTRAINT_TABLE_USAGE Lists one row for each table that has a constraint defined on it 
DOMAIN_CONSTRAINTS Lists the user-defined datatypes that have rules bound to them 
DOMAINS Lists the user-defined datatypes 
KEY_COLUMN_USAGE Lists one row for each column that's defined as a key 
PARAMETERS Lists one row for each parameter in a stored pr
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SQL Server AlwaysOn添加监听器失.. 下一篇Oracle的用户、角色以及权限相关..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目