Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn SQL using MySQL in One Day and Learn It Well

You're reading from  Learn SQL using MySQL in One Day and Learn It Well

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781836205678
Pages 121 pages
Edition 1st Edition
Languages
Author (1):
Jamie Chan Jamie Chan
Profile icon Jamie Chan

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:

 

Image

 

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

 

Image

 

and click on the “Execute Statement” button, you’ll get a single tab in the “Result Grid” that displays the message Hello World.

 

Next Chapter arrow right
You have been reading a chapter from
Learn SQL using MySQL in One Day and Learn It Well
Published in: Apr 2024 Publisher: Packt ISBN-13: 9781836205678
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}