How do I execute an SQL script in MySQL?
Author: Deron Eriksson
Description: This tutorial describes executing a SQL script in MySQL.
Tutorial created using: Windows XP || MySQL 5.0.27


Page:    1 2 >

In MySQLW, you can execute a script with the SOURCE command. For example, suppose we have the following script file.

employees_table.sql

USE testdatabase;
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (id INT, first_name VARCHAR(20), last_name VARCHAR(30));
INSERT INTO employees (id, first_name, last_name) VALUES (1, 'John', 'Doe');
INSERT INTO employees (id, first_name, last_name) VALUES (2, 'Bob', 'Smith');
INSERT INTO employees (id, first_name, last_name) VALUES (3, 'Jane', 'Doe');
SELECT * FROM employees;

If I place this script at C:\, I can then run the SOURCE command with this SQLW script file, as shown below.

executing an sql script in mysql

As you can see, the script switches to the 'testdatabase' databaseW, drops the 'employees' table if it exists, creates the table, inserts three rows, and then displays the contents of the table.

(Continued on page 2)

Page:    1 2 >