Open In App

MariaDB Procedure

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MariaDB is an open-source database to help store and organize data. It’s similar to other popular database software like MySQL but has some unique features and improvements. The MariaDB database is used for various purposes such as data warehousing, e-commerce, enterprise-level features, and logging applications database. MariaDB offers built-in replication and supports Galera Cluster for high availability.

In this article, We will learn about the MariaDB Procedure in detail along with its functionality, some practical examples, and so on.

MariaDB Procedure

MariaDB Procedure is a stored program of the database to pass the parameter in the given procedure. It works like a MariaDB function but does not return any output value. We can Create, Verify, and Delete the procedure. It is similar to the function for all operations but does not get any output.

In MariaDB, the procedure can be operated with three functionality.

  • Create Procedure
  • Verify Procedure
  • Drop Procedure

Create Procedure in MariaDB

The Create procedure is used to create new procedure program to handle complicated information and get output. We can customize your own procedure in the MariaDB database.

Syntax:

The following syntax uses to create a new procedure in mariaDB database.

CREATE   
[ DEFINER = { CURRENT_USER | user_name } ]
PROCEDURE procedure_names
[ (parameter_name datatype [, parameter_name datatype]) ]
[ LANGUAGE SQL
| DETERMINISTIC
| NOT DETERMINISTIC
| { CONTAINS SQL
| NO SQL
| READS SQL DATA
| MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'comment_values'
BEGIN
declaration_section
executable_section
END;

Explanation of Syntax:

  • DEFINER clause: The DEFINER clause is a optional clause. we can use user name or own name.
  • procedure_names: This parameter is provided procedure name in the MariaDB.
  • IN: The procedure can reference to the parameter. The procedure cannot change the value of the parameter.
  • OUT: The procedure can overwrite the value of the parameter.
  • IN OUT: The procedure has the authority to overwrite the parameter’s value and use to refer it.
  • LANGUAGE SQL: It is used for portability. it is unaffected clause in procedure.
  • DETERMINISTIC: This indicates a set of input parameters and the function gets a single output.
  • NOT DETERMINISTIC: It is depending on the input parameters. the function provides a different outcome.
  • CONTAINS SQL: MariaDB database is informed by this instructive clause. the function contains SQL, but the database does not confirm this clause.
  • NO SQL: this is a optional informative clause. It does not affect on the procedure.
  • READS SQL DATA: It is used to read data using the sql statement.
  • MODIFIES SQL DATA: A MariaDB function works for the DDL commands such as INSERT, UPDATE, DELETE, or others to modify SQL data.
  • declaration_section: The local variables are declared in this section.
  • executable_section: The code is used to operate the functionality.

Example of Create Procedure

The example shows Ceate new procedure in MariaDB database using basic requirements.

Query:

Let’s Design a MariaDB procedure that iteratively increments a value from 60 to the closest multiple of 4 greater than or equal to 2000 and capturing the final result as an output parameter.

DELIMITER //  
CREATE procedure MainValue (OUT initial_value INT )
DETERMINISTIC
BEGIN
DECLARE final_value INT;
SET final_value = 60;
labels: WHILE final_value <= 2000 DO
SET final_value = final_value + 4;
END WHILE labels;
SET initial_value = final_value;
END; //
DELIMITER ;

Output:

CreateProcedure

image of create new procedure

Explaination:

  • The function creates procedure to operate the parameters and set the variable data.
  • The “initial_value” is the input parameter of the mariadb procedure.
  • The deterministic is used to get the single output.
  • we can set the final value as a sixty (60) to operate with the function.
  • The “while” condition set the “2000greater than equal to final_value parameter.
  • The procedure is used for addition operation with the loop.
  • The final_value is 60 shows less the 2000 then variable takes 2000 as initial value.
  • The procedure operates the 2000+4 as a functionality and get the 2004 output.
  • Create function is required the query with the input parameter.

Verification Procedure

We can verify the procedure of the MariaDB database. The verification of the function the “CALL” with variable and “select” with the variable is important. We can call the procedure with the input data. The select query displays the output of the procedure in MariaDB database.

Syntax:

The following syntax uses to Verify procedure in mariaDB database.

CALL procedure_names(@variable_names);
select @variable_names;

Example of Verify Procedure

The following example uses to Verify available procedure in MariaDB database.

Query:

Let’s Execute a MariaDB procedure named MainValue and get its output value in a variable, and then display the retrieved value

CALL MainValue(@variable_names);
SELECT @variable_names

Output:

VerifyProcedure

image verify mariaDB procedure

Explanation:

  • Use the “CALL” keyword with the procedure name and its input variable.
  • Use the “select” keyword with the input variable name as an query.
  • The final_value is 60 shows less the 2000 then variable takes 2000 as initial value.
  • The input variable takes 2000 with the addition of 4 value.
  • The output shows the 2004 for the procedure verification code.

Drop Function

The drop function is used to delete available and unwanted functions in the mariaDB.

Syntax:

The following syntax uses to delete the available function in mariaDB database.

DROP procedure [ IF EXISTS ] procedure_names;  
OR
DROP procedure procedure_names;

Explaination of Syntax:

The “procedure_names” is available procedure name to delete from the mariaDB database.

The [ IF EXISTS ] is the optional keyword for the drop procedure name.

Example of DROP Function

The example shows delete old procedure in mariaDB database using basic requirements. the drop procedure can remove function from database permanently.

Query:

DROP procedure MainValue;

Output:

DropProcedure

image of drop mariaDB procedure

Explanation: In the above Query, We have seen the DROP procedure query is used to delete the “MainValue” function.

Conclusion

In the mariaDB database procedure, we can create, select and drop procedure statement for the complicated information and its operation. It helps to handle and operate the different data types of the value with the database statement. In this article, we will learn advance database procedure statement as create, verify and drop the procedure statement. it is used to avoid the repetition of the query and code and run minimum time with the data. It is helpful for developer to operate the variable and its data for the minimum time and short code length.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads