Using mysql to Run SQL Statements

This section provides some tutorial examples on how to run SQL statements on a MySQL server with the client tool mysql. SQL statements can be executed one by one interactively, or in batch mode.

Once mysql is started, it will present an interactive prompt for you to run any SQL statements on the MySQL server or any mysql commands:

In the following example, I executed 1 mysql command and 4 SQL statements with mysql:

C:\herong>"%MySQL%\bin\mysql" --host=localhost 
   --user=root --password=TopSecret

mysql> use test

mysql> create table hello (message varchar(80));
Query OK, 0 rows affected (0.41 sec)

mysql> insert into hello (message) values ('Hello world!');
Query OK, 1 row affected (0.03 sec)

mysql> select * from hello;
+--------------+
| message      |
+--------------+
| Hello world! |
+--------------+
1 row in set (0.00 sec)

mysql> drop table hello;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

Note that:

In the previous example, SQL statements were executed interactively one by one. Another way to execute SQL statements is to put them into a file, and execute them in one command. First let's store all the statements we used in the previous into a file, hello.sql:

-- hello.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
create table hello (message varchar(80));
insert into hello (message) values ('Hello world!');
select * from hello;
drop table hello;

To execute the statements in hello.sql, you can use the "source" command inside mysql:

C:\herong>"%MySQL%\bin\mysql" --host=localhost 
   --user=root --password=TopSecret

mysql> use test

mysql> source hello.sql
Query OK, 0 rows affected (0.05 sec)

Query OK, 1 row affected (0.00 sec)

+--------------+
| message      |
+--------------+
| Hello world! |
+--------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Or you can run mysqsl in batch mode:

\mysql\bin\mysql --host=localhost --user=root test < hello.sql
message
Hello world!

As you can see, this is a much better way to execute SQL statements.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

Introduction of MySQL Programs

 List of MySQL Programs

 mysqld - The MySQL Server Program

 mysqladmin - The Client Tool for Administrators

 mysql - The Client Tool for End Users

Using mysql to Run SQL Statements

 mysqldump - Dumping Data to Files

 mysqlimport - Loading Data from Files

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version