Mysql

MySQL 是否有 information_schema.VIEW_COLUMN_USAGE 或等價物?

  • October 23, 2012

SQL Server 有一個 information_schema 視圖,“VIEW_COLUMN_USAGE”,您可以使用它來確定視圖中的列所在的實際/基本表。

我正在尋找 MySQL 中的等價物——我能看到的唯一與視圖相關的 information_schema 項目是沒有列資訊的 VIEWS。

找到視圖中的列所屬的表來解析視圖定義的唯一方法是什麼?這似乎相當野蠻,如果不控制用於定義視圖的 SQL,這可能是不可能的。

謝謝你的時間!

對不起 - 答案是:不。I_S實際上,您發現的是關於視圖的唯一內容。

這不僅在客戶端是野蠻的,在伺服器端也是如此:當你:

SELECT ... FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_view'

MySQL 實際上深入到引用的表定義,如果有許多嵌套視圖(我的伺服器上有一個特殊情況),這種類型的查詢非常昂貴。

您有機會使用DESCRIBE命令

例如,這是一個名為Contact_Reportusing的視圖SHOW CREATE TABLE

mysql> show create table Contact_Report\G
*************************** 1. row ***************************
               View: Contact_Report
        Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `Contact_Report` AS select concat(`s`.`first`,_latin1' ',`s`.`last`) AS `Salesperson_Name`,`c`.`name` AS `Lead_Provider`,date_format(from_unixtime(`cl`.`date`),_latin1'%Y-%m-%d') AS `Record_Date`,sum((case when (`cl`.`type` = _latin1'appointment') then 1 else 0 end)) AS `Appointment`,sum((case when (`cl`.`type` = _latin1'Appointment Canceled') then 1 else 0 end)) AS `Appointment_Canceled`,sum((case when (`cl`.`type` = _latin1'Bad Lead Note') then 1 else 0 end)) AS `Bad_Lead_Note`,sum((case when (`cl`.`type` = _latin1'call') then 1 else 0 end)) AS `Phone_Call`,sum((case when (`cl`.`type` = _latin1'chat') then 1 else 0 end)) AS `Chat`,sum((case when (`cl`.`type` = _latin1'delay') then 1 else 0 end)) AS `Delay`,sum((case when (`cl`.`type` = _latin1'eBrochure Viewed') then 1 else 0 end)) AS `eBrochure_Viewed`,sum((case when (`cl`.`type` = _latin1'email') then 1 else 0 end)) AS `Email`,sum((case when (`cl`.`type` = _latin1'Lead Ownership Change') then 1 else 0 end)) AS `Lead_Ownership_Change`,sum((case when (`cl`.`type` = _latin1'letter') then 1 else 0 end)) AS `Letter`,sum((case when (`cl`.`type` = _latin1'Lost Lead Note') then 1 else 0 end)) AS `Lost_Lead_Note`,sum((case when (`cl`.`type` = _latin1'note') then 1 else 0 end)) AS `Note`,sum((case when (`cl`.`type` = _latin1'Ownership Change') then 1 else 0 end)) AS `Ownership_Change`,sum((case when (`cl`.`type` = _latin1'Sold Note') then 1 else 0 end)) AS `Sold_Note`,sum((case when (`cl`.`type` = _latin1'viewed') then 1 else 0 end)) AS `Viewed`,sum((case when (`cl`.`testDrive` = _latin1'true') then 1 else 0 end)) AS `Test_Drive` from ((((`D21`.`contactLog` `cl` join `icar`.`sales` `s` on((`s`.`id` = `cl`.`salesID`))) join `icar`.`dealer` `d` on((`d`.`id` = 21))) join `D21`.`leads` `l` on((`l`.`id` = `cl`.`leadID`))) join `icar`.`sources` `c` on((`c`.`id` = `l`.`sourceID`))) group by concat(`s`.`first`,_latin1' ',`s`.`last`),date_format(from_unixtime(`cl`.`date`),_latin1'%Y-%m-%d')
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

那太可怕了。現在,讓我們嘗試DESCRIBE

mysql> desc Contact_Report;
+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Salesperson_Name      | varchar(151)  | NO   |     |         |       |
| Lead_Provider         | varchar(255)  | NO   |     |         |       |
| Record_Date           | varchar(10)   | YES  |     | NULL    |       |
| Appointment           | decimal(23,0) | YES  |     | NULL    |       |
| Appointment_Canceled  | decimal(23,0) | YES  |     | NULL    |       |
| Bad_Lead_Note         | decimal(23,0) | YES  |     | NULL    |       |
| Phone_Call            | decimal(23,0) | YES  |     | NULL    |       |
| Chat                  | decimal(23,0) | YES  |     | NULL    |       |
| Delay                 | decimal(23,0) | YES  |     | NULL    |       |
| eBrochure_Viewed      | decimal(23,0) | YES  |     | NULL    |       |
| Email                 | decimal(23,0) | YES  |     | NULL    |       |
| Lead_Ownership_Change | decimal(23,0) | YES  |     | NULL    |       |
| Letter                | decimal(23,0) | YES  |     | NULL    |       |
| Lost_Lead_Note        | decimal(23,0) | YES  |     | NULL    |       |
| Note                  | decimal(23,0) | YES  |     | NULL    |       |
| Ownership_Change      | decimal(23,0) | YES  |     | NULL    |       |
| Sold_Note             | decimal(23,0) | YES  |     | NULL    |       |
| Viewed                | decimal(23,0) | YES  |     | NULL    |       |
| Test_Drive            | decimal(23,0) | YES  |     | NULL    |       |
+-----------------------+---------------+------+-----+---------+-------+
19 rows in set (0.01 sec)

mysql>

好多了 !!!

引用自:https://dba.stackexchange.com/questions/27422