MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard)

发布时间:2017-7-9 7:35:47编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard) ",主要涉及到MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard) 方面的内容,对于MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard) 感兴趣的同学可以参考一下。

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 procedure or user-defined function 
REFERENTIAL_CONSTRAINTS Lists one row for each foreign constraint 
ROUTINES Lists one row for each stored procedure or user-defined function 
ROUTINE_COLUMNS Contains one row for each column returned by any table-valued functions 
SCHEMATA Contains one row for each database 
TABLE_CONSTRAINTS Lists one row for each constraint defined in the current database 
TABLE_PRIVILEGES Has one row for each table level permission granted to or by the current user 
TABLES Lists one row for each table or view in the current database 
VIEW_COLUMN_USAGE Lists one row for each column in a view including the base table of the column where possible 
VIEW_TABLE_USAGE Lists one row for each table used in a view 
VIEWS Lists one row for each view

*/

  SQL Server Metadata Toolkit 2005 - 2014 https://sqlmetadata.codeplex.com/

Automatic Graph Layout

https://github.com/Microsoft/automatic-graph-layout

https://www.nuget.org/packages/Microsoft.Msagl/

http://www.microsoft.com/en-us/download/details.aspx?id=52034

MySQL  表的注释(备注)信息

-- 创建带解释的表
CREATE TABLE groups( 
 gid INT PRIMARY KEY AUTO_INCREMENT COMMENT '设置主键自增ID',
 gname VARCHAR(200) COMMENT '名称'
 ) COMMENT='群表';
 
 
 SHOW CREATE TABLE city;
#查看表的注释
SELECT table_name,table_comment FROM information_schema.tables WHERE table_schema = 'sakila' AND table_name ='groups';

# 查看列的注释 涂聚文 Geovin Du
SHOW FULL COLUMNS FROM city;

SELECT column_name, column_comment FROM information_schema.columns WHERE table_schema ='sakila' AND table_name = 'groups';

-- 表注释,列注释city_id
 use sakila; 
 
 -- 表描述
 ALTER TABLE city  COMMENT='城市表';

-- 更改注释
alter table city modify column city varchar(50) comment '城市名称';

