2008-09-24 51 views

回答

1

單看從CLI一表的佈局。你會做

遞減MYTABLE

節目表MYTABLE

32

嘗試:

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

它會告訴你我f在某一列上有任何類型的索引,而不需要知道索引的名稱。它也將在存儲過程中工作(而不是顯示指數)

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

你可以找到,如果有存在於表中的唯一鍵

6
show index from table_name where Column_name='column_name'; 
-2

無法運行特定的節目索引查詢,因爲如果索引不存在,它將引發錯誤。因此,如果您想避免任何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爲了看看你可以使用什麼。

-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表

0
來到

使用以下語句:SHOW指數從your_table

,然後檢查該字段的結果:行[ 「表」],行[ 「KEY_NAME」]

確保您正確書寫「Key_name」