Import SQL file into mysql

前端 未结 18 1445
盖世英雄少女心
盖世英雄少女心 2020-11-28 00:16

I have a database called nitm. I haven\'t created any tables there. But I have a SQL file which contains all the necessary data for the database. The file is

18条回答
  •  暖寄归人
    2020-11-28 00:59

    Ok so, I'm using Linux but I think this holds true for Windows too. You can do this either directly from the command prompt

    > mysql -u  -p  < sqlfilename.sql
    

    Or from within the mysql prompt, you can use:

    mysql>source sqlfilename.sql
    

    But both these approaches have their own benefits in the results they display. In the first approach, the script exits as soon as it encounters an error. And the better part, is that it tells you the exact line number in the source file where the error occurred. However, it ONLY displays errors. If it didn't encounter any errors, the scripts displays NOTHING. Which can be a little unnerving. Because you're most often running a script with a whole pile of commands.

    Now second approach (from within the mysql prompt) has the benefit that it displays a message for every different MySQL command in the script. If it encounters errors, it displays the mysql error message but continues on through the scripts. This can be good, because you can then go back and fix all the errors before you run the script again. The downside is that it does NOT display the line numbers in the script where the errors were encountered. This can be a bit of a pain. But the error messages are as descriptive so you could probably figure out where the problem is.

    I, for one, prefer the directly-from-OS-command line approach.

提交回复
热议问题