Sunday, October 7, 2007

Executing SQL scripts on MYSQL

OK, this one IS in the manual :)

But nonetheless, why not take a shortcut & just google it :)

Anyhoo, let's assume you have a database called MYDB at machine localhost which you can access under user name web with password password. The file you want to execute is called sql_script.sql which contains SQL commands delimited by ; as shown below.

DROP TABLE IF EXISTS general_detail;
DROP TABLE IF EXISTS general;

Here's how you can execute this script on MySQL 5.0 :

mysql MYDB -h localhost -u web -ppassword < sql_script.sql

BTW, if you want to execute an SQL command quickly via the command line, type :

mysql MYDB -h localhost -u web -ppassword -e "alter table general AUTO_INCREMENT = 1"