查询索引名和字段名相同的表及查找重复索引
来源:互联网 发布:印度历年人口数据 编辑:程序博客网 时间:2024/05/18 02:02
查看statistics结构:
information_schema 02:51:12>desc statistics;
+---------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| NON_UNIQUE | bigint(1) | NO | | 0 | |
| INDEX_SCHEMA | varchar(64) | NO | | | |
| INDEX_NAME | varchar(64) | NO | | | |
| SEQ_IN_INDEX | bigint(2) | NO | | 0 | |
| COLUMN_NAME | varchar(64) | NO | | | |
| COLLATION | varchar(1) | YES | | NULL | |
| CARDINALITY | bigint(21) | YES | | NULL | |
| SUB_PART | bigint(3) | YES | | NULL | |
| PACKED | varchar(10) | YES | | NULL | |
| NULLABLE | varchar(3) | NO | | | |
| INDEX_TYPE | varchar(16) | NO | | | |
| COMMENT | varchar(16) | YES | | NULL | |
| INDEX_COMMENT | varchar(1024) | NO | | | |
+---------------+---------------+------+-----+---------+-------+
16 rows in set (0.00 sec)
查询用例:
select table_schema,table_name,column_name,index_name from information_schema.statistics where
TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema') and table_name='g_a_task'
+---------------+-------------+---------------+------------------------+
| table_schema | table_name | column_name | index_name |
+---------------+-------------+---------------+------------------------+
| m_g_ver | g_a_task | _id | PRIMARY |
| m_g_ver | g_a_task | id | id |
| m_g_ver | g_a_task | task_no | idx_gsat_task_no |
| m_g_ver | g_a_task | upper_task_no | idx_gsat_upper_task_no |
+---------------+-------------+---------------+------------------------+
查看columns结构:
desc columns;
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| COLUMN_NAME | varchar(64) | NO | | | |
| ORDINAL_POSITION | bigint(21) unsigned | NO | | 0 | |
| COLUMN_DEFAULT | longtext | YES | | NULL | |
| IS_NULLABLE | varchar(3) | NO | | | |
| DATA_TYPE | varchar(64) | NO | | | |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_OCTET_LENGTH | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_PRECISION | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_SCALE | bigint(21) unsigned | YES | | NULL | |
| DATETIME_PRECISION | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_SET_NAME | varchar(32) | YES | | NULL | |
| COLLATION_NAME | varchar(32) | YES | | NULL | |
| COLUMN_TYPE | longtext | NO | | NULL | |
| COLUMN_KEY | varchar(3) | NO | | | |
| EXTRA | varchar(27) | NO | | | |
| PRIVILEGES | varchar(80) | NO | | | |
| COLUMN_COMMENT | varchar(1024) | NO | | | |
+--------------------------+---------------------+------+-----+---------+-------+
20 rows in set (0.00 sec)
查询用例:
select table_name, COLUMN_NAME from information_schema.columns where TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema')
and COLUMN_NAME='vamp_no';
+-----------------------+-------------+
| table_name | COLUMN_NAME |
+-----------------------+-------------+
| go_task | vamp_no |
| g_t_y | vamp_no |
| g_a_task | vamp_no |
| gs_ship_task | vamp_no |
+-----------------------+-------------+
查询索引名和字段名相同的表和字段:
方法一:
select table_schema,table_name,column_name,index_name from information_schema.statistics i where
i.TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema') and i.index_name=i.column_name;
方法二:
select c.TABLE_SCHEMA ,c.table_name, c.COLUMN_NAME,i.index_name from information_schema.statistics c ,information_schema.statistics i
where c.TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema')
and c.COLUMN_NAME=i.index_name and c.table_name=i.table_name and c.COLUMN_NAME=i.column_name;
方法三:
select c.TABLE_SCHEMA ,c.table_name, c.COLUMN_NAME,i.index_name from information_schema.columns c ,information_schema.statistics i
where c.TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema')
and c.COLUMN_NAME=i.index_name and c.table_name=i.table_name and c.COLUMN_NAME=i.column_name
验证上面三种方法是否正确:
下面字段名vamp_no和索引名相同
show create table m_g_ver.g_t_y\G
PRIMARY KEY (`_id`),
UNIQUE KEY `id` (`id`), ----字段和索引名相同
KEY `idx_gotr_task_no` (`task_no`),
KEY `vamp_no` (`vamp_no`) ----字段和索引名相同
方法一:
select a.* from (
select table_schema,table_name,column_name,index_name from information_schema.statistics i where
i.TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema') and i.index_name=i.column_name)a
where a.table_name='g_t_y';
+---------------+-----------------------+-------------+------------+
| table_schema | table_name | column_name | index_name |
+---------------+-----------------------+-------------+------------+
| m_g_ver | g_t_y | id | id |
| m_g_ver | g_t_y | vamp_no | vamp_no |
+---------------+-----------------------+-------------+------------+
方法二:
select a.* from (
select c.TABLE_SCHEMA ,c.table_name, c.COLUMN_NAME,i.index_name from information_schema.statistics c ,information_schema.statistics i
where c.TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema')
and c.COLUMN_NAME=i.index_name and c.table_name=i.table_name and c.COLUMN_NAME=i.column_name)a
where a.table_name='g_t_y';
+---------------+-----------------------+-------------+------------+
| table_schema | table_name | column_name | index_name |
+---------------+-----------------------+-------------+------------+
| m_g_ver | g_t_y | id | id |
| m_g_ver | g_t_y | vamp_no | vamp_no |
+---------------+-----------------------+-------------+------------+
方法三:
select a.* from (
select c.TABLE_SCHEMA ,c.table_name, c.COLUMN_NAME,i.index_name from information_schema.columns c ,information_schema.statistics i
where c.TABLE_SCHEMA not in ('mysql','performance_schema','information_schema','common_schema')
and c.COLUMN_NAME=i.index_name and c.table_name=i.table_name and c.COLUMN_NAME=i.column_name)a
where a.table_name='g_t_y';
+---------------+-----------------------+-------------+------------+
| TABLE_SCHEMA | table_name | COLUMN_NAME | index_name |
+---------------+-----------------------+-------------+------------+
| m_g_ver | g_t_y | id | id |
| m_g_ver | g_t_y | vamp_no | vamp_no |
+---------------+-----------------------+-------------+------------+
查找重复索引:
SELECT table_name,column_name,COUNT(1) ct
FROM information_schema.`STATISTICS`
WHERE seq_in_index=1
AND table_schema='m_g_ver'
GROUP BY table_name,column_name
HAVING ct >=2;
+----------------+-------------+----+
| table_name | column_name | ct |
+----------------+-------------+----+
| g_s_so | bking_no | 2 |
+----------------+-------------+----+
1 row in set (0.03 sec)
UNIQUE KEY `unique_booking_no` (`bking_no`) USING BTREE,
KEY `idx_gsss_booking_no` (`bking_no`)
- 查询索引名和字段名相同的表及查找重复索引
- 无效的字段名或字段索引
- SQL Server修改表名,字段名,索引名
- 查询oracle表的信息(表,字段,约束,索引) 按列名+表名查询约束名称
- 查询oracle表的信息(表,字段,约束,索引) 按列名+表名查询约束名称
- 查询oracle表的信息(表,字段,约束,索引)按列名+表名查询约束名称
- 索引名相同报错
- oracle 查询表名和字段名
- 查询在指定的数据库中所有的表名和每个表的字段名及字段类型
- SQLSERVER查询所有数据库名,表名,和字段名
- SQLSERVER查询所有数据库名,表名,和字段名
- 查询表的字段名
- 通过表名和字段名查询字段类型
- sql server查询表的字段名和字段类型
- hibernate本地sql查询,相同列名的字段
- 查找相同名的进程
- 查找重复的索引
- 通过注释查找字段和表名
- 中东学C之路
- Codeforces #375(Div.2)C.Polycarp at the Radio【思维】
- 电子市场总结(二)
- 从尾到头打印链表
- 智慧北京:引导页面动态点的实现
- 查询索引名和字段名相同的表及查找重复索引
- spring mvc的配置
- hdu 5918 KMP
- onreadystatechange()事件
- codeforces 723D. Lakes in Berland [DFS]【】
- 102. Binary Tree Level Order Traversal(层次遍历)
- VMware Fusion 提示磁盘空间不足的解决办法
- openstack学习记要02 虚拟化技术
- HTTP状态码