Sunday, July 5, 2009

How to run many scripts in MySQL

In some projects, the database schema changes day by day. For every changes, we have a script to update database. This causes we will have many scripts regarding the changes. In fact, we can merge these scripts into one main script. However, it is not easy to keep track changes of db between releases. For every time create database(maybe for testing), we have to run many scripts in sequence, it takes time :). I have tried by using command:

mysql> source script1.sql, script2.sql,..

but it doesn't work.

To do this, we can create an shell script(Unix ) or an bat file(Window). The content of the file is just commands to run scripts we need.

The syntax is:

mysql -uusername -ppassword -D database_name < script

For ex:
below is a shell script to run script1.sql, script2.sql, script3.sql in sequence automatically. This

mysql -u root -ppassword -D shopping < c:/script1.sql
mysql -u root -ppassword -D shopping < c:/script2.sql
mysql -u root -ppassword -D shopping < c:/script3.sql

From now, to create database, the only thing we need is to run this bat file or shell script file.

Thursday, February 5, 2009