MySQL 控制圖是一種用來輔助理解數(shù)據(jù)庫操作的關(guān)系圖。該圖形能夠展示與 MySQL 數(shù)據(jù)庫關(guān)聯(lián)的表、索引、外鍵以及其他相關(guān)信息。MySQL 控制圖能夠讓開發(fā)人員更容易地了解數(shù)據(jù)庫結(jié)構(gòu),幫助他們更好地進行數(shù)據(jù)庫設(shè)計。
示例代碼: mysql>set global event_scheduler = ON; mysql>create event control_graph_table on schedule every 1 day do call create_control_graph_table(); mysql>delimiter // mysql>CREATE PROCEDURE create_control_graph_table() BEGIN DROP TABLE IF EXISTS Control_Graph; CREATE TABLE Control_Graph AS SELECT CONCAT(key_table.TABLE_SCHEMA, '.', key_table.TABLE_NAME) AS `Table`, col.COLUMN_NAME AS `Column`, CONCAT(ref_table.TABLE_SCHEMA, '.', ref_table.TABLE_NAME) AS `References`, ref_col.COLUMN_NAME AS `Ref_Column`, tc.CONSTRAINT_TYPE AS `Constraint_Type` FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS col ON col.CONSTRAINT_NAME = tc.CONSTRAINT_NAME AND col.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ref_col ON ref_col.REFERENCED_CONSTRAINT_NAME = col.CONSTRAINT_NAME AND ref_col.REFERENCED_TABLE_SCHEMA = col.TABLE_SCHEMA AND ref_col.REFERENCED_TABLE_NAME = col.TABLE_NAME AND ref_col.REFERENCED_COLUMN_NAME = col.COLUMN_NAME LEFT JOIN INFORMATION_SCHEMA.TABLES AS key_table ON key_table.TABLE_NAME = col.TABLE_NAME AND key_table.TABLE_SCHEMA = col.TABLE_SCHEMA LEFT JOIN INFORMATION_SCHEMA.TABLES AS ref_table ON ref_table.TABLE_NAME = ref_col.TABLE_NAME AND ref_table.TABLE_SCHEMA = ref_col.TABLE_SCHEMA WHERE tc.CONSTRAINT_TYPE = 'FOREIGN KEY' OR tc.CONSTRAINT_TYPE = 'PRIMARY KEY'; END // DELIMITER ;
在這個示例代碼中,MySQL 控制圖通過使用 Stored Procedure 和 Event Scheduler 來創(chuàng)建。它會自動創(chuàng)造一個名為 Control_Graph 的表格,并在每天執(zhí)行一次。該表格將包含有關(guān)所有 FOREIGN KEY 和 PRIMARY KEY 約束的信息,并將其組合成一個易于理解的圖形格式。
通過以上代碼,我們可以理解 MySQL 控制圖是一個非常有用的工具,它能夠幫助我們更好地理解數(shù)據(jù)庫的結(jié)構(gòu),從而更好地設(shè)計和維護數(shù)據(jù)庫。