Import single sql table from whole db export - mysql

Is it possible to import a single database table from a large sql file? This would save me several hours, as I'm currently doing:
mysql -u username -p database_name < file.sql

You can add the tablename option, using the tick marks around the table name:
mysql -u root --database=`avails` --table=`main_creditmaster` < /Users/david/Desktop/Avails/July2-Avails-Prod-Export

Related

How I can escape backtick (´) from table name and column name?

I am trying to import DB dump with following command.
mysql -u doadmin -p -f tetdb < test.sql
then I am getting all imported table name and column warped by Backtick (´).
´test_cache_menu´
´test_cache_data´
My test.sql is too big (40GB) so I can't edit the file.
use these commands :
1) login to mysql by:
mysql -u USERNAME -p
2) create a database in which you want to import by
create database DB_NAME;
3) use created database
use DB_NAME;
4) then export test.sql to your database :
source /PATH_TO_DIR/test.sql;
that's all

How to Import MySqlDump File into MySQL without Selective Columns

I am having few large mysqldump files that i need to import to mysql on my desktop mysql server.Each dump file has only 1 table with multiple columns.I want to import on selective 2 columns as other columns are not required.
Currently i have been importing the dump using below stated commands, but this imports table with all columns.But i want to import only selective columns.
Create Database DatabaseName;
CONNECT DatabaseName
Show tables;
Source D:/DatabaseName
please suggest.
There are three ways you can go about this:
The easiest and fastest way to do this would be to first import the full dump, then drop the column(s) you don't want:
mysql -u username -p password database_name < file_name.sql
mysql -u username -p password database_name -e'ALTER TABLE table_name DROP COLUMN column_name'
If the column you want to remove is very large, you can use sed to preemptively (and quickly) remove the offending column. Something like this (the regex patterns will change based on the data you have in your table):
# First, remove the column name from the INSERT line
sed -i "s/`column_name`,//g" file_name.sql
# Then, remove the column value from the VALUES lines
sed -i "s/(\([0-9]+,'[\w+]',[0-9]+,)'.*',([0-9]+\),/\1\2/g" file_name.sql
# Then, you can import your dump, entirely stripped of that column
mysql -u username -p password database_name < file_name.sql
More about sed: https://en.wikipedia.org/wiki/Sed
More about regex: https://en.wikipedia.org/wiki/Regular_expression
If you have access to the DB the dump was made from, you can remove the column from a temp table before dumping:
# First, create the temp table as a clone of your table, import the data
mysql -u username -p password database_name -e 'CREATE TABLE table_name2 LIKE table_name; INSERT INTO table_name2 SELECT * FROM table_name; ALTER TABLE table_name2 DROP COLUMN column_name;'
# Dump ONLY the temp table with the missing column
mysqldump -u username -p password database_name table_name2 > dump_excluding_column.sql
# Afterwards, delete the temporary table
mysql -u username -p password database_name -e 'DROP TABLE table_name2;'
When performing the import, you'll have to rename your table afterwards, e.g.:
# Run the import
mysql -u username -p password database_name < dump_excluding_column.sql
# Afterwards, rename the table name to remove the "2" suffix
mysql -u username -p password database_name -e 'RENAME TABLE table_name2 TO table_name;'
if You need dump with only necessary columns:
1) create temporary db
2) import dump
3) with some db gui or from console alter tables and keep necessary columns
4) backup resulting database
if You need to import directly to database only N olumns, so:
1) import dump
2) with some db gui or from console alter tables and keep necessary columns
sooooooo... simple

ssh Mysql dump table parts (11GB DB to smaller pieces)

