如何检查MySQL中表字段上是否存在索引?


105

我已经需要Google几次,所以我要分享我的问题。

Answers:


139

SHOW INDEX像这样使用:

SHOW INDEX FROM [tablename]

文件:https//dev.mysql.com/doc/refman/5.0/en/show-index.html


35
my_table哪里显示索引Key_name ='index_to_check';
麻省理工学院

6
最好使用Column_name而不是Key_name,这样,如果索引名称是自动添加而没有名称的,则无需找出索引名称。
Programista 2014年

如何检查多个按键?
疯抢2014年

请小心并使用此语句测试所有语句。尝试与LiquiBase 1.9.5一起使用时,这在我数据库的某些表上失败。也许我的数据库已损坏。也许这是我在工作中一直使用的古老版本的LiquiBase中的一个错误。
史蒂夫·盖尔曼

37

尝试:

SELECT * FROM information_schema.statistics 
  WHERE table_schema = [DATABASE NAME] 
    AND table_name = [TABLE NAME] AND column_name = [COLUMN NAME]

它会告诉您某个列上是否存在任何类型的索引,而无需知道为索引指定的名称。它也将在存储过程中工作(与show index相对)


9
SHOW KEYS FROM  tablename WHERE Key_name='unique key name'

您可以找到表中是否存在唯一键



4

使用以下语句:SHOW INDEX FROM your_table

然后检查字段的结果:row [“ Table”],row [“ Key_name”]

确保正确输入“ Key_name”


1

只是从cli看表布局。你会做

描述表

要么

显示表mytable


0

如果您需要此功能(如果存在列索引)(在这里排在第一位)作为数据库功能,则可以使用/采用此代码。如果要检查索引是否存在,而不管多列索引中的位置如何,只需删除“ AND SEQ_IN_INDEX = 1”部分。

DELIMITER $$
CREATE FUNCTION `fct_check_if_index_for_column_exists_at_first_place`(
    `IN_SCHEMA` VARCHAR(255),
    `IN_TABLE` VARCHAR(255),
    `IN_COLUMN` VARCHAR(255)
)
RETURNS tinyint(4)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Check if index exists at first place in sequence for a given column in a given table in a given schema. Returns -1 if schema does not exist. Returns -2 if table does not exist. Returns -3 if column does not exist. If index exists in first place it returns 1, otherwise 0.'
BEGIN

-- Check if index exists at first place in sequence for a given column in a given table in a given schema. 
-- Returns -1 if schema does not exist. 
-- Returns -2 if table does not exist. 
-- Returns -3 if column does not exist. 
-- If the index exists in first place it returns 1, otherwise 0.
-- Example call: SELECT fct_check_if_index_for_column_exists_at_first_place('schema_name', 'table_name', 'index_name');

-- check if schema exists
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    INFORMATION_SCHEMA.SCHEMATA
WHERE 
    SCHEMA_NAME = IN_SCHEMA
;

IF @COUNT_EXISTS = 0 THEN
    RETURN -1;
END IF;


-- check if table exists
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    INFORMATION_SCHEMA.TABLES
WHERE 
    TABLE_SCHEMA = IN_SCHEMA
AND TABLE_NAME = IN_TABLE
;

IF @COUNT_EXISTS = 0 THEN
    RETURN -2;
END IF;


-- check if column exists
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_SCHEMA = IN_SCHEMA
AND TABLE_NAME = IN_TABLE
AND COLUMN_NAME = IN_COLUMN
;

IF @COUNT_EXISTS = 0 THEN
    RETURN -3;
END IF;

-- check if index exists at first place in sequence
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    information_schema.statistics 
WHERE 
    TABLE_SCHEMA = IN_SCHEMA
AND TABLE_NAME = IN_TABLE AND COLUMN_NAME = IN_COLUMN
AND SEQ_IN_INDEX = 1;


IF @COUNT_EXISTS > 0 THEN
    RETURN 1;
ELSE
    RETURN 0;
END IF;


END$$
DELIMITER ;

-1

您可以使用以下SQL语句检查表上给定的列是否已建立索引

select  a.table_schema, a.table_name, a.column_name, index_name
from    information_schema.columns a
join    information_schema.tables  b on a.table_schema  = b.table_schema and
                                    a.table_name = b.table_name and 
                                    b.table_type = 'BASE TABLE'
left join (
 select     concat(x.name, '/', y.name) full_path_schema, y.name index_name
 FROM   information_schema.INNODB_SYS_TABLES  as x
 JOIN   information_schema.INNODB_SYS_INDEXES as y on x.TABLE_ID = y.TABLE_ID
 WHERE  x.name = 'your_schema'
 and    y.name = 'your_column') d on concat(a.table_schema, '/', a.table_name, '/', a.column_name) = d.full_path_schema
where   a.table_schema = 'your_schema'
and     a.column_name  = 'your_column'
order by a.table_schema, a.table_name;

由于联接是针对INNODB_SYS_ *的,因此匹配索引仅来自INNODB表


-1

试试这个:

SELECT TRUE
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_SCHEMA = "{DB_NAME}" 
AND TABLE_NAME = "{DB_TABLE}"
AND COLUMN_NAME = "{DB_INDEXED_FIELD}";

-3

您无法运行特定的显示索引查询,因为如果索引不存在,它将引发错误。因此,如果要避免任何SQL错误,则必须将所有索引获取到数组中并遍历它们。

这是我的方法。我从表中获取所有索引(在本例中为leads),然后在foreach循环中检查列名(在本例中为province)是否存在。

$this->name = 'province';

$stm = $this->db->prepare('show index from `leads`');
$stm->execute();
$res = $stm->fetchAll();
$index_exists = false;

foreach ($res as $r) {
    if ($r['Column_name'] == $this->name) {
        $index_exists = true;
    }
}

这样,您可以真正缩小索引属性的范围。做print_r$res,为了看看有什么可以一起工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.