MySQL - SHOW FUNCTION STATUS 语句



MySQL SHOW FUNCTION STATUS 语句

函数是由组织良好的、可重复使用的代码块组成的,用于执行单个相关的操作。函数为您的应用程序提供了更好的模块化和高度的代码重用性。您可以使用 CREATE FUNCTION 语句创建存储函数。

MySQL SHOW FUNCTION STATUS 语句显示存储函数的特性。它提供以下信息:

  • 过程的名称。
  • 创建它的数据库。
  • 过程的类型。
  • 过程的创建者。
  • 修改日期等……

语法

以下是 SHOW FUNCTION STATUS 语句的语法:

SHOW FUNCTION STATUS
   [LIKE 'pattern' | WHERE expr]

示例

以下语句显示存储函数的特性:

SHOW FUNCTION STATUS\G;

输出

以上查询产生如下所示的输出:

************** 1. row **************
                  Db: test
                Name: areaOfCircle
                Type: FUNCTION
             Definer: root@localhost
            Modified: -------------------
             Created: -------------------
       Security_type: DEFINER
             Comment:
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 2. row **************
                  Db: test
                Name: demo
                Type: FUNCTION
             Definer: root@localhost
            Modified: -------------------
             Created: -------------------
       Security_type: DEFINER
             Comment:
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 3. row **************
                  Db: test
                Name: sample
                Type: FUNCTION
             Definer: root@localhost
            Modified: -------------------
             Created: -------------------
       Security_type: DEFINER
             Comment:
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 4. row **************
                  Db: test
                Name: while_example
                Type: FUNCTION
             Definer: root@localhost
            Modified: -------------------
             Created: -------------------
       Security_type: DEFINER
             Comment:
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 5. row **************
                  Db: test
                Name: test
                Type: FUNCTION
             Definer: root@localhost
            Modified: -------------------
             Created: -------------------
       Security_type: DEFINER
             Comment:
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .

LIKE 子句

使用 LIKE 子句,您可以指定模式来检索有关函数的信息。

示例

假设我们已经创建了一个新的数据库,并使用如下所示的 CREATE 语句在其中创建了 3 个函数:

CREATE demo;
use dem;
database changed
DELIMITER //
CREATE FUNCTION TestAdd(a INT, b INT) RETURNS INT DETERMINISTIC
   BEGIN
      DECLARE res INT;
      SET res=a+b;
      RETURN res;
   END//
   Query OK, 0 rows affected (0.29 sec)
CREATE FUNCTION TestSub (a INT, b INT) RETURNS INT DETERMINISTIC
   BEGIN
      DECLARE res INT;
      SET res=a-b;
      RETURN res;
   END//
   Query OK, 0 rows affected (0.29 sec)
CREATE FUNCTION TestMul (a INT, b INT) RETURNS INT DETERMINISTIC
   BEGIN
      DECLARE res INT;
      SET res=a*b;
      RETURN res;
   END//
   Query OK, 0 rows affected (0.29 sec)
CREATE FUNCTION TestDiv (a INT, b INT) RETURNS INT DETERMINISTIC
   BEGIN
      DECLARE res INT;
      SET res=a/b;
      RETURN res;
   END//
   Query OK, 0 rows affected (0.29 sec)
DELIMITER ;

以下查询检索名称以“Test”开头的过程信息。

SHOW FUNCTION STATUS LIKE 'Test%'\G;

输出

查询执行后,将产生以下输出:

************** 1. row **************
                  Db: demo
			    Name: TestAdd
			    Type: FUNCTION
		     Definer: root@localhost
		    Modified: 2021-05-14 06:37:04
		     Created: 2021-05-14 06:37:04
       Security_type: DEFINER
		     Comment: 
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 2. row **************
                  Db: demo
			    Name: TestDiv
			    Type: FUNCTION
		     Definer: root@localhost
		    Modified: 2021-05-14 06:37:22
		     Created: 2021-05-14 06:37:22
       Security_type: DEFINER
		     Comment: 
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 3. row **************
                  Db: demo
			    Name: TestMul
			    Type: FUNCTION
		     Definer: root@localhost
		    Modified: 2021-05-14 06:37:16
		     Created: 2021-05-14 06:37:16
       Security_type: DEFINER
		     Comment: 
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci
************** 4. row **************
                  Db: demo
			    Name: TestSub
			    Type: FUNCTION
		     Definer: root@localhost
		    Modified: 2021-05-14 06:37:10
		     Created: 2021-05-14 06:37:10
       Security_type: DEFINER
		     Comment: 
