MySQL 如何使用命令行将单个表导入到mysql数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5387619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import a single table in to mysql database using command line
提问by tismon
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.
我已经使用命令行成功导入了一个数据库,但现在我的痛点是如何使用命令行将单个表及其数据导入到现有数据库中。
回答by sush
Linux :
Linux:
In command line
在命令行中
mysql -u username -p databasename < path/example.sql
put your table in example.sql
把你的表放在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 thecreate table
sql command to create tabletableName
.Import data into table
mysql -u username -p databasename < path/example.sql
This command needs an sql file containing data in form of
insert
statements for tabletableName
. All theinsert
statements will be executed and the data will be loaded.
导出表架构
mysqldump -u username -p databasename tableName > path/example.sql
这将创建一个以
example.sql
提到的路径命名的文件,并编写create table
sql 命令来创建 tabletableName
。将数据导入表
mysql -u username -p databasename < path/example.sql
此命令需要一个 sql 文件,其中包含表的
insert
语句形式的数据tableName
。insert
将执行所有语句并加载数据。
回答by tismon
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
回答by bizna
All of these options are fine, if you have the option to re-export the data.
如果您可以选择重新导出数据,那么所有这些选项都可以。
But if you need to use an existing SQL file, and use a specific table from it, then this perl script in the TimeSheet blog, with enable you to extract the table to a separate SQL file, and then import it.
但是,如果您需要使用现有的 SQL 文件,并使用其中的特定表,那么TimeSheet 博客中的这个 perl 脚本使您能够将表提取到单独的 SQL 文件中,然后将其导入。
回答by Tanmay Patel
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 the sql file tableName.sql
注意:最好使用sql文件tableName.sql的完整路径
回答by Prabhakar Undurthi
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.
这将创建一个名为 test.sql 的文件并创建 table sql 命令来创建表 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.
确保您的 test.sql 文件在同一目录中,如果不是通过路径导航,然后运行命令。
回答by Varun P V
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
然后,移动到.sql
要导入文件的文件夹,并从终端运行以下命令
mysql -u username -p databasename < yourtable.sql
The terminal will ask you to enter the password. Enter it and check the database.
终端会要求您输入密码。输入它并检查数据库。
回答by Ranjith
It works correctly...
它工作正常...
C:\>mysql>bin>mysql -u USERNAME DB_NAME < tableNameFile.sql
please note .sql file specified your current database..
请注意 .sql 文件指定了您当前的数据库..
回答by Shamit Verma
It would be combination of
EXPORT INTO OUTFILE
andLOAD 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 usingLOAD DATA INFILE
回答by Digisha
We can import single table using CMD as below:
我们可以使用 CMD 导入单个表,如下所示:
D:\wamp\bin\mysql\mysql5.5.24\bin>mysql -h hostname -u username -p passowrd databasename < filepath
回答by money
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