Stored procedure to get table data from different database - mysql

Can we get table data from multiple database using stored procedure?

Yes you can. You need to access table using database name.
e.g.
Select * From myDb1.Table1
Select * From myDb2.Table2
Make sure you have access to both the database or both the databases are linked if they are on different servers

Related

Getting SHOW CREATE TABLE from a view, as if it were a table

I have access to a remote database, and I would like to dump the schema and data of several views onto my local machine and load this into my local database as tables in a quick and easy way.
I lack the user privileges to run CREATE TABLE AS (SELECT * FROM target_view), otherwise this would be trivial to solve. In other words, I want to retrieve and recreate the "composite" schema of target_view as if it were a table.
I do not want the output of SHOW CREATE VIEW, as this only shows a complex SELECT statement with joins to various tables on remote I have limited ability to access. And a problem I'm seeing in MySQL 8.x is when I run SHOW CREATE TABLE on the view, this command simply acts as an alias of SHOW CREATE VIEW (which is reasonable).
Frustratingly, I can run DESCRIBE and see the schema of these views as they were tables. I really just need to convert this information into a CREATE TABLE statement without actually being able to run CREATE TABLE.
In case it weren't obvious, the key is to avoid manual reconstruction of these views' tabular schemas (as they may change in the future). I also want to avoid the solution of reverse engineering a generic table construction of 20-30 generic VARCHAR or TEXT columns from a CSV dump.
I don't know of any way to display the metadata of a result set in CREATE TABLE syntax.
What I would do given your circumstance is first create on your local MySQL instance the base table and the view, then you can use the CREATE TABLE AS SELECT ... syntax to produce a concrete table to match the metadata of the view result set.

How can I copy a table from one database to another database along with its data?

I have two separate databases installed on the same server. I name the representation as db1 and db2. I have a table named sample_table in my db1 database. I am trying to copy this table with its records to my db2 database with the following queries:
CREATE TABLE db2.sample_table LIKE db1.sample_table
insert into db2.sample_table select * from db1.sample_table
As a result of my research, I found that this method works in most places. However, when I type and run my query from the db2 console, I am having a user permission problem for db1. The query might be running, but should there be a configuration link between these two databases? If so, how can I get this link? What are the alternative solution methods I can overcome this problem?

Laravel 4 - copy a table from one databse to another

I am using Laravel 4 to build a site that uses a large number of mysql databases, where each database has multiple tables. The structure and organization of the databases is not within my control.
I need to be able to replicate a table from one database in another database at run time (the source database and destination database (and the specific table within the source database are dependent on choices made by the user).
Duplicating a table is easy to do with mysql:
CREATE TABLE database2.new_table LIKE database1.original_table;
INSERT INTO database2.new_table SELECT * FROM database1.original_table;
but I cannot figure out how to do it with Laravel.
I can easily access each database by creating their own connections ('mysql1' and 'mysql2') but I can't figure out how to construct the statement to use both. The following doesn't work
$success = DB::connection('mysql2')->statement('CREATE TABLE new_table LIKE database1.original_table);
because I am trying to access database1 directly without using the 'mysql1' connection, and Laravel generates an error saying that database1.original_table doesn't exist.
I feel like the solution should be obvious but don't have enough experience with Laravel to figure it out. Any guidance would be greatly appreciated.

use mysql functions for multiple databases with same pattern name

Can I import Mysql function and use it for multiple database with same pattern name without import that function any more ?
I know Mysql save function in two table :
information_schema.ROUTINES and mysql.proc
Does anyone have an idea to do this?
Every stored procedure is associated with its schema or database.
So long as the SPs are just routines but not depended on table data, one can happily call them from anywhere.
And if the SPs are intended to calculate based on table data, then you definitely need that database qualifier while defining the SP body.
I.e. instead of calling
select count(*) from routines;,
you require to call
select count(*) from information_schema.routines;.
If you look into the table structure of both routines and proc you can find a column routine_schema and db respectively which point to target database of the SPs.
Unless you define SP bodies, which are table data related, in this way
you definitely have to redefine them in your database environment.
More important thing is that, you again require privileges to access and execute such cross database SPs.

How to accomplish "MySQL cross database reference" with PostgreSQL

We will migrate the database from mysql to postgresql in our product(through java). So we need to change the mysql query to postgresql query in java application. How to create the table i.e., databasename.tablename in postgresql.
For mysql, we can directly create the table e.g create table information.employee.
Here database name is "information" and table name is "employee" . Is it possible to achieve same query in postgresql.
I searched google it says cross database reference is not possible. Please help me.
I saw pg_class table it contains the table names in the specific database, like wise databse and tables relationships are stored in any other table.
This is normally done using schemas rather than databases, which is more or less like how MySQL organizes it anyway.
Instead of
create database xyz
use
create schema xyz
When you create tables, create them:
create table xyz.myTable
you will need to update your search path to see them on the psql command line tool, or if you want to query them without using the schema explicitly. The default schema is public, so when you create a table without a schema name, it ends up in public. If you modify your search_path as below, the default schema becomes the first in the list: xyz.
set search_path=xyz,public,pg_catalog;
and you must not have spaces in that statement. You can do it globally for a user/role too:
alter role webuser set search_path=xyz,public,pg_catalog;
Also, don't forget that postgresql string matches are case sensitive by default (this one catches people out a lot).
If you want to have different physical locations for the files for each schema, you can do that with tablespaces. If you have a look at the postgresql documentation page, they have info on how to do it, it's pretty easy.
database in MySQL == schema in PostgreSQL. So you will most probably want to migrate all your mysql dbs into one postgres db. Then you will be able to do "cross-database" queries.
See my answer to this question: Relationship between catalog, schema, user, and database instance