what is the correct syntax for executing .sql script in MySQL command line?

Benny Tjia picture Benny Tjia · May 22, 2011 · Viewed 19.7k times · Source

I am confused. From references I have seen online, the command to execute a text file script is this:

mysql> --user=root --password=admin --database=zero <query.sql

However when I ran this, the command line said theres an error with mySQL syntax (error 1064). I saved the query.sql script file within the C:program files...\MYSQL\MYSQL Server5.1.. (whichever folder directory that contains the mySQL command line terminal .exe)

I then did this:

 mysql> USE db1 \g
 mysql> source <query.sql \g

It also doesnt work; command line gave me the same error. mySQL version I have is different than other versions I have seen. As you can see, you have to add '\g' at the end of every query.

Please help, and let me know if the description is not very clear..thx

EDITED: So this is the code I have inside the query.sql:

CREATE TABLE IF NOT EXISTS 'db1'(
'id' int(255) NOT NULL auto_increment,
'date' date NOT NULL,
'title' varchar(255) NOT NULL,
'introtext' text NOT NULL,
'maintext' text NOT NULL,
PRIMARY KEY ('id')
)

Answer

rid picture rid · May 22, 2011

You can run an SQL file from within the client with:

\. query.sql

Alternatively, if you're not already in the client, you can use the following from the command line:

mysql --user=root --password=admin --database=zero < query.sql