MySQL 执行 SQL 脚本以创建表和行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4003034/
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
Execute SQL script to create tables and rows
提问by Johanisma
I have a database that I created using the CREATE DATABASE statement in the terminal and I have a .sql file full of statements creating tables and rows.
我有一个在终端中使用 CREATE DATABASE 语句创建的数据库,我有一个 .sql 文件,其中包含创建表和行的语句。
I just wanted to know what was the command line to execute that .sql on the database I created?
我只是想知道在我创建的数据库上执行该 .sql 的命令行是什么?
回答by Mark Byers
In the MySQL interactive client you can type:
在 MySQL 交互式客户端中,您可以键入:
source yourfile.sql
Alternatively you can pipe the data into mysql from the command line:
或者,您可以从命令行将数据通过管道传输到 mysql:
mysql < yourfile.sql
If the file doesn't specify a database then you will also need to add that:
如果文件没有指定数据库,那么您还需要添加:
mysql db_name < yourfile.sql
See the documentation for more details:
有关更多详细信息,请参阅文档:
回答by user88975
If you have password for your dB then
如果您有 dB 的密码,那么
mysql -u <username> -p <DBName> < yourfile.sql