I'm facing the following:
We have a DB table of 11GB with over 257 million records and need a backup. Exporting via PHPmyAdmin isn't possible (chrome keeps crashing) and backing up with SSH mysqldump tablename will give a insufficient space disk error (error 28).
Now I'd like to know if there is a way to export a mysqldump with a row 0 till ~100.000.000 command so we can make 3 parts (or smaller parts if required).
What I'm using:
mysqldump -p -u username database_name database_table > dbname.sql
[EDIT]
Found out how to get a row of <50.0000.0000 to SQL with the following:
mysqldump -p -u db_name db_table --where='id<50000000'
But the big question remains now, how to go further? Now I want to get all records between 50.000.000 and 100.000.000 ..
Anybody knows the answer if it's possible and what command I should use?
Problem solved:
Part 1 (<50.000.000):
mysqldump -p -u db_name db_table --where='id<50000000' >part_1.sql
Part 2 (>50.000.000 till <100.0000.000):
mysqldump -p -u db_name db_table --where='id>=50000000 &&
id<100000000' >part_2.sql
Part last (>250.000.000)
mysqldump -p -u db_name db_table --where='id>250000000' >part_final.sql
And so on..
mysqldump creates a text file that contains sql statements, if want to take mysql backup in parts then you will have to run mysqldump like this
mysqldump --where "id%2=0" database_name table > table_even.sql
mysqldump --where "id%2=1" database_name table > table_odd.sql
OR
you need to write some program, script to achieve that
I found a nice solution for heavy transfers! This might also help you to avoid to transfer your database in parts (as in this example) - since it does this super fast:
Exporting a full database or in parts as mentioned using mysqldump:
mysqldump -p -u db_name db_table --where='id<50000000' >part_1.sql
To import to the new database - login via terminal to the new database:
mysql -h localhost -upotato -p123456
Enter the database:
USE databasename;
Use the source command:
source /path/to/file.sql;
This works X1000 faster than the standard:
mysql -h localhost_new -upotato -p1234567 table_name < /path/to/file.sql
Since you enter the database.

Import Tables From Database

Sorry if this is dumb question. I want to know if/how to import tables from one database to another using the mysql console/command line. I would love to speed up my workflow with Something like this
CREATE DATABASE dbname;
IMPORT tables into dbname
I have been creating a lot of databases locally using wamp and would like to easily create my database and import some WordPress tables using the command line. Thanks in advanced.
Use CREATE TABLE targetdb.tablename SELECT * FROM sourcedb.tablename to copy the table structure and its content from one database to another database.
Note that auto increment and indizes are not preserved, but can be added to the CREATE TABLE statement explicitly.
You can export one database and then can import that to another database.
mysqldump --host=hostip --user=username --password=password --database databasename > dump.sql
mysql --host=hostip --user=username --password=password databasename < dump.sql
Thanks
You should familiarize yourself with the use of the mysqldump tool (http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html) and command line mysql usage
> mysqldump --no-create-db [DB name] > /path/to/file.sql
> mysql -h [hostname] -u [username] -p[password] [new db name] < /path/to/file.sql
Note here the use of the --no-create-db option, which will remove the CREATE DATABASE part of the SQL, allowing you to create the DB with new name.

How to import a single table in to mysql database using command line