character_set_client: cp850
collation_connection: cp850_general_ci
  Database Collation: utf8mb4_0900_ai_ci

WHERE 子句

您可以使用 SHOW PROCEDURE STATUS 语句的 WHERE 子句来检索与指定条件匹配的过程信息。

示例

假设我们已经使用 CREATE 语句创建了一个名为 Emp 的表,如下所示:

CREATE TABLE Emp(Name VARCHAR(255), DOB DATE, Location VARCHAR(255));

现在,让我们使用 INSERT 语句向 Emp 表中插入一些记录:

INSERT INTO Emp VALUES 
('Amit', DATE('1970-01-08'), 'Hyderabad'),
('Sumith', DATE('1990-11-02'), 'Vishakhapatnam'),
('Sudha', DATE('1980-11-06'), 'Vijayawada');

假设我们创建了一个 `getDob()` 函数,它接受员工的姓名作为参数,检索并返回 DOB 列的值。

DELIMITER //
CREATE FUNCTION test.getDob(emp_name VARCHAR(50))
   RETURNS DATE
   DETERMINISTIC
   BEGIN
      declare dateOfBirth DATE;
      select DOB into dateOfBirth from test.emp where Name = emp_name;
      return dateOfBirth;
   END//
DELIMITER ;

同样,如果我们创建了另一个表,如下所示:

CREATE TABLE student (Name VARCHAR(100), Math INT, English INT, 
Science INT, History INT);

让我们向 student 表中插入一些记录:

INSERT INTO student values
('Raman', 95, 89, 85, 81),
('Rahul' , 90, 87, 86, 81),
('Mohit', 90, 85, 86, 81),
('Saurabh', NULL, NULL, NULL, NULL );

以下函数更新了上面创建的表:

Create Function test.tbl_Update(S_name Varchar(50), M1 INT, 
M2 INT, M3 INT, M4 INT)
   RETURNS INT
   DETERMINISTIC
   BEGIN
      UPDATE student SET Math = M1, English = M2, Science = M3, 
	  History = M4 WHERE Name = S_name;
      RETURN 1;
   END //
DELIMITER ;

您可以使用 SHOW FUNCTION STATUS 语句验证数据库中的函数列表,如下所示:

SHOW FUNCTION STATUS WHERE db = 'test'\G;

输出

以上查询产生以下输出:

************** 1. row **************
                     Db: test
				   Name: getDob
				   Type: FUNCTION
				Definer: root@localhost
			   Modified: 2021-03-21 11:21:12
				Created: 2021-03-13 14:45:36
		  Security_type: DEFINER
				Comment: This is a sample comment
   character_set_client: cp850
   collation_connection: cp850_general_ci
	 Database Collation: utf8mb4_0900_ai_ci
************** 2. row **************
                     Db: test
				   Name: tbl_Update
				   Type: FUNCTION
				Definer: root@localhost
			   Modified: 2021-03-13 22:16:05
				Created: 2021-03-13 22:16:05
		  Security_type: DEFINER
				Comment: 
   character_set_client: cp850
   collation_connection: cp850_general_ci
	 Database Collation: utf8mb4_0900_ai_ci

以下查询删除上面创建的函数:

DROP FUNCTION getDob;

DROP FUNCTION tbl_update;

由于我们已经删除了这两个函数。如果您再次验证函数列表,您将得到一个空集:

SHOW FUNCTION STATUS WHERE db = 'test';
Empty set (0.00 sec)
广告
© . All rights reserved.