|MySQL, Answers <|
|Table of contents|
The objective of this exercise is to make you familiar with how to executing some SQL commands. The following exercises assume you are in the SQL console environment.
By the end of this page you will be able to:
Executing SQL Statements
Exercise 1: SQL and the ;
To get started try the following two commands (press [Enter] at the end of each line).
- Not working???
- Make sure you do not have a space between the function and the brackets. (e.g. NOW[space]() doesn't work!)
Exercise 2: SQL and \g or \G
A ; is the usual way to terminate an sql statement, however you can use \g or \G. Try them out...
The \g is the same as a ; while the \G lists things in a "vertical" format
To speed up the typing see what happens when you use the right and left arrow keys!
Exercise 3: SQL over multiple lines
If you get an extra long line (which will happen as you get more complicated sql commands) you can press [Enter] mid line, for example
Notice that the prompt shows as "->" to show that you are continuing on the statement.
Exercise 4: Cancelling over multiple lines
Use \c to cancel the entering of the current statement, for example
Reference: DuBois, P. (2009, pp. 30-33)
Exercise 5: Running commands from an external file
Read this part first (exercise follows...)
To make things easier you can type your sql into an external text file (e.g. using notepad) and get mysql to load then run the file using the source command.
If you have shell access you can do this when you run mysql
- The % stands for the path
- The extension sql is used but the file is actually a txt file. (You could use a .txt extension but it is more usual to use .sql)
- I created a folder called MVsql for my files on drive C
- If you need to edit the file, you can use Windows Explorer, find the file in the Folders, then right click and Open With > NotePad.
Now try this...
1. Create a folder on your drive with your intials followed by sql (e.g. mine will be MVsql )
2. Create a text file in the folder (e.g. MVsql) called mysqlA.sql, and enter: (Notepad is an application that comes with Windows that allows you to create a text file (Start > Programs > Accessories > Notepad))
3. Start mysql and enter (note I have my sql file on drive C, you may have yours somewhere else)
- DuBois, P. (2009) MySQL Developer's Library, 4th Ed. Addison-Wesley