This lab introduces you to working with MySQL from the command line. You will learn how to connect to a MySQL server, create a database, define a table, insert data, and query it. This foundational "Hello World" exercise will prepare you for more complex SQL tasks.
By the end of this lab, you will be able to:
Open your terminal or command prompt.
Log in to the MySQL server using the following command:
mysql -u root -p
root
with your MySQL username if different.After a successful login, you will see the MySQL prompt:
mysql>
At the MySQL prompt, create a new database named hello_world
:
CREATE DATABASE hello_world;
Use the newly created database:
USE hello_world;
Database changed
.Define a table named greetings
with the following structure:
id
(integer, primary key, auto-incremented)message
(text, stores a greeting message)Execute the following SQL statement:
CREATE TABLE greetings (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT NOT NULL
);
Verify the table creation by describing its structure:
DESCRIBE greetings;
Insert a "Hello, World!" message into the greetings
table:
INSERT INTO greetings (message) VALUES ('Hello, World!');
Confirm that the data has been added:
SELECT * FROM greetings;
You should see output similar to this:
+----+--------------+
| id | message |
+----+--------------+
| 1 | Hello, World!|
+----+--------------+
Retrieve only the message
column from the table:
SELECT message FROM greetings;
Experiment by adding another greeting and querying the data again:
INSERT INTO greetings (message) VALUES ('Welcome to MySQL!');
SELECT * FROM greetings;
Exit the MySQL command line:
EXIT;
Close your terminal or command prompt.
In this lab, you learned how to:
This "Hello World" lab serves as your first step into MySQL, providing hands-on experience with the essential commands needed for database interaction. In the next lab, we will build on these concepts by exploring how to filter and sort data using SQL queries.