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;
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.
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.
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.
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'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With