ALTER TABLE city MODIFY COLUMN `city_id` smallint(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市ID';

ALTER TABLE city MODIFY COLUMN `country_id` smallint(5) unsigned NOT NULL COMMENT '国家代码';


ALTER TABLE city MODIFY COLUMN city_id smallint(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市ID';

ALTER TABLE city MODIFY COLUMN country_id smallint(5) unsigned NOT NULL COMMENT '国家代码';

 
alter table city modify column last_update timestamp comment '更新时间';

SHOW CREATE TABLE city;
#查看表的注释
SELECT table_name as '表名',table_comment as '表注释' FROM information_schema.tables WHERE table_schema = 'sakila' AND table_name ='city';

# 查看列的注释
SHOW FULL COLUMNS FROM city;

SELECT column_name as '列名', column_comment as '列注释' FROM information_schema.columns WHERE table_schema ='sakila' AND table_name = 'city';

SELECT * FROM information_schema.columns WHERE table_schema ='sakila' AND table_name = 'city';

  

 -- 数据库中所有表的主键和外键约束信息的Sql语句
 -- 1方法
 SELECT C.TABLE_SCHEMA,              -- 拥有者,
           C.REFERENCED_TABLE_NAME,  -- 父表名称 ,
           C.REFERENCED_COLUMN_NAME,  -- 父表字段 ,
           C.TABLE_NAME,             -- 子表名称,
           C.COLUMN_NAME,            -- 子表字段,
           C.CONSTRAINT_NAME,       -- 约束名,
           T.TABLE_COMMENT,          -- 表注释,
           R.UPDATE_RULE,            -- 约束更新规则,
           R.DELETE_RULE           -- 约束删除规则
      FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C
      JOIN INFORMATION_SCHEMA. TABLES T
        ON T.TABLE_NAME = C.TABLE_NAME
      JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R
        ON R.TABLE_NAME = C.TABLE_NAME
       AND R.CONSTRAINT_NAME = C.CONSTRAINT_NAME
       AND R.REFERENCED_TABLE_NAME = C.REFERENCED_TABLE_NAME
      WHERE C.REFERENCED_TABLE_NAME IS NOT NULL ;  
      
  SELECT C.TABLE_SCHEMA            拥有者,
           C.REFERENCED_TABLE_NAME  父表名称 ,
           C.REFERENCED_COLUMN_NAME 父表字段 ,
           C.TABLE_NAME             子表名称,
           C.COLUMN_NAME            子表字段,
           C.CONSTRAINT_NAME        约束名,
           T.TABLE_COMMENT          表注释,
           R.UPDATE_RULE            约束更新规则,
           R.DELETE_RULE            约束删除规则
      FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C
      JOIN INFORMATION_SCHEMA. TABLES T
        ON T.TABLE_NAME = C.TABLE_NAME
      JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R
        ON R.TABLE_NAME = C.TABLE_NAME
       AND R.CONSTRAINT_NAME = C.CONSTRAINT_NAME
       AND R.REFERENCED_TABLE_NAME = C.REFERENCED_TABLE_NAME
      WHERE C.REFERENCED_TABLE_NAME IS NOT NULL ;   
-- 2方法
   select O.CONSTRAINT_SCHEMA,O.CONSTRAINT_NAME,O.TABLE_SCHEMA,O.TABLE_NAME,O.COLUMN_NAME,O.REFERENCED_TABLE_SCHEMA,O.REFERENCED_TABLE_NAME,O.REFERENCED_COLUMN_NAME,O.UPDATE_RULE,O.DELETE_RULE,O.UNIQUE_CONSTRAINT_NAME,T.CONSTRAINT_TYPE from (
select K.CONSTRAINT_SCHEMA,K.CONSTRAINT_NAME,K.TABLE_SCHEMA,K.TABLE_NAME,K.COLUMN_NAME,K.REFERENCED_TABLE_SCHEMA,K.REFERENCED_TABLE_NAME,K.REFERENCED_COLUMN_NAME,R.UPDATE_RULE,R.DELETE_RULE,R.UNIQUE_CONSTRAINT_NAME from information_schema.KEY_COLUMN_USAGE K LEFT join information_schema.REFERENTIAL_CONSTRAINTS R on K.CONSTRAINT_NAME=R.CONSTRAINT_NAME)
 as O inner join Information_schema.TABLE_CONSTRAINTS T on O.Table_Name=T.TABLE_NAME and T.CONSTRAINT_NAME=O.CONSTRAINT_NAME where O.CONSTRAINT_SCHEMA!='mysql' and O.CONSTRAINT_SCHEMA!='sys';

  

#查询数据库中所有表的自增主键:

SELECT
  t.TABLE_NAME,
  c.COLUMN_NAME,
  ts.AUTO_INCREMENT
FROM
  INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
  information_schema.TABLES AS ts,
  information_schema.KEY_COLUMN_USAGE AS c
WHERE
  t.TABLE_NAME = ts.TABLE_NAME
  AND ts.TABLE_NAME  = c.TABLE_NAME
  -- AND t.TABLE_SCHEMA = 数据库名称
  AND t.CONSTRAINT_TYPE = 'PRIMARY KEY'
  ORDER BY ts.`AUTO_INCREMENT` DESC;
# 查询数据库中所有表的主键及数量:
SELECT
  t.TABLE_NAME,
  t.CONSTRAINT_TYPE,
  c.COLUMN_NAME,
  c.ORDINAL_POSITION
FROM
  INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c,
  information_schema.TABLES AS ts
WHERE
  t.TABLE_NAME = c.TABLE_NAME
  AND t.TABLE_SCHEMA = 'sakila'
  AND t.CONSTRAINT_TYPE = 'PRIMARY KEY';

# 查看所有没有主键的表
SELECT
    #CONCAT("truncate table ",table_name,";") 
table_name 
FROM
    information_schema. TABLES
WHERE
    table_schema = 'sakila'
AND TABLE_NAME NOT IN (
    SELECT
        table_name
    FROM
        information_schema.table_constraints t
    JOIN information_schema.key_column_usage k USING (
        constraint_name,
        table_schema,
        table_name
    )
    WHERE
        t.constraint_type = 'PRIMARY KEY'
    AND t.table_schema = 'sakila'
);

--
SELECT column_name FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` 
 WHERE table_name='city' AND constraint_name='PRIMARY'
 AND CONSTRAINT_SCHEMA='sakila';

show keys from `city` where key_name='PRIMARY';

select DISTINCT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where table_name='city' AND COLUMN_KEY='PRI';

SELECT
  t.TABLE_NAME,
  t.CONSTRAINT_TYPE,
  c.COLUMN_NAME,
  c.ORDINAL_POSITION
FROM
  INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
WHERE
  t.TABLE_NAME = c.TABLE_NAME
  AND t.TABLE_SCHEMA = 'sakila'
  AND t.CONSTRAINT_TYPE = 'PRIMARY KEY';


上一篇:Bootstrap图像

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款