MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard)
MySQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | --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 表的注释(备注)信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | -- 创建带解释的表 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' ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | -- 数据库中所有表的主键和外键约束信息的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' ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #查询数据库中所有表的自增主键: 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' ; |
ALTER DATABASE testdb SET RECOVERY FULL;
BACKUP DATABASE testdb TO DISK='c:temp\testdb.bak' WITH INIT;
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
数据库编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2013-07-05 Javascript: hash tables in javascript
2013-07-05 Csharp:asp.net CheckBoxList databind