Printing Current Time With MySQL

MySQLMySQLBeginner
Practice Now

Introduction

In this project, you will learn how to print the current time using SQL statements in the MySQL client.

👀 Preview

Unfinished

🎯 Tasks

In this project, you will learn:

  • How to access the MySQL database using the sudo command
  • How to write an SQL statement to print the current time in the format HH:MM:SS
  • How to use the AS keyword to set an alias for the current time
  • How to save the SQL statement to a file and execute it in the MySQL client

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to interact with the MySQL database using SQL statements
  • Write SQL queries to retrieve and format date and time data
  • Save SQL scripts to files and execute them in the MySQL client
  • Apply these skills to build more complex database-driven applications

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL mysql(("`MySQL`")) -.-> mysql/BasicKeywordsandStatementsGroup(["`Basic Keywords and Statements`"]) sql(("`SQL`")) -.-> sql/BasicSQLCommandsGroup(["`Basic SQL Commands`"]) sql(("`SQL`")) -.-> sql/DataManipulationandQueryingGroup(["`Data Manipulation and Querying`"]) sql(("`SQL`")) -.-> sql/DataDefinitionandIntegrityGroup(["`Data Definition and Integrity`"]) sql(("`SQL`")) -.-> sql/AdvancedDataOperationsGroup(["`Advanced Data Operations`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/use_database("`Database Selection`") sql/DataDefinitionandIntegrityGroup -.-> sql/data_types("`Data Types`") sql/AdvancedDataOperationsGroup -.-> sql/date_time_functions("`Date and Time functions`") subgraph Lab Skills mysql/source -.-> lab-301376{{"`Printing Current Time With MySQL`"}} sql/select -.-> lab-301376{{"`Printing Current Time With MySQL`"}} sql/in -.-> lab-301376{{"`Printing Current Time With MySQL`"}} mysql/select -.-> lab-301376{{"`Printing Current Time With MySQL`"}} mysql/use_database -.-> lab-301376{{"`Printing Current Time With MySQL`"}} sql/data_types -.-> lab-301376{{"`Printing Current Time With MySQL`"}} sql/date_time_functions -.-> lab-301376{{"`Printing Current Time With MySQL`"}} end

Access MySQL

In this step, you will learn how to access the MySQL database using the sudo command without any password.

  1. Open a terminal on your system.
  2. Start the MySQL service by running the following command:
    sudo service mysql start
  3. Access the MySQL client by running the following command:
    sudo mysql
    This will allow you to interact with the MySQL database using SQL statements.

Write the SQL Statement

In this step, you will write an SQL statement to print the current time in the format HH:MM:SS, using the symbols %h %i %S. You will also use the AS keyword to set an alias as Current Time.

  1. In the MySQL client, run the following SQL statement:
    SELECT DATE_FORMAT(NOW(), '%h:%i:%S') AS `Current Time`;
    This statement uses the DATE_FORMAT() function to format the current time and assigns the result to the alias Current Time.

Save the SQL Statement to a File

In this step, you will save the SQL statement to a file named currentTime.sql in the /home/labex/project directory.

  1. Create the /home/labex/project directory if it doesn't already exist:
    sudo mkdir -p /home/labex/project
  2. Open a text editor and create a new file named currentTime.sql in the /home/labex/project directory.
  3. Copy and paste the following SQL statement into the file:
    SELECT DATE_FORMAT(NOW(), '%h:%i:%S') AS `Current Time`;
  4. Save the file.

Run the SQL Script

In this step, you will run the SQL script you created in the previous step to display the current time.

  1. In the MySQL client, run the following command to execute the SQL script:
    SOURCE /home/labex/project/currentTime.sql;
    This will execute the SQL statement in the currentTime.sql file and display the current time in the format HH:MM:SS.

You should see the following output:

MariaDB [(none)]> SOURCE /home/labex/project/currentTime.sql;
+--------------+
| Current Time |
+--------------+
| 01:44:11     |
+--------------+
1 row in set (0.000 sec)

You have now completed the project of printing the current time using SQL statements in the MySQL client. Congratulations!

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other MySQL Tutorials you may like