I want to dump only view object from mysql databases in the following format :
CREATE OR REPLACE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost` SQL SECURITY DEFINER VIEW
`v_sample` AS
SELECT
`a`.`id` AS `id`,
`a`.`code` AS `active`,
`a`.`title` AS `title`
FROM t_test a;
the script above is the best practive i have ever had... no problem with privilege issue like can not drop the temporary view table, etc
Notes :
I found inside the dump script database, that mysql treat the view object as table first then will be replaced by the real view.
You need to use SHOW CREATE VIEW statement.
SHOW CREATE VIEW v_sample
Related
I'm having a hard time trying to create a view in phpMyAdmin. I have a database named myDB and a table named myTable.
In phpMyAdmin I click on the SQL tab, type in :
SHOW CREATE VIEW myView;
I got this error MySQL said:
#1146 - Table 'myTable.myView' doesn't exist
I don't understand this error message at all, of course it doesn't exist, otherwise why would I want to create it in the first place? And why wouldn't mySQL allow me to create it? How do I create a view?
Thanks
SHOW CREATE VIEW
The syntax you are using is not for creating view in SQL.
it is to show the views you have created in SQL
You need to use the following syntax to create a view
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
ALTER ALGORITHM = UNDEFINED DEFINER=**[YOUR_USERNAME]**#localhost VIEW **[YOUR_VIEW_NAME]** AS [YOUR_VIEW_QUERY];
Change the bold text above, example:
ALTER ALGORITHM = UNDEFINED DEFINER=`dadu_keeve`#`localhost` VIEW `view_banner` AS select `mst_banner`.`banner_uid` AS `banner_uid`,`mst_banner`.`banner_img` AS `banner_img`,`mst_banner`.`banner_alt` AS `banner_alt`,`mst_banner`.`banner_caption` AS `banner_caption`,`mst_banner`.`banner_link` AS `banner_link`,`mst_banner`.`banner_sort` AS `banner_sort`,`mst_banner`.`banner_tipe` AS `banner_tipe`,if((`mst_banner`.`banner_tipe` = 0),'BOX','FULL WIDTH') AS `banner_tipe_desc` from `mst_banner` ;
The way this database is setup (not my design) is that every user has their own database with many tables, so if a user ever decides to leave us we can just give them their DB and they have all of their data. The issue is, when I create a new form or feature I need to create a table in each one of these databases (50+) and it takes a huge amount of my time. Is there any way to create a table in each database?
Run the following query, then execute the output.
If the table already exists in the schema it will still run. However, if the table already exists and you want to redefine it, you can tune this statement to drop those table and recreate them.
SELECT DISTINCT CONCAT('CREATE TABLE IF NOT EXISTS ', TABLE_SCHEMA, '.define table here')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA NOT IN
('mysql', 'information_schema', 'performance_schema');
You could do it programmatically using a programming language like PHP or Python.
Here's the pseudocode that would need to be implemented.
get array of user databases by selecting from information_schema
for each database:
SQL : "create table if not exists <database>.tableName ..."
end for each
I am trying to replicate a specific view on mysql slave while ignoring the base table.
I have created a view that select * from a specific table on a specific DB.
In the slave my.cnf I have restricted the replication to the following:
replicate-do-db=DBNAME
replicate-ignore-table=TABLENAME
When I start the replication on the slave, I get an sql error :
Last_SQL_Error: Error 'TABLENAME 'DBNAME.TABLENAME' doesn't exist' on query. Default database: 'DBNAME'. Query: 'CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost` SQL SECURITY DEFINER VIEW `TABLENAMEVIEW` AS SELECT * FROM TABLENAME'
I am using mysql 5.5.1 and as per the following http://dev.mysql.com/doc/refman/5.5/en/replication-features-views.html a view can be replicated even if the table is ignored.
Any idea how I can solve this ?
Thanks,
Without the base table, the VIEW won't work. You can ignore the base table in replication so that new data doesn't come in but that table must exist for the VIEW to function.
VIEWs in MySQL are nothing more than simple aliases for SELECT statements. They are not materialized, no data is stored within, and thus execute their underlying SELECT each time you SELECT from the VIEW.
I'd like to know if it is possible to create an SQL function that will automatically update my View and add to it new tables in my DB.
My DB consist of multiple tables (same data structure) and are named as follow "MM_DD", now I would like to create a VIEW that joins all this data ( pretty simple , see query below) but I wish to automate the process so every time a new table is added the view will update.
CREATE OR REPLACE VIEW `viewTable` AS
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_06`
union all
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_08`
ect...
What I am doing at the moment is using PHP every time a table is added. It loops through the tables and create / update the view.
select * from information_schema.tables WHERE table_name LIKE '%05%'
Now that I have an array of table names -> create my Query string -> replace view...
Is this possible to do this in SQL?
If so, what is the correct approach?
Write a stored procedure to rebuild your view. You'll need to use what MySQL internally calls "prepared statements" so you can use string manipulation.
You'll still use your SELECT ... FROM information_schema.tables query to drive this stored procedure.
Once you get this working, you can embed it in a mySQL event, and arrange to run it automatically. For example, you could run it at the same time late at night.
Or, you can invoke the stored procedure immediately after you create one of these tables.
I have a view in database B which I use in database A.
I would like to create a synonym to this view. Because right now each time I have to write the query like this
Select * from DBNAME.VIEWNAME
rather I just want to be able to write
SELECT * FROM MYSYNONYMNAME
Is that possible to do in mysql? I didn't see much in the manual..
It's not possible to create synonyms in mysql like it's possible in Oracle
Apparently a VIEW may work as a SYNONYM:
DROP VIEW IF EXISTS `MYSYNONYMNAME` $$
CREATE ALGORITHM=MERGE DEFINER=`root`#`localhost`
SQL SECURITY DEFINER VIEW `MYSYNONYMNAME` AS
SELECT * FROM DBNAME.VIEWNAME $$
Not sure of performance or how far you can get away stacking views within views etc. Also might need to recreate when base table columns change.
See: http://blog.mclaughlinsoftware.com/2013/11/24/mysql-synonym/