Stored Procedures In MySQL

When you use MySQL Workbench or mysql shell to issue the query to MySQL Server, MySQL processes the query and returns the result set. If you want to save this query on the database server for execution later, one way to do it is to use a stored procedure.

The following?SELECT?statement returns all rows in the table?customers?from the?sample database:

SELECT?

??customerName,?

??city,?

??state,?

??postalCode,?

??country

FROM

??customers

ORDER BY customerName;


The following?CREATE PROCEDURE?statement creates a new stored procedure that wraps the query above:


DELIMITER $$


CREATE PROCEDURE GetCustomers()

BEGIN

SELECT?

customerName,?

city,?

state,?

postalCode,?

country

FROM

customers

ORDER BY customerName;???

END$$

DELIMITER ;


Once you save the stored procedure, you can invoke it by using the?CALL?statement:


CALL GetCustomers();


The first time you invoke a stored procedure, MySQL looks up for the name in the database catalog, compiles the stored procedure’s code, place it in a memory area known as a cache, and execute the stored procedure.

If you invoke the same stored procedure in the same session again, MySQL just executes the stored procedure from the cache without having to recompile it.

A stored procedure may contain control flow statements such as?IF,?CASE, and?LOOP?that allow you to implement the code in the procedural way.


MySQL stored procedures advantages:

1. Reduce network traffic

2. Centralize business logic in the database

3. Make database more secure


MySQL stored procedures disadvantages:

1. Resource usages

2. Troubleshooting

3. Maintenances

要查看或添加评论,请登录

Prasad Deshmukh的更多文章

  • Statistical Modeling

    Statistical Modeling

    Statistical modeling is a powerful tool used in data science to describe, analyze, and make predictions about patterns…

  • Artificial Neural Network (ANN)

    Artificial Neural Network (ANN)

    Artificial Neural Network (ANN) is a type of machine learning model that is inspired by the structure and function of…

  • Tableau Interview Questions

    Tableau Interview Questions

    1. What is Tableau, and how does it differ from other data visualization tools? Tableau is a powerful data…

  • Performance Measurement of a Machine Learning Model

    Performance Measurement of a Machine Learning Model

    The performance of a machine learning model is a measure of how well the model is able to generalize to new, unseen…

  • Statistics for Data Science

    Statistics for Data Science

    Statistics is a branch of mathematics that deals with the collection, analysis, interpretation, presentation, and…

    2 条评论
  • Data Science Project Life Cycle

    Data Science Project Life Cycle

    Data Acquisition: This involves identifying relevant data sources, collecting and storing data in a suitable format for…

  • Activation Function in Neural Network

    Activation Function in Neural Network

    An activation function in a neural network is a mathematical function that introduces non-linearity into the output of…

  • Bias-Variance Trade-off

    Bias-Variance Trade-off

    The bias-variance trade-off is a key concept in machine learning that relates to the problem of overfitting and…

  • Python & Libraries

    Python & Libraries

    Python is a high-level programming language that is widely used in a variety of industries, including web development…

  • SQL Interview Questions

    SQL Interview Questions

    1. What is Database? A database is an organized collection of data that is stored and managed on a computer.

社区洞察

其他会员也浏览了