Creating Stored Procedures for Course Data Retrieval

MySQLMySQLBeginner
Practice Now

Introduction

In this project, you will learn how to create a stored procedure in MySQL to retrieve courses with credits greater than 3. This project will help you understand the basics of working with stored procedures in a database management system.

👀 Preview

MariaDB [edusys]> CALL getCourses();
+-----------+----------------------------+------------+---------+
| course_id | title                      | dept_name  | credits |
+-----------+----------------------------+------------+---------+
| BIO-101   | Intro. to Biology          | Biology    |       4 |
| BIO-301   | Genetics                   | Biology    |       4 |
| CS-101    | Intro. to Computer Science | Comp. Sci. |       4 |
| CS-190    | Game Design                | Comp. Sci. |       4 |
| PHY-101   | Physical Principles        | Physics    |       4 |
+-----------+----------------------------+------------+---------+
5 rows in set (0.000 sec)

🎯 Tasks

In this project, you will learn:

  • How to access MySQL using the sudo command without a password
  • How to import data into the MySQL database
  • How to create a stored procedure to retrieve specific data from a table
  • How to test the stored procedure to ensure it is working as expected

🏆 Achievements

After completing this project, you will be able to:

  • Understand the purpose and benefits of using stored procedures in a database
  • Create your own stored procedures to perform specific data retrieval tasks
  • Troubleshoot and test stored procedures to ensure they are functioning correctly
  • Apply the knowledge gained in this project 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`"]) mysql(("`MySQL`")) -.-> mysql/AdvancedFeaturesGroup(["`Advanced Features`"]) sql(("`SQL`")) -.-> sql/AdvancedDataOperationsGroup(["`Advanced Data Operations`"]) sql(("`SQL`")) -.-> sql/DatabaseManagementandOptimizationGroup(["`Database Management and Optimization`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/where("`WHERE clause`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") mysql/AdvancedFeaturesGroup -.-> mysql/stored_procedures("`Procedure Management`") sql/AdvancedDataOperationsGroup -.-> sql/stored_procedures("`Stored Procedures`") sql/DatabaseManagementandOptimizationGroup -.-> sql/using_indexes("`Using Indexes`") subgraph Lab Skills mysql/source -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} sql/select -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} sql/where -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} sql/in -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} mysql/select -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} mysql/stored_procedures -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} sql/stored_procedures -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} sql/using_indexes -.-> lab-301319{{"`Creating Stored Procedures for Course Data Retrieval`"}} end

Access MySQL and Import the Database

In this step, you will learn how to access MySQL using the sudo command without any password, and import the data from /home/labex/project/edusys.sql into MySQL.

  1. Start the MySQL service:
sudo service mysql start
  1. Access MySQL using the sudo command:
sudo mysql
  1. Import the data from /home/labex/project/edusys.sql into MySQL and switch to the edusys database:
MariaDB [None]> SOURCE /home/labex/project/edusys.sql;

Create the getCourses Stored Procedure

In this step, you will create a stored procedure called getCourses that retrieves courses from the course table where the credits field value is greater than 3.

  1. Open the getCourses.sql file.
  2. Add the following code to the getCourses.sql file:
-- Creating the getCourses stored procedure
DELIMITER //

CREATE PROCEDURE getCourses()
BEGIN
-- Retrieve courses from the course table where the credits field is greater than 3
SELECT course_id, title, dept_name, credits
FROM course
WHERE credits > 3;
END //

DELIMITER ;
  1. Save and exit the file.

Test the getCourses Stored Procedure

In this step, you will test the getCourses stored procedure to ensure it is working as expected.

  1. Run the getCourses.sql script in MySQL:
MariaDB [edusys]> SOURCE /home/labex/project/getCourses.sql;
  1. Call the getCourses stored procedure:
MariaDB [edusys]> CALL getCourses();
+-----------+----------------------------+------------+---------+
| course_id | title                      | dept_name  | credits |
+-----------+----------------------------+------------+---------+
| BIO-101   | Intro. to Biology          | Biology    |       4 |
| BIO-301   | Genetics                   | Biology    |       4 |
| CS-101    | Intro. to Computer Science | Comp. Sci. |       4 |
| CS-190    | Game Design                | Comp. Sci. |       4 |
| PHY-101   | Physical Principles        | Physics    |       4 |
+-----------+----------------------------+------------+---------+
5 rows in set (0.000 sec)

The output shows that the getCourses stored procedure is working as expected, retrieving all the courses with credits greater than 3.

Summary

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

Other MySQL Tutorials you may like