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)
Related
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.
I need to delete the data from all tables but one table in my database. Let's assumme the database is called my_database and the table in which data should be preserved is called my_important_table, so is there any way to achieve this?
I was able to figure out this problem thanks to these questions:
Truncate all tables in a MySQL database in one command? (check most voted answer)
mysql: What is the right syntax for NOT LIKE? (check validated answer)
The following command worked properly for me:
mysql -u root -p -Nse "SHOW TABLES WHERE \`Tables_in_my_database\` != 'my_important_table'" my_database | while read table; do echo "SET FOREIGN_KEY_CHECKS = 0; truncate table $table;"; done | mysql -u root -p my_database
The following command is the same as the previous one, but I split it into multiple lines to improve visualization.
mysql -u root -p -Nse "SHOW TABLES WHERE \`Tables_in_my_database\` != 'my_important_table'" my_database | \
while read table; do echo "SET FOREIGN_KEY_CHECKS = 0; truncate table $table;"; done | \
mysql -u root -p my_database
I have 2 databases, both called dataweb (these are databases of 2 different sites) I need to put the new tables I have made on site 1, into the database of site 2, without copying the data from site 1, or deleting the data from site 2, any ideas?
MySQL client version: 5.0.51a
It can be done with bash script. E.g. using the next scenario
mysql -hdb1host.com -uroot -pxxxxxx -e "use db1; show tables" >
file1.txt
mysql -hdb2host.com -uroot -pxxxxxx -e "use db2; show tables" >
file2.txt
then compare these two files, e.g. with diff command
http://www.computerhope.com/unix/udiff.htm
take diff output and stream it to files (tables_from_diff1.txt,
tables_from_diff2.txt).
Then make dump for these tables
mysqldump -hdb1host.com -uroot
-pxxxxxx db1 [tables from tables_from_diff1.txt] > db1_dump.txt
mysqldump -hdb2host.com -uroot -pxxxxxx db2 [tables from
tables_from_diff2.txt] > db2_dump.txt
Apply datadumps to necessary dbs.
mysql -hdb1host.com -uroot -pxxxxxx db1 < db2_dump.txt
mysql -hdb1host.com -uroot -pxxxxxx db2 < db1_dump.txt
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
Is there a SQL command that 'resets' the database in MySQL?
By reset I mean, all rows are deleted and auto increment are reset.
You're looking for TRUNCATE, as in TRUNCATE TABLE mystuff
More info: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
mysqldump -uuser -hhost -p --no-data name_of_database > backup_file_name.sql
The above command will keep the entire structure of your database but without the data. (that's what the --no-data param does).
Now when you want to reset your database you just:
mysql -uuser -hhost -p < backup_file_name.sql
View the .sql file created to see what its doing. There are a bunch of options you can add to the mysqldump command.
Enjoy.