Writing our first MySQL code
Now, we are ready to get our feet wet and write some SQL code.
Type the following lines into the text editor (for the first line, be sure to add a space after the two hyphens):
-- Using SELECT to display messages
SELECT 'Hello World';
SELECT 'MySQL is fun!';
You should notice that the word SELECT is in blue and 'Hello World' and 'MySQL is fun!' are in brown. (The colors may differ depending on your settings or the MySQL Workbench version that you are using).
This is the software’s way of making our code easier to read. Different words serve different purposes in our program, hence they are displayed using different colors. We’ll go into more detail in later chapters.
There are two ways to run the SQL commands that we wrote.
The first task is to select all the code that we want to run and click on the "Execute Selected" button (the button with a lightning bolt icon).
This button should normally be located to the right of the "Save" button (refer to the previous screenshot); it executes the selected portion of the script, or everything if there is no selection.
You will see a new panel called "Result Grid," with two tabs as shown below:
These two tabs give the results of the last two lines of code that we wrote (i.e. the two lines that start with the word SELECT).
The first line of code that we wrote does not give any result as it is a comment. We’ll talk about comments in the next section.
Besides running all the code at one go, you can also choose to execute them one by one. To do that, simply position your cursor on the line that you want to execute and click on the “Execute Statement” button to run that statement. The “Execute Statement” button shows a lightning bolt and a cursor and is located to the right of the “Execute Selected” button.
For instance, if you place your cursor as shown in the image below
and click on the “Execute Statement” button, you’ll get a single tab in the “Result Grid” that displays the message Hello World.