sql:MySQL 6.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‘ #存储过程

  

时间: 2024-11-06 07:20:00

sql:MySQL 6.7 表,视图,存储过程结构查询的相关文章

sql 判断 表 视图 存储过程 存在 然后 删除

sql 判断 函数 存储过程是否存在的方法 (2010-12-03 10:08:57) 转载▼     下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助.库是否存在if exists(select * from master..sysdatabases where name=N'库名')print 'exists'elseprint 'not exists'----------------- 判断要创建的表名是否存在if ex

MYSQL批量创建表的存储过程

因为业务需要,创建了100个表,但是这些表的结构都是一样的,作为程序员,就是要解决这种重复劳动.然而这种事情还要单独写个php脚本的话太麻烦了吧,所以就干脆学了一下直接用Mysql存储过程怎么实现: 首先是创建表的:( LPAD(`@i`, 2, '0')的作用 是将1,2,3这些数字转化为'01','02','03') DROP PROCEDURE IF EXISTS `create_tables`; CREATE PROCEDURE create_tables () BEGIN DECLAR

sql通用分页自定义表条件存储过程

create PROCEDURE PrcTestByPage ( @tablename varchar(50), @selectfilter varchar(100), @orderbyfilter varchar(100), @selectpage int, @pageSize int ) AS BEGIN -- 存储过程开始 declare @pkname varchar(100) -- 获取表的主键名称 SELECT @pkname=COLUMN_NAME FROM INFORMATION

第二阶段 MySQL函数库 表的修改与查询

一.表结构的修改(DDL) 1.添加表结构的字段 a.ALTER TABLE 表名 ADD 字段名 数据类型 [字段属性|约束条件] [FIRST|AFTER 字段名]: b. 添加多个字段 ALTER TABLE 表名 ADD 字段名 数据类型 [字段属性|约束条件] [FIRST|AFTER 字段名], ADD 字段名 数据类型 [字段属性|约束条件] [FIRST|AFTER 字段名]...: 2 删除字段 ALTER TABLE 表名 DROP 字段名, DROP 字段名...; 3.修

MySQL的多表联查和嵌套查询

1.数据准备 ### 创建表与插入数据准备 ```python #建表 create table dep2( id int, name varchar(20) ); create table emp2( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int ); #插入数据 insert into dep

mysql搜索多表多字段模糊查询

select parttime_job_business_assessments.*, u.nick_name, u.mobile, pj.name as job_name, b.name as business_name from `parttime_job_business_assessments` left join `users` as `u` on `u`.`id` = `parttime_job_business_assessments`.`user_id` left join `p

MS SQL Server 统计用户表数据量的查询语句

SELECT a.name, b.rows  FROM sysobjects a WITH(NOLOCK) JOIN sysindexes b WITH(NOLOCK) ON b.id = a.id  WHERE a.xtype = 'U ' AND b.indid IN (0, 1)  ORDER By b.rows DESC

mysql中部门表的自我嵌套查询

SELECT t1.id,t1.deptName,t2.id,t2.parentDept,t2.deptName,t3.id,t3.parentDept,t3.deptName,t4.id,t4.parentDept,t4.deptName,t5.id,t5.parentDept,t5.deptName,t6.id,t6.parentDept,t6.deptName,t7.id,t7.parentDept,t7.deptName FROM users_t_department t1LEFT JO

设置Sql server用户对表、视图、存储过程、架构的增删改查权限

根据数据库Schema限制用户对数据库的操作行为 授予Shema dbo下对象的定义权限给某个用户(也就是说该用户可以修改架构dbo下所有表/视图/存储过程/函数的结构) use [Your DB NAME] GRANT VIEW DEFINITION ON SCHEMA :: dbo to [THE USER NAME] 回收某个用户对Shema dbo下对象的定义权限(也就是说该用户不可以修改架构dbo下所有表/视图/存储过程/函数的结构) use [Your DB NAME] DENY V