如何在mysql中查找所有无效视图?

时间:2014-09-18 12:18:13

标签: mysql

因此,我们设法为创建无效视图的数据库做了有趣的事情。我们只想从数据库中删除这些视图并继续。 我找不到的是一种简单的方法来查找数据库中的所有无效视图,以便我可以在那里工作。是否有捷径可寻?

配方创建无效视图

create table some_table (some_column varchar(20));
insert into some_table(some_column) values('some_data');
create view some_view as (select some_column from some_table);
select * from some_view;

# Now drop the table and test the view
drop table some_table;
select * from some_view;

4 个答案:

答案 0 :(得分:1)

在MySQL数据库中查找损坏视图的更好方法:

SELECT vws.table_schema,vws.table_name 
FROM (
    SELECT * 
    FROM  information_schema.tables 
    WHERE table_type='VIEW' 
        AND table_comment LIKE '%invalid%'
  ) vws;

Original source of query

答案 1 :(得分:0)

 SELECT TABLE_NAME 
 FROM information_schema.VIEWS
 WHERE TABLE_NAME NOT IN (
      SELECT TABLE_NAME
      FROM information_schema.TABLES
 )

答案 2 :(得分:0)

根据以某种方式删除的答案。

SELECT CONCAT('CHECK TABLE ', table_name, ';') AS my_view_check_statements
FROM information_schema.views 
WHERE table_schema = 'your_database_name'
INTO OUTFILE '/tmp/chkstmts.sql';

source '/tmp/chkstmts.sql';

答案 3 :(得分:0)

Ralph的解决方案工作正常。如果您希望查询没有子选择,请尝试以下操作:

SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'view'
  AND table_rows is null
  AND table_comment like '%invalid%'

条件 table_rows为空很重要,因为它将强制评估视图和 table_comment 列中的错误消息。

如果以后要修复视图,则可以使用来查看原始定义

SELECT view_definition
FROM information_schema.views
WHERE table_schema = 'your_database'
  AND table_name = 'your_view'
相关问题