I had successfully imported a database using command line, but now my pain area is how to import a single table with its data to the existing database using command line.
Linux :
In command line
mysql -u username -p databasename < path/example.sql
put your table in example.sql
Import / Export for single table:
Export table schema
mysqldump -u username -p databasename tableName > path/example.sql
This will create a file named example.sql at the path mentioned and write the create table sql command to create table tableName.
Import a single table into database
mysql -u username -p databasename < path/example.sql
This command needs an sql file containing data in form of insert statements for table tableName. All the insert statements will be executed and the data will be loaded.
Export:
mysqldump --user=root databasename > whole.database.sql
mysqldump --user=root databasename onlySingleTableName > single.table.sql
Import:
Whole database:
mysql --user=root wholedatabase < whole.database.sql
Single table:
mysql --user=root databasename < single.table.sql
Importing the Single Table
To import a single table into an existing database you would use the following command:
mysql -u username -p -D database_name < tableName.sql
Note:It is better to use full path of the sql file tableName.sql
First of all, login to your database and check whether the database table which you want to import is not available on your database.
If it is available, delete the table using the command. Else it will throw an error while importing the table.
DROP TABLE Table_Name;
Then, move to the folder in which you have the .sql file to import and run the following command from your terminal
mysql -u username -p databasename < yourtable.sql
The terminal will ask you to enter the password. Enter it and check the database.
Command Line
Import / Export for single table:
Exporting table schema
-> mysqldump -u your_user_name -p your_database_name table_name > test.sql
This will create a file named test.sql and creates table sql command to create table table_name.
Importing data into table
-> mysql -u your_user_name -p database_name table_name < test.sql
Make sure your test.sql file is in the same directory, if not navigate through the path and then run the command.
It works correctly...
C:\>mysql>bin>mysql -u USERNAME DB_NAME < tableNameFile.sql
please note .sql file specified your current database..
We can import single table using CMD as below:
D:\wamp\bin\mysql\mysql5.5.24\bin>mysql -h hostname -u username -p passowrd databasename < filepath
If you're in the pwd of an SQL dump and you need a table from that, do this:
sed -n '/-- Table structure for table `'TableNameTo_GrabHere'`/,/-- Table/{ /^--.*$/d;p }' dump_file_to_extract_from.sql > table_name_here.sql
Then just import the table you extracted from the above into the needed database
you can do it in mysql command instead of linux command.
1.login your mysql.
2.excute this in mysql command:
use DATABASE_NAME;
SET autocommit=0 ; source ABSOLUTE_PATH/TABLE_SQL_FILE.sql ; COMMIT ;
if you already have the desired table on your database, first delete it and then run the command below:
mysql -u username -p databasename < yourtable.sql
From server to local(Exporting)
mysqldump -u username -p db_name table_name > path/filename.sql;
mysqldump -u root -p remotelab welcome_ulink >
/home_local/ladmin/kakalwar/base/welcome_ulink.sql;
From local to server(Importing)
mysql -u username -p -D databasename < path/x/y/z/welcome_queue.sql
mysql -u root -p -D remotelab <
/home_local/ladmin/kakalwar/instant_status/db_04_12/welcome_queue.sql
Also its working. In command form
cd C:\wamp\bin\mysql\mysql5.5.8\bin //hit enter
mysql -u -p databasename //-u=root,-p=blank
It would be combination of EXPORT INTO OUTFILE and LOAD DATA INFILE
You need to export that single table with EXPORT INTO OUTFILE, this will export table data to a file. You can import that particular table using LOAD DATA INFILE
Refer doc1 , doc2
To import a particular table in database follow below command.
Here table_name.sql is dump of taht particular table that you are going to import
mysql -u root -p database_name table_name < table_name.sql
To export a particular table from database follow below command.
mysqldump -u root -p database_name table_name > table_name.sql
-> mysql -h host -u user -p database_name table_name < test_table.sql
Using a temporary database could be a solution depending on the size of the database.
mysql -u [username] -p -e "create database tempdb"
mysql -u [username] -p tempdb < db.sql
mysqldump -u [username] -p tempdb _table_to_import_ > table_to_import.sql
mysql -u [username] -p maindb < table_to_import.sql
To import a table into database, if table is already exist then add this line in your sql file DROP TABLE IF EXIST 'table_name' and run command mysql -u username -p database_name < import_sql_file.sql.
Please verify the sql file path before execute the command.
Open the backup file in the VScode and search the table name copy the create table and insert command for the table. Copy and execute those two commands in the database where it is required.
Use the below command to import a single table into the database on RDS
mysql -h rds_end_point -u username -p databasename < example.sql
First of all take backup of your both database, step 2 select table which you want to export now select export button now download sql file now you have to import into another database simply select database and then import sql file ... simple and easy.