How to Import MySqlDump File into MySQL without Selective Columns - mysql

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

Related

How can i export single table from database

I Need to take single "nametable" from "MYSQLDATABASE1" and take it by all privilege & relation then import it into other "MYSQLDATABASE2"
This will dump the whole table from the specified database 'MYSQLDATABASE1'
mysqldump -u username -p MYSQLDATABASE1 table_name > nametable.sql
This will load the dumped table into the required database 'MYSQLDATABASE2'
mysql -u username -p MYSQLDATABASE2 < nametable.sql
regarding the privileges & relationship i think this part will require you to manually lookup for them and do it manually table by table

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

mysqldump specific table with table structure with where clause

I want to dump data from a specific table on a specific time period.
So I added a WHERE clause to the dump command. Now I also want to have the table structure so that when I import the SQL it wouldn't return an error. I used the following command:
mysqldump -t -u root -p --host IP DBNAME TABLENAME --where="LastUpdate > '2018-01-09 00:00:00'" > result.sql
However the resulting SQL dump does not include the table structure. How to include the table structure when dumping from MySQL using mysqldump?
Try:
mysqldump -u root -p --host IP DBNAME TABLENAME --where="LastUpdate > '2018-01-09 00:00:00'" > result.sql
Remove -t flag.
4.5.4 mysqldump — A Database Backup Program :: --no-create-info, -t
Do not write CREATE TABLE statements that create each dumped table.
...

Mysqldump: Can you change the name of the table you're inserting into?

I am using mysqldump to transfer a table from one database to another.
mysqldump -h host -u user -p password -e --single-transaction --no-create- info --default-character-set=utf8 --complete-insert --result-file=thisisaresult db table
I was wondering, however, if there is a way to change the name of the table you insert into? For example, I'd like this to insert into table_staging, or something like that. Is this possible, or am I going to have to just use sed?
After creating a mysql dump file you could do the following:
sed -i 's/`old-table-name`/`new-table-name`/g' old-table-name.dump
The sed command will search and replace the old table name with the new table name from within the mysql dump file.
You can change table name on the fly when you create the dump using Unix sed.
# create dump of mytable, renaming it to mytable_new
$ mysqldump -umyuser -pmypass -hmyhost mydb mytable |\
sed -e 's/`mytable`/`mytable_new`/'| gzip -c > mydb_mytable_new.dump.gz
# restoring in another database:
$ gunzip -c mydb_mytable_new.dump.gz | mysql -umyuser2 -pmypass2 -hmyhost2 mydb2
I don't think is possible when dumping data because there may be FK references to the table you are changing.
If there are no FK references to the table you wish to change then it is possible to just hand edit the resulting dump file:
CREATE TABLE `old_table_name`
Becomes
CREATE TABLE `new_table_name`
My recommendation would be to dump the data, re-import it into your new database, then run the alters to rename your table.

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.