dump out some tables of the database - sql

I am using MySQL v5.1 on Ubuntu machine.
I have a database named db_test which contain tables like cars, customers, departments, prices , and so on.
I know I can use the following commands to dump out the db_test database and dump the database back into a new database in following way:
mysqldump -u username -p -v db_test > db_test.sql
mysqladmin -u username -p create new_database
mysql -u username -p new_database < db_test.sql
But for my new_database , I only needs some tables from db_test database, not all the tables.
So, How can I dump out some tables from db_test database and dump these tables back to my new_database ?

please use below code:
mysqldump -u username -p -v db_test[table1, table2,....] > db_test.sql

From the MySQL docs:
shell> mysqldump [options] db_name [tbl_name ...]
List the names of the tables after the database name; listing no table names results in all tables being dumped.

Simply list the tables in the mysqldump command.

Related

psql export query output to a new table in a new sqlite3 db

Using psql we can export a query output to a csv file.
psql -d somedb -h localhost -U postgres -p 5432 -c "\COPY (select * from sometable ) TO 'sometable.csv' DELIMITER ',' CSV HEADER;"
However I need to export the query output to a new table in a new sqlite3 database.
I also looked at pg_dump, but haven't been able to figure it out a way with it.
The reason I want to export it as a new table in a new sqlite3 db without any intermediately CSV conversion is because
The query output is going to run into GBs, I have disk space constraints - so rather than csv export and then create a new sqlite3 db, need to get this in one shot
My solution is using the standard INSERT SQL statements.
It's required the same table scheme. The grep command removes the problematic characters, such as -- or blanklines.
pg_dump --data-only --inserts --table=sometable DBNAME | grep -v -e '^SET' -e '^$' -e '^--' | sqlite3 ./target.db
I hope this will help you.

mysqldump with --where clause is not working

mysqldump -t -u root -p mytestdb mytable --where=datetime LIKE '2014-09%'
This is what I am doing and it returns:
mysqldump: Couldn't find table: "LIKE"
I am trying to return all the rows where the column datetime is like 2014-09 meaning "all September rows".
You may need to use quotes:
mysqldump -t -u root -p mytestdb mytable --where="datetime LIKE '2014-09%'"
Selecting dates using LIKE is not a good idea. I saw this method in one project. This causes huge DBMS load and slow system operation as no index by this table column used.
If you need to select date range use between:
where datetime between '2014-09-01' and '2014-09-30 23:59:59'
Not the answer but just a notice, when using mysqldump it will automatically add DROP TABLE and CREATE TABLE to the export file, in case you don't want that add --skip-add-drop-table and --no-create-info with the command, like:
mysqldump -u root-p database_name table_name --skip-add-drop-table --no-create-info > export.sql
You missed "" for where clause . datetime column name datetime is not recommended. It is a data type as well.
mysqldump -u root -p mytestdb mytable --where="datetime LIKE '2014-09%'
" > mytable.sql;
After executing the command a prompt will ask for MySQL password. then check your current folder for generated mystable.sql

MySQL Restore - DB Tables with Prefix and SQL Script with No Prefix

In command line, I'm trying to restore some (not all) tables of data from an MySQL SQL script file. However, my single database tables have a prefix and the sql file tables does not.
Is there a way within the command line to specify a prefix on restore?
mysql -uroot -p databasename < script_with_no_prefix.sql
You can pick out the tables you need using a sed command. For example, if your table prefix is "prefix_", you could use this:
$ sed -n -e '/^-- Table structure for table `prefix_/,/^UNLOCK TABLES/p' \
script_with_no_prefix.sql | mysql -uroot -p databasename

MYSQL Dump only certain rows

I am trying to do a mysql dump of a few rows in my database. I can then use the dump to upload those few rows into another database. The code I have is working, but it dumps everything. How can I get mysqldump to only dump certain rows of a table?
Here is my code:
mysqldump --opt --user=username --password=password lmhprogram myResumes --where=date_pulled='2011-05-23' > test.sql
Just fix your --where option. It should be a valid SQL WHERE clause, like:
--where="date_pulled='2011-05-23'"
You have the column name outside of the quotes.
You need to quote the "where" clause.
Try
mysqldump --opt --user=username --password=password lmhprogram myResumes --where="date_pulled='2011-05-23'" > test.sql
Use this code for specific table rows, using LIKE condition.
mysqldump -u root -p sel_db_server case_today --where="date_created LIKE '%2018
%'" > few_rows_dump.sql

PostgreSQL how to create a copy of a database or schema?

Is there a simple way to create a copy of a database or schema in PostgreSQL 8.1?
I'm testing some software which does a lot of updates to a particular schema within a database, and I'd like to make a copy of it so I can run some comparisons against the original.
If it's on the same server, you just use the CREATE DATABASE command with the TEMPLATE parameter. For example:
CREATE DATABASE newdb WITH TEMPLATE olddb;
pg_dump with the --schema-only option.
If you have to copy the schema from the local database to a remote database, you may use one of the following two options.
Option A
Copy the schema from the local database to a dump file.
pg_dump -U postgres -Cs database > dump_file
Copy the dump file from the local server to the remote server.
scp localuser#localhost:dump_file remoteuser#remotehost:dump_file
Connect to the remote server.
ssh remoteuser#remotehost
Copy the schema from the dump file to the remote database.
psql -U postgres database < dump_file
Option B
Copy the schema directly from the local database to the remote database without using an intermediate file.
pg_dump -h localhost -U postgres -Cs database | psql -h remotehost -U postgres database
This blog post might prove helpful for you if you want to learn more about options for copying the database using pg_dump.
This can be done by running the following command:
CREATE DATABASE [Database to create] WITH TEMPLATE [Database to copy] OWNER [Your username];
Once filled in with your database names and your username, this will create a copy of the specified database. This will work as long as there are no other active connections to the database you wish to copy. If there are other active connections you can temporarily terminate the connections by using this command first:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '[Database to copy]'
AND pid <> pg_backend_pid();
A good article that I wrote for Chartio's Data School which goes a bit more in depth on how to do this can be found here:
https://dataschool.com/learn/how-to-create-a-copy-of-a-database-in-postgresql-using-psql