码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 创建函数

时间:2016-11-10 03:05:10      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:nec   char   data   sel   type   表关联   return   关联   function   

函数

1:查看创建函数的功能是否开启
mysql> show variables like ‘%func%‘;//
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | OFF |
+---------------------------------+-------+

2:开启创建函数的功能
mysql> set global log_bin_trust_function_creators=1;//
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘%func%‘;//
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | ON |
+---------------------------------+-------+
1 row in set (0.00 sec)

3:创建函数
mysql> create function fun01()
-> returns int
-> begin
-> return 10;
-> end;
-> //
Query OK, 0 rows affected (0.06 sec)

4:调用函数
mysql> select fun01();//
+---------+
| fun01() |
+---------+
| 10 |
+---------+
1 row in set (0.00 sec)

5:查看已创建的函数
mysql> show function status where db=‘wh‘\G;
*************************** 1. row ***************************
Db: wh
Name: fun01
Type: FUNCTION
Definer: root@localhost
Modified: 2016-11-09 23:28:39
Created: 2016-11-09 23:28:39
Security_type: DEFINER
Comment:
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
1 row in set (0.14 sec)

4:删除函数
mysql> show function status where db=‘wh‘;
+----+-------+----------+----------------+---------------------+--------------------
| Db | Name | Type | Definer | Modified | Created
+----+-------+----------+----------------+---------------------+--------------------
| wh | fun01 | FUNCTION | root@localhost | 2016-11-09 23:28:39 | 2016-11-09 23:28:39
| wh | fun02 | FUNCTION | root@localhost | 2016-11-09 23:38:03 | 2016-11-09 23:38:03
+----+-------+----------+----------------+---------------------+--------------------

mysql> drop function fun02;//
Query OK, 0 rows affected (0.25 sec)
mysql> show function status where db=‘wh‘;
+----+-------+----------+----------------+---------------------+--------------------
| Db | Name | Type | Definer | Modified | Created
+----+-------+----------+----------------+---------------------+--------------------
| wh | fun01 | FUNCTION | root@localhost | 2016-11-09 23:28:39 | 2016-11-09 23:28:39
+----+-------+----------+----------------+---------------------+--------------------

5:查看某个具体的函数
mysql> show create function fun01;//
+----------+----------------------------------------------------------------+---
| Function | sql_mode | Create Function
+----------+----------------------------------------------------------------+---
| fun01 | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost`
+----------+----------------------------------------------------------------+---
FUNCTION `fun01`() RETURNS int(11)
begin
return 10;
end | latin1 | latin1_swedish_ci | latin1_swedish_


6:函数与表关联使用(只能添加修改语句,不能添加查询语句???)
mysql> create function fun05(p int)
-> returns int
-> begin
-> insert into t values (p);
-> return 10;
-> end;
-> //
Query OK, 0 rows affected (0.01 sec)

mysql> select fun05(10000);//
+--------------+
| fun05(10000) |
+--------------+
| 10 |
+--------------+
1 row in set (0.06 sec)

mysql> select * from t;//
+-------+
| s1 |
+-------+
| 10000 |
+-------+

级联查询

inner join
left join
right join


mysql> desc user;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | decimal(8,2) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+

 

MySQL 创建函数

标签:nec   char   data   sel   type   表关联   return   关联   function   

原文地址:http://www.cnblogs.com/1020182600HENG/p/6049446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!