John Smith
John Smith

Reputation: 8851

How to execute a SQL file using MySQL command line tool?

I'm using symfony, putty and other SQL commands seem to work fine but when I try to run an .sql file (of which there are many), the command line throws an error:

You have an error in your SQL syntax; check the manual ...

This is the command I'm trying to execute:

mysql < data/MySQLFile.sql;

I already used the use command on the proper database and tried using the full path, relative path, etc. Not sure what the problem is.

Here's the statement:

ALTER TABLE tablename
    ADD COLUMN `blahblah` varchar(10) NULL;
ALTER TABLE tablename
    ADD COLUMN `ggggggg` varchar(50) NULL;

Upvotes: 2

Views: 48510

Answers (3)

NarasimhaTejaJ
NarasimhaTejaJ

Reputation: 1526

if you want to execute through command line

mysql -u user -p < file.sql

and if you want to execute once logged in to mysql database. use any of the below commands.

source file.sql

or

\. file.sql

or

mysql db_name <file.sql

Upvotes: 13

datavoredan
datavoredan

Reputation: 3756

I think you just need to type 'source' before the path to the sql file. So you would have

mysql < source [fullpath]/MySQLFile.sql;

Upvotes: 1

Abraham
Abraham

Reputation: 331

Ok if you are in the mysql interface already type in:

\. /full path/to/file.sql

What I meant with full path is that you need to specify according to the current OS file system the location of the file. Just by putting data/ is not enough. For example if you are using MAC OS it would be: From the terminal and assuming is in Documents:

mysql < /Users/<yourusername>/Documents/file.sql

If you are already logged in mysql then type in:

\. /Users/<yourusername>/Documents/file.sql

If my tips don't help you, I will recommend you to go to this page:

http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html To Find out more about this.

Upvotes: 2

Related Questions