Mysqldump, exclude data from tables by query - mysql

It's possible to ignore tables in mysqldump using:
mysqldump -u username -p database --ignore-table=database.table1 > database.sql
Is it possible to ignore certain records in tables while dumping?

Related

Can I delete all data in Database MySQL without deleting tables or relationships?

I am trying to empty the database of any data, while keeping the relationships and tables as they are
I have no idea if my thinking is right or wrong
Yes, you just run mysqldump with --no-data
mysqldump --no-data -u someuser -p mydatabase
You can save it to a .sql file and then drop your database
Then you restore it from the dump
truncate table_name;
table_name is the table you want to delete all data in it.
truncate only works on tables, so you need to execute truncate table one by one.
Use truncate to all table :
mysqldump -d -uuser -ppass --add-drop-table databasename > databasename.sql
mysql -uuser -ppass databasename < databasename.sql
or you can read this, similar problem.

Is there a way to import all databases into one database with Mysqldump?

I want to gather automatically all my databases into one with Mysqldump, is there a way to do it ?
For example what I want is to move all the tables from DB1, DB2 and DB3 into DB4 (DB4 can already contain some tables or can be created during the import, it doesn't matter to me).
I tried mysqldump -uroot -p --all-databases > dump.sql
Then import it with mysql -uroot -p allInOne < dump.sql
But the resulting database is only filled with it's own data.
I think you will have to do the databases individually but with:
mysqldump -uroot -p DB1 > dump.sql
This will not include the Database name and Use in the dump so you can re-import into the new database.
If you really have a lot of databases then not sure if any of the other options will help :https://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
Might be worth exploring:
mysqldump -uroot -p --all-databases --tables > dump.sql

how to take backup of two tables in the mysql database?

I am aware of mysqldump utility, as it takes backup of entire database. I need to take backup of two tables in mysql database, in which one table with all entries and second one without entries. and also i need both tables in a single sql(i.e mydb.sql) file.
is it possible ?
Mysqldump can also dump single tables, optionally with or without data:
mysqldump [options] db_name [tbl_name ...]
--no-data, -d: Do not write any table row information (that is, do not dump table contents).
So to dump table1 with all entries, and table2 without entries, you would invoke mysqldump twice like this:
mysqldump db_name table1 > table1.sql
mysqldump --no-data db_name table2 > table2.sql
UPDATE: To dump both tables into a single file, you can append the output of the second command to the first:
mysqldump db_name table1 > dump.sql
mysqldump --no-data db_name table2 >> dump.sql

Mysql - How to clear all data from all table in single database

I have database called Database1 and this DB have 40 tables. Now i want to delete all data from that 40 tables.I know that to display all tables from DB using
SELECT table_name
FROM INFORMATION_SCHEMA.tables
WHERE table_schema = 'Database1';
So how to delete all data from all tables form Database1 using single query?
Note :
I should delete only data, not tables.
I am using mysql workbench 6.0
You can try this:
mysqldump -d -uuser -ppass --add-drop-table yourdatabasename > yourdatabasename.sql
mysql -uuser -ppass yourdatabasename < yourdatabasename.sql
As pointed correctly by Zafar, if you want to include stored procedure/function also then you can include -R option.
Or you can try like
mysql -Nse 'show tables' yourdatabasename | while read table; do mysql -e "truncate table $yourtable" yourdatabasename; done
You can execute below command on server console.
mysql -uroot -p<pass> -Nse 'show tables' database1 | while read table; do mysql -uroot -p<pass> database1 -e "truncate table $table"; done
You can also do it by any gui like sqlyog by below steps-
right click on database1 > choose more database options > truncate database
Third option is by structure backup and restore as per below-
mysqldump -R -d -uroot -proot123 database1 | mysql -uroot -proot123 database1
Note: Always use -R if you are using stored procedures/function other wise you loose it.
enjoy...
For Oracle :
if you want to delete all the records of all the tables without drop the tables you should have a look, it may help you
https://dba.stackexchange.com/questions/74519/delete-all-the-data-from-all-tables
you can try any of following :
Delete data from all tables in MYSQL
How to empty all rows from all tables in mysql (in sql)

Select MySQL Tables with less than 100k Rows

I'd like to be able to make a backup of tables with less than 100K rows. What I'm trying to do is clone a development database to my local machine that has many log tables that I don't need the data for, and tables with "legitimate" content.
So I'm going to have one dump that just copies the structures of the tables, and another, that copies the relevant data from these tables with less than 100k rows.
If I have to use an intermediary language like Python or PHP, I'm fine with that.
edit: So the question is, how do I create a mysql dump of data from tables with less than 100k rows?
USe something like this
mysql databasename -u [root] -p[password] —disable-column-names -e
'select table_name from information_schema.tables where table_rows < 100000;'
| xargs mysqldump [databasename] -u [root] -p[password] > [target_file]
p.s. all this will need to be in a single line
To dump only the schema
mysqldump --user=dbuser --password --no-data --tab=/tmp dbname
or try to export schema and data seperately for each table with below command
mysqldump --user=dbuser --password --tab=/tmp dbname
Or
mysqldump --opt --where="1 limit 100000" database > fileName.sql
that would give you the 100K rows from every table.
To Ignore some tables
mysqldump --opt --where="1 limit 100000" --ignore-table=database.table1
--ignore-table=database.table2 database > fileName.sql