Is `definer` required when creating a MySQL stored procedure?


No, definer part is not compulsory when you are creating a stored procedure. It is used when you want to create a definer.

Check all the user and host from the MySQL.user table −

mysql> select user,host from mysql.user;

The following is the output −

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Manish           | %         |
| User2            | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| Adam Smith       | localhost |
| User1            | localhost |
| am               | localhost |
+------------------+-----------+
9 rows in set (0.03 sec)

Here, let us create a definer as Adam Smith. The query to create a stored procedure is as follows −

mysql> delimiter //
mysql> CREATE DEFINER = 'Adam Smith'@'localhost' procedure Sp_Definer()
   -> begin
   -> select 'Hello MySQL';
   -> end;
   -> //
Query OK, 0 rows affected (0.14 sec)
mysql> delimiter ;

Call the stored procedure using CALL command. The syntax is as follows −

CALL yourStoredProcedureName();

Call the above stored procedure.

mysql> call Sp_Definer();

The following is the output.

+-------------+
| Hello MySQL |
+-------------+
| Hello MySQL |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)

Updated on: 30-Jun-2020

901 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements