如何检查MySQL中的表字段上是否存在索引?
我需要用谷歌搜索多次,所以我分享了我的问答。
如何检查MySQL中的表字段上是否存在索引?
我需要用谷歌搜索多次,所以我分享了我的问答。
使用SHOW INDEX
,如下所示:
SHOW INDEX FROM [tablename]
尝试:
SELECT * FROM information_schema.statistics
WHERE table_schema = [DATABASE NAME]
AND table_name = [TABLE NAME] AND column_name = [COLUMN NAME]
它会告诉你某一列上是否有任何类型的索引,而不需要知道索引的名称。它也将在存储过程中工作(与显示索引相反)
show index from table_name where Column_name= column_name ;
SHOW KEYS FROM tablename WHERE Key_name= unique key name
将显示表中是否存在唯一键。
使用以下语句:
SHOW INDEX FROM *your_table*
然后检查字段的结果:row[“Table”]
,row[“Key_name”]
确保正确写入“Key_name”
要从CLI查看表的布局,您可以使用
desc mytable
或
show table mytable
补充GK10的建议:
使用以下语句:SHOW INDEX FROM your_table
And then check the result for the fields: row["Table"], row["Key_name"]
确保正确写入“Key_name”
可以将其应用到PHP(或其他语言)中,并将其封装在sql语句中,以查找索引列。基本上,您可以将SHOW INDEX FROM mytable的结果拉入PHP,然后使用column_name列来获取索引列。
制作数据库连接字符串并执行以下操作:
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
$sql = "SHOW INDEX FROM mydatabase.mytable WHERE Key_name = PRIMARY ;" ;
$result = mysqli_query($mysqli, $sql);
while ($row = $result->fetch_assoc()) {
echo $rowVerbatimsSet["Column_name"];
}
尝试使用此选项:
SELECT TRUE
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = "{DB_NAME}"
AND TABLE_NAME = "{DB_TABLE}"
AND COLUMN_NAME = "{DB_INDEXED_FIELD}";
您可以使用以下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表。
If you need to check if a index for a column exists as a database function, you can use/adopt this code.
If you want to check if an index exists at all regardless of the position in a multi-column-index, then just delete the part 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 ;