I have several sql
files and I want to import all of them at once into a MySQL database.
I go to PHPMyAdmin
, access the databa
In Windows, open a terminal, go to the content folder and write:
copy /b *.sql all_files.sql
This concate all files in only one, making it really quick to import with PhpMyAdmin.
In Linux and macOS, as @BlackCharly pointed out, this will do the trick:
cat *.sql > .all_files.sql
Important Note: Doing it directly should go well, but it could end up with you stuck in a loop with a massive output file getting bigger and bigger due to the system adding the file to itself. To avoid it, two possible solutions.
A) Put the result in a separate directory to be safe (Thanks @mosh):
mkdir concatSql
cat *.sql > ./concatSql/all_files.sql
B) Concat them in a file with a different extension and then change it the name. (Thanks @William Turrell)
cat *.sql > all_files.sql1
mv all_files.sql1 all_files.sql
You could also a for loop to do so:
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql your_db_name < $i
wait
done
Source
Save this file as .bat and run it , change variables inside parenthesis ...
@echo off
title Mysql Import Script
cd (Folder Name)
for %%a in (*) do (
echo Importing File : %%a
mysql -u(username) -p(password) %%~na < %%a
)
pause
if it's only one database modify (%%~na) with the database name .
Enter the mysql shell like this.
mysql --host=localhost --user=username --password --database=db
Then use the source command and a semicolon to seperate the commands.
source file1.sql; source file2; source file3;
Goto cmd
Type in command prompt
C:\users\Usersname>cd [.sql tables folder path ]
Press Enter
Ex: C:\users\Usersname>cd E:\project\database
Type command prompt
C:\users\Usersname>[.sql folder's drive (directory)name]
Press Enter
Ex: C:\users\Usersname>E:
Type command prompt for marge all .sql file(table) in a single file
copy /b *.sql newdatabase.sql
Press Enter
EX: E:\project\database>copy /b *.sql newdatabase.sql
You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
Ex: E:\project\database
Just type below command on your command prompt & it will bind all sql file into single sql file,
c:/xampp/mysql/bin/sql/ type *.sql > OneFile.sql;