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
Related
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.
I have two different databases. I have to access data from one database and insert them into another ( with some data processing included, it is not only to copy data ) Also, the schema is really complex and each table has many rows, so copying data into schema in the second database is not an option. I have to do that using MySQL Workbench, so I have to do it using SQL queries. Is there a way to create a connection from one database to another and access its data?
While MySQL Workbench can be used to transfer data between servers (e.g. as part of a migration process) it is not useful when you have to process the data first. Instead you have 2 other options:
Use a dedicated tool you write yourself to do that (as eddwinpaz mentioned).
Use the capabilities of your server. That is, copy the data to the target server, into a temporary table (using dump and restore). Then use queries to modify the data as you need it. Finally copy it to the target table.
I have two databases on two different servers, i wish to insert the records from table A on server2 to table B on server1, table A and B have identifical schema but different names.
how can this be done?
You can try following,
The Easiest approach as per my point is make .sql file from first server db.
Now Open that .sql file in note and find & replace the table name A with table name B.
Now execute that .sql file on the remote server 2.
I prefer to use linked server in this type of scenario.....Check out following url to implement linked server
http://www.sqlmag.com/article/sql-server/querying-tables-and-views-on-a-linked-server
http://www.quackit.com/sql_server/sql_server_2008/tutorial/linked_servers.cfm
http://msdn.microsoft.com/en-us/library/aa213778%28SQL.80%29.aspx
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.
If I have a MySQL database with several tables on a live server, now I would like to migrate this database to another server. Of course, the migration I mean here involves some database tables, for example: add some new columns to several tables, add some new tables etc..
Now, the only method I can think of is to use some php/python(two scripts I know) script, connect two databases, dump the data from the old database, and then write into the new database. However, this method is not efficient at all. For example: in old database, table A has 28 columns; in new database, table A has 29 columns, but the extra column will have default value 0 for all the old rows. My script still needs to dump the data row by row and insert each row into the new database.
Using MySQLDump etc.. won't work. Here is the detail. For example: I have FOUR old databases, I can name them as 'DB_a', 'DB_b', 'DB_c', 'DB_d'. Now the old table A has 28 columns, I want to add each row in table A into the new database with a new column ID 'DB_x' (x to indicate which database it comes from). If I can't differentiate the database ID by the row's content, the only way I can identify them is going through some user input parameters.
Is there any tools or a better method than writing a script yourself? Here, I dont need to worry about multithread writing problems etc.., I mean the old database will be down (not open to public usage etc.., only for upgrade ) for a while.
Thanks!!
I don't entirely understand your situation with the columns (wouldn't it be more sensible to add any new columns after migration?), but one of the arguably fastest methods to copy a database across servers is mysqlhotcopy. It can copy myISAM only and has a number of other requirements, but it's awfully fast because it skips the create dump / import dump step completely.
Generally when you migrate a database to new servers, you don't apply a bunch of schema changes at the same time, for the reasons that you're running into right now.
MySQL has a dump tool called mysqldump that can be used to easily take a snapshot/backup of a database. The snapshot can then be copied to a new server and installed.
You should figure out all the changes that have been done to your "new" database, and write out a script of all the SQL commands needed to "upgrade" the old database to the new version that you're using (e.g. ALTER TABLE a ADD COLUMN x, etc). After you're sure it's working, take a dump of the old one, copy it over, install it, and then apply your change script.
Use mysqldump to dump the data, then echo output.txt > msyql. Now the old data is on the new server. Manipulate as necessary.
Sure there are tools that can help you achieving what you're trying to do. Mysqldump is a premier example of such tools. Just take a glance here:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
What you could do is:
1) You make a dump of the current db, using mysqldump (with the --no-data option) to fetch the schema only
2) You alter the schema you have dumped, adding new columns
3) You create your new schema (mysql < dump.sql - just google for mysql backup restore for more help on the syntax)
4) Dump your data using the mysqldump complete-insert option (see link above)
5) Import your data, using mysql < data.sql
This should do the job for you, good luck!
Adding extra rows can be done on a live database:
ALTER TABLE [table-name] ADD [row-name] MEDIUMINT(8) default 0;
MySql will default all existing rows to the default value.
So here is what I would do:
make a copy of you're old database with MySql dump command.
run the resulting SQL file against you're new database, now you have an exact copy.
write a migration.sql file that will modify you're database with modify table commands and for complex conversions some temporary MySql procedures.
test you're script (when fail, go to (2)).
If all OK, then goto (1) and go live with you're new database.
These are all valid approaches, but I believe you want to write a sql statement that writes other insert statements that support the new columns you have.