Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all invalid views in mysql?

Tags:

mysql

So, we managed to do interesting things to our database that created invalid views. We just want to drop these views from the database and move on. What I could not find is an easy way to find all invalid views in the database so that I can work from there. Is there an easy way to do this?

Recipe to create an invalid view

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;
like image 928
Vinay Venu Avatar asked Sep 18 '14 12:09

Vinay Venu


People also ask

How can I see all views in MySQL?

To get a list of MySQL views, we can use the SELECT command with LIKE operator. Let us see the syntax first. mysql> SELECT TABLE_SCHEMA, TABLE_NAME -> FROM information_schema.

How do I find an invalid object?

Invalid objects can be identified by querying *_OBJECTS with a filter on status = INVALID. Error details can be queried from *_ERRORS. Invalid objects get automatically recompiled when they are accessed or executed.

Can you query a view in MySQL?

MySQL supports views, including updatable views. Views are stored queries that when invoked produce a result set. A view acts as a virtual table. The following discussion describes the syntax for creating and dropping views, and shows some examples of how to use them.


2 Answers

The solution from Ralph works fine. If you want a query without subselect, try this:

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

The condition table_rows is null is important as it will force an evaluation of the view and the error message in the table_comment column.

If afterwards you want to fix your view, you can see the original definition with

SELECT view_definition
FROM information_schema.views
WHERE table_schema = 'your_database'
  AND table_name = 'your_view'
like image 158
Theo Avatar answered Sep 21 '22 17:09

Theo


A better way of finding broken views within your MySQL database:

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

like image 24
Ralph Avatar answered Sep 21 '22 17:09

Ralph