I have a view in my production replica (it's a read-only database) and I need to copy one of the views to my data warehouse.
What would be the best way to go about it?
I can not create a table from the view on the replica. Meaning I can not do CREATE TABLE Table_From_View AS SELECT * FROM My_View;
I can not copy all the tables that create the view definition. Some of them contain sensitive data.
I am sure I missing something basic here...
Ideas?
In MySQL, a view doesn't store anything. It's more like an alias or a macro. It just passes the query logic through to underlying base tables.
You need the underlying base tables to exist on the same MySQL instance where you create the view, and the base tables must remain existing on that same MySQL instance while you query the view.
If you only want to copy the contents of a view, so you don't copy the other sensitive data that is not selected by the view, you would have to create a base table to copy the data:
mysql> CREATE TABLE myview_base AS SELECT * FROM myview;
Then you can do a logical dump of that copy table:
shell > mysqldump --single-transaction mydatabase myview_base > myview_base.sql
Then restore that dump file to your data warehouse as you would any other SQL dump file.
Another possible strategy:
SELECT * FROM myview INTO OUTFILE 'filename.csv';
This dumps the result of an SQL query to a file. See https://dev.mysql.com/doc/refman/8.0/en/select-into.html
The file will be created on the database server, so if you don't have shell access to the server, you won't be able to retrieve the file.
Related
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.
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?
Mysql 8.X
Database name: db1.
In this database, I create a view:
CREATE VIEW vw1 AS
SELECT * FROM sometable;
When I check the source code of the view, instead of the code above, I can see:
SELECT * FROM db1.sometable;
I.e. MySQL engine automatically adds a database name prefix to every table I refer to in the view.
Now, I need to rename my database from db1 to db2. There is no built-in database rename functionality in MySQL. I have to take a backup, then drop the original database, then restore the backup under a new name.
Result: the vw1 view in my new db2 database tries to select rows from (now non-existing) db1 database, causing errors.
Now imagine 100s of databases, with 100s or 1000s of tables each. This issue makes things absolutely non-manageable.
Is there any way of stopping MySQL from adding database prefixes in view definitions?
I want to take backup of single table on same server but in different backup database, backup table name should like "tablename_currentdate+7". Also I want to check if backup table is present in backup database or not. If yes I want to drop that table.
Please suggest how to perform this task in SSIS package.
Establish a connection to both databases
I would suggest creating a table in the backup database and always use it. something like tablename_BU
Exec SQL Object to TRUNCATE the table
put in a data flow
connect to source and select * from TABLE
Put in a destination and map.
VIOLA
I would like to select data from a second MySQL database in order to migrate data from one server to another.
I'm looking for syntax like
SELECT * FROM username:password#serverip.databaseName.tableName
Is this possible? I would be able to do this in Microsoft SQL Server using linked servers, so I'm assuming it's possible in MySQL as well.
You can create a table using FEDERATED storage engine:
CREATE TABLE tableName (id INT NOT NULL, …)
ENGINE=FEDERATED
CONNECTION='mysql://username:password#serverip/databaseName/tableName'
SELECT *
FROM tableName
Basically, it will serve as a view over the remote tableName.
There are generally two approaches you can take, although neither of them sound like what you're after:
Use replication and set up a master/slave relationship between the two databases.
Simply dump the data (using the command line mysqldump tool) from the 1st database and import it into the 2nd.
However, both of these will ultimately migrate all of the data (i.e.: not a subset), although you can specify specific table(s) via mysqldump. Additionally, if you use the mysqldump approach and you're not using InnoDB you'll need to ensure that the source database isn't in use (i.e.: has integrity) when the dump is created.
You can't do this directly, but as someone else alluded to in a comment, you can use mysqldump to export the contents of a table as a SQL script.
At that point you could run the script on the new server to create the table, or if more manipulation of the data is required, import that data into a table with a different name on the new server, then write a query to copy the data from there.