views:

34

answers:

1

A few months ago I created a VIEW that gives me last month's transactions. I use that view within other queries. The problem is, I changed some table names of tables that are referenced inside the VIEW and have broken the VIEW query in the process. Now my other queries don't execute anymore.

Is there any way to see what the VIEW statement is and edit it to reflect the new table names? I'm using phpmyadmin, but everytime I try to go to the view table, it gives me an error saying it is invalid, but it won't show me what it is...

+3  A: 

Providing you know the name of the view you can run this and it'll show you the query that creates the view. If phpMyAdmin doesn't let you run it, then you will probably have to do it at the command line.

SHOW CREATE VIEW view_name

You should also that it says something like:

SQL SECURITY DEFINER VIEW `myViewName` AS

Directly after that you will find the query that creates this view. Copy and paste it into a text editor and you can alter it so it works with your new table names.

Run it as a normal query to ensure it works, then when you're ready to replace it with the broken original you can run this query (beware this will delete the original view!):

DROP VIEW IF EXISTS myViewName;

CREATE VIEW myViewName AS ...(your new query here)...

Hope that helps!

References:

  1. http://dev.mysql.com/doc/refman/5.1/en/show-create-view.html
Phillykins
Thanks. This gives me the following: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localho...Not sure how to interpret that.
just skip this part from `algorithm ` to `security definer`. `CREATE VIEW view_name AS SELECT` is what you need, especially after `SELECT` part.
dev-null-dweller
I've added some more detail which shows you how to drop the original view, recreate it with the new query.
Phillykins