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 to interactive prompt for you
to run any SQL statements on the MySQL server or any mysql commands:
To run any SQL statement, enter "sqlStatement;".
The last character, ";", triggers mysql to execute the statement on the server.
To run any mysql commands, enter "command".
To quit from mysql, enter "quit".
To get help, enter "help".
In the following example, I executed 4 SQL statements with mysql:
mysql reports you back on the execution time of each statement.
Result of the SELECT statement is nicely formatted.
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) 1999 by Dr. Herong Yang
--
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: