How to create a synonym in mysql - mysql

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/

Related

How to SHOW CREATE TABLE in a particular database in MySQL?

It is possible to get CREATE TABLE statement in specific database without using USE before?
Something like SHOW CREATE TABLE my_table IN my_database; (this doesn't works).
Thanks
show create table database_name.table_name;

MySQL - Automate a view update

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.

MySQL - Trigger or Replication is better?

I want to replicate certain table from one database into another database in the same server. This tables contain exactly the same fields.
I was considering to use MySQL Replication to replicate that table but some people said that it will increase IO so i find another way to create 3 Trigger (Insert, update and Delete) that will perform exactly the same thing like what i expect.
My Question is, which way is better? Is it using MySQL replication is better even though it's in the same server or using Trigger to replicate the data is better.
Thanks.
I don't know what is your goal, but I got mine getting use of the VIEW functionality.
I had two different applications with separate databases but in the same Mysql server. Application2 needed to get a few data from Application1. In general, this is a trivial situation that you can handle with USE DB1; or USE DB2; as your needing, but my programming framework does not work very well with multiple DBs.
So, lets see my solution...
Here is my select query to retrieve this data:
SELECT id, name FROM DB1.customers;
So, using DB2 as default schema, I've created a VIEW:
USE DB2;
CREATE VIEW app1_customers AS SELECT id, name FROM DB1.customers;
Now I can retrieve this data in DB2 as a regular table with a regular SELECT statement.
SELECT * FROM DB2.app1_customers;
Hope ts useful. BR
Assuming you have two databases on the same server i.e DB1 and DB2 and the table is called tbl1 and it is sitting in DB1 you can query the table like this:
USE DB1;
SELECT * FROM tbl1;
USE DB2;
SELECT * FROM DB1.tbl1;
This way you wont need to copy the data and worry about extra space and extra code. You can query a table in another database on the same server. Replication and triggers are not your answer here. You could also create a view to encapsulate the SQL statement.
Definitely triggers is the way to go. Having another server (slave) will need to spare several MB for installation, logs, cpu and memory usage.
I'd use triggers to keep both tables equal. If you want to create a table with the same columns definition and data use:
USE db2;
CREATE TABLE t1 AS SELECT * FROM db1.t1;
After that, go ahead and create the triggers for Update, Insert and Delete statemetns.
Also you could ALTER the new table to a different engine like MEMORY or add indexes to see if you can improve something.

mysql create table like to query?

Is it possible to use the create table query (create table t2 like t1) to get the query instead the actual table? Kind of like duplicating some management consoles Send To editor feature?
I think you're looking for SHOW CREATE TABLE tablename;
'tablename' can be the table within the current database, if you've selected one, or you can qualify it as in 'dbname.tablename'. The output can be used to create the table again. In fact, I sometimes use it in scripts that need to either use an existing table or create it if it doesn't exist. First I'll get the table creation info as shown above, and then I'll use something like"
CREATE TABLE IF NOT EXISTS tablename .........
Where '..........' is all the goodies that SHOW CREATE TABLE tablename gives me.
If you're interested in a good tutorial on MySQL (the database), as well as SQL (the language), you might have a look at the O'Reilly book, Learning MySQL. It's pretty good.

how to "extract" mysql view object?

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