I broke my Mysql View query. How do I fix it? - mysql

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...

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:
http://dev.mysql.com/doc/refman/5.1/en/show-create-view.html

Related

QTableView does not update after ALTER TABLE query using PySide6

I am using the QSqlTableModel and the QTableView. I want to be able to add and drop columns via the menubar in the GUI. When I trigger the SQL query, the db is altered as expected but the view won't update. I have to close and rerun the application to see the altered version of the db.
Here are the methods that are called when clicking on the respective entries in the menu:
def add_db_column(self):
query = QSqlQuery()
query.exec("ALTER TABLE Names ADD NewColumn varchar(255)")
query.finish()
def drop_column(self):
query = QSqlQuery()
query.exec("ALTER TABLE Names DROP NewColumn")
query.finish
I tried to include self.update(), self.model.layoutChanged.emit() and self.select() after the query in those methods, but the view just won't update.
What am I doing wrong here?

What does generated by server on drop tables mean [duplicate]

Under the structure tab, when EXPORTing a database using phpmyadmin there is a check box labeled:
Add DROP TABLE / VIEW / PROCEDURE / FUNCTION
What does this do?
When creating a table, view, procedure, or function, it will add a DROP statement before it. The result of this is that even if the item exists, it will still be created.
For example: If you have a table called users and you run the export script without the DROP checkbox, it will attempt to create the users table again but will fail since it already exists. If you check it, it will drop the table before it is created (if it exists) to ensure the creation will always be successful.
Of course this can be dangerous if you have data in the table that you don't want to lose.
For example: If you have a table called users and you run the export script without the DROP checkbox, it will attempt to create the users table again but will fail since it already exists. If you check it, it will drop the table before it is created (if it exists) to ensure the creation will always be successful.
I was confused as to what this statement meant exactly, so I did additional research on the topic and wanted to leave an elaborated explanation here for future reference.
The create and drop actions in the above quote are simply instructions for when you import the file you have already exported. I was initially under the impression that these actions were happening as I was exporting. This is not the case. It's simply instructions for when you import your exported file.

Select Into/Insert Into SQL Server query duplicates

Sorry for asking this question, but I am a beginner in SQL, my colleague at work build a view, which I need as datasource for a report, however since this view is based on several other views it takes like 45 minutes to execute the query. This is way to long. Therefore I created a table from that view, initial execution time is the same, but once in place it executes in seconds.
In Microsoft SQL Server 2014 I used the following query:
select *
into [dbo].[MAT_v_demnew_daily_am_all_data]
from [dbo].[v_demnew_daily_am]
This works fine, but since the view is updated daily I also need to refresh the table everyday. When I now execute the above mentioned query I get the message that the table already exists.
That's why I tried to use 'insert' in this case:
insert into [dbo].[MAT_v_demnew_daily_am_all_data]
select *
from [dbo].[v_demnew_daily_am]
Here I have the problem that it not only inserts the additional data but also the already existing data, so in the end I have duplicates.
As a workaround I now manually delete the [dbo].MAT_v_demnew_daily_am_all_data] table and then execute the select * into query.
Now I am looking for an easier solution, is it possible to having the table deleted by query and in the same query create a new one by select * into or is it possible to only insert new data from the view to the table so that I don't get duplicates.
Moreover, is it possible to have such SQL statement being executed automatically on a daily basis, maybe by .bat file and windows task scheduler?
I know that the source of all problems is the View and that we should improve that, but looking for a short term solution first.
Thanks so much.
Mathias
Try this:
IF OBJECT_ID('dbo.MAT_v_demnew_daily_am_all_data', 'U') IS NOT NULL
DROP TABLE dbo.MAT_v_demnew_daily_am_all_data
SELECT INTO dbo.MAT_v_demnew_daily_am_all_data FROM dbo.v_demnew_daily_am
This query is reusable on a daily basis.
You can create one stored procedure including this query.
Then you only need to execute the stored procedure.
Updated
Before you create the stored procedure, please check if you have the permission.
Then try:
create procedure [procedure_name]
as
IF OBJECT_ID('dbo.MAT_v_demnew_daily_am_all_data', 'U') IS NOT NULL
DROP TABLE dbo.MAT_v_demnew_daily_am_all_data
SELECT INTO dbo.MAT_v_demnew_daily_am_all_data FROM dbo.v_demnew_daily_am;
After you create it:
EXEC [procedure_name];

Unable to refresh a local table created from linked table

I am new to access database.
I created a linked table (linked to an excel file)
I them created a local table which is just a filtered table from the linked table. (same table just filtered for some records.)
The issue I am running into is that I am not able to refresh this local filtered table. The steps I am following are :
Refresh linked table using 'linked table manager'
Refresh the local table (filtered version of linked table) using linked table manager and the refresh button in the menu bar.
While my linked table gets refreshed, this filtered table does not get refreshed.
Can someone suggest what I can do?
Thanks in advance
You might want to use a query rather then your local, filtered, table.
If you want to keep your current structure try this
Create -> Query Design -> click on SQL on the bottom right -> paste the following code in
SELECT * INTO local_filtered_table
FROM linked_table
WHERE <put your conditions here>;
Then run this query each time you update your linked table.
Or you can do as Rene suggested and just make a query instead of a local table which would look something like this.
SELECT *
FROM linked_table
WHERE <put your conditions here>;

How can I make a MYSQL view update after a trigger causes an insert on table used in view

I have a MYSQL table that I insert into based on a trigger and I have a view that uses this table (and joins on others). When my trigger fires and inserts a new row it shows up fine in the table but the view does not update. Is there a way I can get the view to update or is this a limitation of triggers?
Note: regular inserts into the table update the view just fine, only inserts from a trigger are missing from the view.
Thanks in advance.
Turns out the issue was with the joins I used to make the view - I had inner joins on fields that were only NULL during inserts from the trigger. I switched them to left joins and now the view updates fine regardless of whether the insert is from a trigger or not.
Also, #PM-77-1's answer is correct but in this case I was not altering any columns, just inserting new rows of data.
From the docs (with my emphasis):
The view definition is “frozen” at creation time, so changes to the
underlying tables afterward do not affect the view definition. For
example, if a view is defined as SELECT * on a table, new columns
added to the table later do not become part of the view.
So your trigger code should include ALTER VIEW statement (if you are absolutely certain that the view exists) or CREATE OR REPLACE (if you are not).
For additional information on MySQL views (not related to the question at hand) see Restrictions on Views.