SQL语法基础之高级应用

                                     作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.存储过程与函数

1>.CREATE PROCEDURE 用来创建存储过程

  1. mysql> ? CREATE PROCEDURE
  2. Name: 'CREATE PROCEDURE'
  3. Description:
  4. Syntax:
  5. CREATE
  6. [DEFINER = { user | CURRENT_USER }]
  7. PROCEDURE sp_name ([proc_parameter[,...]])
  8. [characteristic ...] routine_body
  9.  
  10. CREATE
  11. [DEFINER = { user | CURRENT_USER }]
  12. FUNCTION sp_name ([func_parameter[,...]])
  13. RETURNS type
  14. [characteristic ...] routine_body
  15.  
  16. proc_parameter:
  17. [ IN | OUT | INOUT ] param_name type
  18.  
  19. func_parameter:
  20. param_name type
  21.  
  22. type:
  23. Any valid MySQL data type
  24.  
  25. characteristic:
  26. COMMENT 'string'
  27. | LANGUAGE SQL
  28. | [NOT] DETERMINISTIC
  29. | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  30. | SQL SECURITY { DEFINER | INVOKER }
  31.  
  32. routine_body:
  33. Valid SQL routine statement
  34.  
  35. These statements create stored routines. By default, a routine is
  36. associated with the default database. To associate the routine
  37. explicitly with a given database, specify the name as db_name.sp_name
  38. when you create it.
  39.  
  40. The CREATE FUNCTION statement is also used in MySQL to support UDFs
  41. (user-defined functions). See
  42. http://dev.mysql.com/doc/refman/8.0/en/adding-functions.html. A UDF can
  43. be regarded as an external stored function. Stored functions share
  44. their namespace with UDFs. See
  45. http://dev.mysql.com/doc/refman/8.0/en/function-resolution.html, for
  46. the rules describing how the server interprets references to different
  47. kinds of functions.
  48.  
  49. To invoke a stored procedure, use the CALL statement (see [HELP CALL]).
  50. To invoke a stored function, refer to it in an expression. The function
  51. returns a value during expression evaluation.
  52.  
  53. CREATE PROCEDURE and CREATE FUNCTION require the CREATE ROUTINE
  54. privilege. They might also require the SET_USER_ID or SUPER privilege,
  55. depending on the DEFINER value, as described later in this section. If
  56. binary logging is enabled, CREATE FUNCTION might require the SUPER
  57. privilege, as described in
  58. http://dev.mysql.com/doc/refman/8.0/en/stored-programs-logging.html.
  59.  
  60. By default, MySQL automatically grants the ALTER ROUTINE and EXECUTE
  61. privileges to the routine creator. This behavior can be changed by
  62. disabling the automatic_sp_privileges system variable. See
  63. http://dev.mysql.com/doc/refman/8.0/en/stored-routines-privileges.html.
  64.  
  65. The DEFINER and SQL SECURITY clauses specify the security context to be
  66. used when checking access privileges at routine execution time, as
  67. described later in this section.
  68.  
  69. If the routine name is the same as the name of a built-in SQL function,
  70. a syntax error occurs unless you use a space between the name and the
  71. following parenthesis when defining the routine or invoking it later.
  72. For this reason, avoid using the names of existing SQL functions for
  73. your own stored routines.
  74.  
  75. The IGNORE_SPACE SQL mode applies to built-in functions, not to stored
  76. routines. It is always permissible to have spaces after a stored
  77. routine name, regardless of whether IGNORE_SPACE is enabled.
  78.  
  79. The parameter list enclosed within parentheses must always be present.
  80. If there are no parameters, an empty parameter list of () should be
  81. used. Parameter names are not case sensitive.
  82.  
  83. Each parameter is an IN parameter by default. To specify otherwise for
  84. a parameter, use the keyword OUT or INOUT before the parameter name.
  85.  
  86. *Note*:
  87.  
  88. Specifying a parameter as IN, OUT, or INOUT is valid only for a
  89. PROCEDURE. For a FUNCTION, parameters are always regarded as IN
  90. parameters.
  91.  
  92. An IN parameter passes a value into a procedure. The procedure might
  93. modify the value, but the modification is not visible to the caller
  94. when the procedure returns. An OUT parameter passes a value from the
  95. procedure back to the caller. Its initial value is NULL within the
  96. procedure, and its value is visible to the caller when the procedure
  97. returns. An INOUT parameter is initialized by the caller, can be
  98. modified by the procedure, and any change made by the procedure is
  99. visible to the caller when the procedure returns.
  100.  
  101. For each OUT or INOUT parameter, pass a user-defined variable in the
  102. CALL statement that invokes the procedure so that you can obtain its
  103. value when the procedure returns. If you are calling the procedure from
  104. within another stored procedure or function, you can also pass a
  105. routine parameter or local routine variable as an OUT or INOUT
  106. parameter. If you are calling the procedure from within a trigger, you
  107. can also pass NEW.col_name as an OUT or INOUT parameter.
  108.  
  109. Routine parameters cannot be referenced in statements prepared within
  110. the routine; see
  111. http://dev.mysql.com/doc/refman/8.0/en/stored-program-restrictions.html
  112. .
  113.  
  114. The following example shows a simple stored procedure that uses an OUT
  115. parameter:
  116.  
  117. mysql> delimiter //
  118.  
  119. mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
  120. -> BEGIN
  121. -> SELECT COUNT(*) INTO param1 FROM t;
  122. -> END//
  123. Query OK, 0 rows affected (0.00 sec)
  124.  
  125. mysql> delimiter ;
  126.  
  127. mysql> CALL simpleproc(@a);
  128. Query OK, 0 rows affected (0.00 sec)
  129.  
  130. mysql> SELECT @a;
  131. +------+
  132. | @a |
  133. +------+
  134. | 3 |
  135. +------+
  136. 1 row in set (0.00 sec)
  137.  
  138. The example uses the mysql client delimiter command to change the
  139. statement delimiter from ; to // while the procedure is being defined.
  140. This enables the ; delimiter used in the procedure body to be passed
  141. through to the server rather than being interpreted by mysql itself.
  142. See
  143. http://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html.
  144.  
  145. The RETURNS clause may be specified only for a FUNCTION, for which it
  146. is mandatory. It indicates the return type of the function, and the
  147. function body must contain a RETURN value statement. If the RETURN
  148. statement returns a value of a different type, the value is coerced to
  149. the proper type. For example, if a function specifies an ENUM or SET
  150. value in the RETURNS clause, but the RETURN statement returns an
  151. integer, the value returned from the function is the string for the
  152. corresponding ENUM member of set of SET members.
  153.  
  154. The following example function takes a parameter, performs an operation
  155. using an SQL function, and returns the result. In this case, it is
  156. unnecessary to use delimiter because the function definition contains
  157. no internal ; statement delimiters:
  158.  
  159. mysql> CREATE FUNCTION hello (s CHAR(20))
  160. mysql> RETURNS CHAR(50) DETERMINISTIC
  161. -> RETURN CONCAT('Hello, ',s,'!');
  162. Query OK, 0 rows affected (0.00 sec)
  163.  
  164. mysql> SELECT hello('world');
  165. +----------------+
  166. | hello('world') |
  167. +----------------+
  168. | Hello, world! |
  169. +----------------+
  170. 1 row in set (0.00 sec)
  171.  
  172. Parameter types and function return types can be declared to use any
  173. valid data type. The COLLATE attribute can be used if preceded by the
  174. CHARACTER SET attribute.
  175.  
  176. The routine_body consists of a valid SQL routine statement. This can be
  177. a simple statement such as SELECT or INSERT, or a compound statement
  178. written using BEGIN and END. Compound statements can contain
  179. declarations, loops, and other control structure statements. The syntax
  180. for these statements is described in
  181. http://dev.mysql.com/doc/refman/8.0/en/sql-syntax-compound-statements.h
  182. tml.
  183.  
  184. MySQL permits routines to contain DDL statements, such as CREATE and
  185. DROP. MySQL also permits stored procedures (but not stored functions)
  186. to contain SQL transaction statements such as COMMIT. Stored functions
  187. may not contain statements that perform explicit or implicit commit or
  188. rollback. Support for these statements is not required by the SQL
  189. standard, which states that each DBMS vendor may decide whether to
  190. permit them.
  191.  
  192. Statements that return a result set can be used within a stored
  193. procedure but not within a stored function. This prohibition includes
  194. SELECT statements that do not have an INTO var_list clause and other
  195. statements such as SHOW, EXPLAIN, and CHECK TABLE. For statements that
  196. can be determined at function definition time to return a result set, a
  197. Not allowed to return a result set from a function error occurs
  198. (ER_SP_NO_RETSET). For statements that can be determined only at
  199. runtime to return a result set, a PROCEDURE %s can't return a result
  200. set in the given context error occurs (ER_SP_BADSELECT).
  201.  
  202. USE statements within stored routines are not permitted. When a routine
  203. is invoked, an implicit USE db_name is performed (and undone when the
  204. routine terminates). The causes the routine to have the given default
  205. database while it executes. References to objects in databases other
  206. than the routine default database should be qualified with the
  207. appropriate database name.
  208.  
  209. For additional information about statements that are not permitted in
  210. stored routines, see
  211. http://dev.mysql.com/doc/refman/8.0/en/stored-program-restrictions.html
  212. .
  213.  
  214. For information about invoking stored procedures from within programs
  215. written in a language that has a MySQL interface, see [HELP CALL].
  216.  
  217. MySQL stores the sql_mode system variable setting in effect when a
  218. routine is created or altered, and always executes the routine with
  219. this setting in force, regardless of the current server SQL mode when
  220. the routine begins executing.
  221.  
  222. The switch from the SQL mode of the invoker to that of the routine
  223. occurs after evaluation of arguments and assignment of the resulting
  224. values to routine parameters. If you define a routine in strict SQL
  225. mode but invoke it in nonstrict mode, assignment of arguments to
  226. routine parameters does not take place in strict mode. If you require
  227. that expressions passed to a routine be assigned in strict SQL mode,
  228. you should invoke the routine with strict mode in effect.
  229.  
  230. URL: http://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
  231.  
  232. mysql>

查看创建存储过程的帮助信息( mysql> ? CREATE PROCEDURE)

2>.CREATE FUNCTION用来创建函数

  1. mysql> ? CREATE FUNCTION
  2. Name: 'CREATE FUNCTION'
  3. Description:
  4. The CREATE FUNCTION statement is used to create stored functions and
  5. user-defined functions (UDFs):
  6.  
  7. o For information about creating stored functions, see [HELP CREATE
  8. PROCEDURE].
  9.  
  10. o For information about creating user-defined functions, see [HELP
  11. CREATE FUNCTION UDF].
  12.  
  13. URL: http://dev.mysql.com/doc/refman/8.0/en/create-function.html
  14.  
  15. mysql>
  16. mysql>
  17. mysql>

查看创建函数的帮助信息(mysql> ? CREATE FUNCTION)

3>.关键词说明

  1. 1>.函数与存储过程最大的区别就是函数调用有返回值,调用存储过程用call语句,而调用函数就直接引用函数名+参数即可!
  2.  
  3. 2>.DefinerSQL SECURITY子句指定安全环境
  4. DefinderMySQL的特殊的访问控制手段,当数据库当前没有这个用户权限时,执行存储过程可能会报错。
  5. SQL SECURITY的值决定了调用存储过程的方式,其值为:definer(默认)或者invoker,详细说明如下:
  6. definer:在执行存储过程前验证definer对应的用户,如:yinzhengjie@127.0.0.1是否存在,以及是否具有执行存储过程的权限,若没有则报错。
  7. invoker:在执行存储过程时判断inovker即调用该存储过程的用户是否有相应权限,若没有则报错。
  8.  
  9. 3>.IN,OUT,INOUT三个参数前的关键词只适用于存储过程,对函数而言所有的参数默认都是输入参数。
  10.  
  11. 4>.IN输入参数用于把数值传入到存储过程中;OUT输出参数将数值传递到调用者,初始值是NULL;INOUT输入输出参数把数据传入到存储过程,在存储过程中修改之后再传递到调用者。
  12.  
  13. 5>.DELIMITER命令是改变语句的结束符,MySQL默认的结束符为分号(”;”),由于PROCEDUREFUNCTION 的分号(”;”)并不代表创建的结束,所以要替换成另外的结束符以便表示创建的结束。
  14.  
  15. 6>.Rontine_body 子句可以包含一个简单的SQL语句,也可以包含多个 SQL语句,通过begin...end将这多个SQL语句包含在一起
  16.  
  17. 7>.MySQL存储过程和函数中也可以包含类似createdropDDL语句
  18.  
  19. 8>.Comment子句用来写入对存储过程和函数的注释
  20.  
  21. 9>.Language子句用来表示此存储过程和函数的创建语言
  22.  
  23. 10>.存储过程和函数被标注为deterministic表明当输入相同的参数是会 返回相同的结果,反之如果是not deterministic则表示相同参数不 会是相同结果,默认是not deterministic 

  24.  
  25. 11>.相关属性短语只有咨询含义,并不是强制性的约束Contains sql表明此存储过程或函数不包含读或者写数据的语句,这是默认属性
  26. NO SQL表示此存储过程或函数不包含SQL语句
  27. Reads sql data表示此存储过程包含诸如select的查询数据的语句,但不包含插入或删除数据的语句
  28. Modifies sql data表示此存储过程包含插入或删除数据的语句
  29.  
  30. 12>.Drop procedure/function语句用来删除指定名称的存储过程或函数
  31.  
  32. 13>.IF EXISTS关键词用来避免在删除一个本身不存在的存储过程或函数时,MySQL返回错误
  33.  
  34. 14>.BEGIN...END语句通常出现在存储过程、函数和触发器中,其中可 以包含一个或多个语句,每个语句用分号(";")隔开

4>.案例展示

  创建存储过程案例一:

  1. mysql> SELECT DATABASE();
  2. +-------------+
  3. | DATABASE() |
  4. +-------------+
  5. | yinzhengjie |
  6. +-------------+
  7. row in set (0.00 sec)
  8.  
  9. mysql>
  10. mysql> SHOW TABLES;
  11. +-----------------------+
  12. | Tables_in_yinzhengjie |
  13. +-----------------------+
  14. | course |
  15. | score_graph |
  16. | student |
  17. | teacher |
  18. | view_teacher |
  19. +-----------------------+
  20. rows in set (0.00 sec)
  21.  
  22. mysql>
  23. mysql> SELECT * FROM teacher;
  24. +----+-----------+-----------+
  25. | id | name | course_id |
  26. +----+-----------+-----------+
  27. | 1 | 谢霆锋 | 11 |
  28. | 2 | 周杰伦 | 1 |
  29. | 3 | 蔡依林 | 13 |
  30. | 4 | 杨幂 | 2 |
  31. | 5 | 胡歌 | 12 |
  32. | 6 | 刘德华 | 3 |
  33. | 7 | 张学友 | 10 |
  34. | 8 | 郭德纲 | 4 |
  35. | 9 | 林俊杰 | 9 |
  36. +----+-----------+-----------+
  37. rows in set (0.00 sec)
  38.  
  39. mysql>
  40. mysql> DELIMITER //
  41. mysql>
  42. mysql> CREATE PROCEDURE demo(IN args1 int,OUT args2 INT)
  43. -> BEGIN
  44. -> SELECT COUNT(*) INTO args2 FROM teacher where id> args1;
  45. -> END//
  46. Query OK, 0 rows affected (0.01 sec)
  47.  
  48. mysql>
  49. mysql> DELIMITER ;
  50. mysql>
  51. mysql>

创建存储过程案例展示

  1. mysql> CALL demo2(2,@res);
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. mysql>
  5. mysql> SELECT @res;
  6. +------+
  7. | @res |
  8. +------+
  9. | 7 |
  10. +------+
  11. 1 row in set (0.00 sec)
  12.  
  13. mysql>

调用存储过程案例展示

  创建存储过程案例二:

  1. [root@node110 ~]# mysql -uroot -pyinzhengjie
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 18
  5. Server version: 8.0.14 MySQL Community Server - GPL
  6.  
  7. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  15. mysql>
  16. mysql> CREATE USER 'jason'@'node110.yinzhengjie.org.cn' IDENTIFIED BY 'yinzhengjie';
  17. Query OK, 0 rows affected (0.01 sec)
  18.  
  19. mysql>
  20. mysql> SELECT Host,User FROM mysql.user WHERE Host = 'node110.yinzhengjie.org.cn';
  21. +----------------------------+-------+
  22. | Host | User |
  23. +----------------------------+-------+
  24. | node110.yinzhengjie.org.cn | jason |
  25. +----------------------------+-------+
  26. 1 row in set (0.00 sec)
  27.  
  28. mysql>
  29. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  30. +------------------------------------------------------------+
  31. | Grants for jason@node110.yinzhengjie.org.cn |
  32. +------------------------------------------------------------+
  33. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  34. +------------------------------------------------------------+
  35. 1 row in set (0.00 sec)
  36.  
  37. mysql>
  38. mysql> GRANT ALL PRIVILEGES ON yinzhengjie.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION;
  39. Query OK, 0 rows affected (0.01 sec)
  40.  
  41. mysql>
  42. mysql> SHOW GRANTS FOR 'jason'@'node110.yinzhengjie.org.cn';
  43. +---------------------------------------------------------------------------------------------------+
  44. | Grants for jason@node110.yinzhengjie.org.cn |
  45. +---------------------------------------------------------------------------------------------------+
  46. | GRANT USAGE ON *.* TO `jason`@`node110.yinzhengjie.org.cn` |
  47. | GRANT ALL PRIVILEGES ON `yinzhengjie`.* TO `jason`@`node110.yinzhengjie.org.cn` WITH GRANT OPTION |
  48. +---------------------------------------------------------------------------------------------------+
  49. 2 rows in set (0.00 sec)
  50.  
  51. mysql>
  52. [root@node110 ~]# hostname
  53. node110.yinzhengjie.org.cn
  54. [root@node110 ~]#
  55. [root@node110 ~]# mysql -h node110.yinzhengjie.org.cn -ujason -pyinzhengjie
  56. mysql: [Warning] Using a password on the command line interface can be insecure.
  57. Welcome to the MySQL monitor. Commands end with ; or \g.
  58. Your MySQL connection id is 23
  59. Server version: 8.0.14 MySQL Community Server - GPL
  60.  
  61. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  62.  
  63. Oracle is a registered trademark of Oracle Corporation and/or its
  64. affiliates. Other names may be trademarks of their respective
  65. owners.
  66.  
  67. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  68.  
  69. mysql> SHOW DATABASES;
  70. +--------------------+
  71. | Database |
  72. +--------------------+
  73. | information_schema |
  74. | yinzhengjie |
  75. +--------------------+
  76. 2 rows in set (0.00 sec)
  77.  
  78. mysql>
  79. mysql> USE yinzhengjie
  80. Reading table information for completion of table and column names
  81. You can turn off this feature to get a quicker startup with -A
  82.  
  83. Database changed
  84. mysql>
  85. mysql> SHOW TABLES
  86. -> ;
  87. +-----------------------+
  88. | Tables_in_yinzhengjie |
  89. +-----------------------+
  90. | course |
  91. | score_graph |
  92. | student |
  93. | teacher |
  94. | view_teacher |
  95. +-----------------------+
  96. 5 rows in set (0.00 sec)
  97.  
  98. mysql>
  99. mysql> SELECT * FROM teacher;
  100. +----+-----------+-----------+
  101. | id | name | course_id |
  102. +----+-----------+-----------+
  103. | 1 | 谢霆锋 | 11 |
  104. | 2 | 周杰伦 | 1 |
  105. | 3 | 蔡依林 | 13 |
  106. | 4 | 杨幂 | 2 |
  107. | 5 | 胡歌 | 12 |
  108. | 6 | 刘德华 | 3 |
  109. | 7 | 张学友 | 10 |
  110. | 8 | 郭德纲 | 4 |
  111. | 9 | 林俊杰 | 9 |
  112. | 16 | Jason Yin | 8 |
  113. +----+-----------+-----------+
  114. 10 rows in set (0.00 sec)
  115.  
  116. mysql>

创建存储过程案例展示二之环境准备(创建测试用户并授权)

  1. mysql> SELECT VERSION();
  2. +-----------+
  3. | VERSION() |
  4. +-----------+
  5. | 8.0.14 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>
  10. mysql>
  11. mysql> SELECT USER();
  12. +----------------------------------+
  13. | USER() |
  14. +----------------------------------+
  15. | jason@node110.yinzhengjie.org.cn |
  16. +----------------------------------+
  17. 1 row in set (0.00 sec)
  18.  
  19. mysql>
  20. mysql> SELECT DATABASE();
  21. +-------------+
  22. | DATABASE() |
  23. +-------------+
  24. | yinzhengjie |
  25. +-------------+
  26. 1 row in set (0.00 sec)
  27.  
  28. mysql>
  29. mysql>
  30. mysql> DELIMITER //
  31. mysql>
  32. mysql> CREATE PROCEDURE myproc(IN args1 INT,OUT args2 INT)
  33. ->
  34. -> BEGIN
  35. ->
  36. -> SELECT COUNT(*) INTO args2 FROM teacher WHERE course_id > args1;
  37. ->
  38. -> END//
  39. Query OK, 0 rows affected (0.00 sec)
  40.  
  41. mysql>
  42. mysql> DELIMITER ;
  43. mysql>

创建存储过程案例展示二

  1. mysql> SELECT VERSION();
  2. +-----------+
  3. | VERSION() |
  4. +-----------+
  5. | 8.0.14 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>
  10. mysql>
  11. mysql> SELECT DATABASE();
  12. +-------------+
  13. | DATABASE() |
  14. +-------------+
  15. | yinzhengjie |
  16. +-------------+
  17. 1 row in set (0.00 sec)
  18.  
  19. mysql>
  20. mysql> SELECT USER();
  21. +----------------------------------+
  22. | USER() |
  23. +----------------------------------+
  24. | jason@node110.yinzhengjie.org.cn |
  25. +----------------------------------+
  26. 1 row in set (0.00 sec)
  27.  
  28. mysql>
  29. mysql> CALL myproc(5,@res);
  30. Query OK, 1 row affected (0.00 sec)
  31.  
  32. mysql>
  33. mysql> SELECT @res;
  34. +------+
  35. | @res |
  36. +------+
  37. | 6 |
  38. +------+
  39. 1 row in set (0.00 sec)
  40.  
  41. mysql>
  42. mysql> SELECT COUNT(*) FROM teacher WHERE course_id > 5;
  43. +----------+
  44. | COUNT(*) |
  45. +----------+
  46. | 6 |
  47. +----------+
  48. 1 row in set (0.00 sec)
  49.  
  50. mysql>
  51. mysql>
  52. mysql> SELECT * FROM teacher WHERE course_id > 5;
  53. +----+-----------+-----------+
  54. | id | name | course_id |
  55. +----+-----------+-----------+
  56. | 16 | Jason Yin | 8 |
  57. | 9 | 林俊杰 | 9 |
  58. | 7 | 张学友 | 10 |
  59. | 1 | 谢霆锋 | 11 |
  60. | 5 | 胡歌 | 12 |
  61. | 3 | 蔡依林 | 13 |
  62. +----+-----------+-----------+
  63. 6 rows in set (0.00 sec)
  64.  
  65. mysql>
  66. mysql>

调用存储过程案例展示二

  1. mysql> SELECT VERSION();
  2. +-----------+
  3. | VERSION() |
  4. +-----------+
  5. | 8.0.14 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>
  10. mysql> SELECT DATABASE();
  11. +-------------+
  12. | DATABASE() |
  13. +-------------+
  14. | yinzhengjie |
  15. +-------------+
  16. 1 row in set (0.00 sec)
  17.  
  18. mysql>
  19. mysql> SELECT USER();
  20. +----------------+
  21. | USER() |
  22. +----------------+
  23. | root@localhost |
  24. +----------------+
  25. 1 row in set (0.00 sec)
  26.  
  27. mysql>
  28. mysql> CALL myproc(5,@res);
  29. Query OK, 1 row affected (0.01 sec)
  30.  
  31. mysql>
  32. mysql> SELECT @res;
  33. +------+
  34. | @res |
  35. +------+
  36. | 6 |
  37. +------+
  38. 1 row in set (0.00 sec)
  39.  
  40. mysql>
  41. mysql>
  42. mysql>
  43. mysql> DROP USER jason@node110.yinzhengjie.org.cn;
  44. Query OK, 0 rows affected (0.00 sec)
  45.  
  46. mysql>
  47. mysql> CALL myproc(5,@res);
  48. ERROR 1449 (HY000): The user specified as a definer ('jason'@'node110.yinzhengjie.org.cn') does not exist
  49. mysql>
  50. mysql> ALTER PROCEDURE myproc SQL SECURITY INVOKER;
  51. Query OK, 0 rows affected (0.01 sec)
  52.  
  53. mysql>
  54. mysql> CALL myproc(5,@res);
  55. Query OK, 1 row affected (0.00 sec)
  56.  
  57. mysql>
  58. mysql> SELECT @res;
  59. +------+
  60. | @res |
  61. +------+
  62. | 6 |
  63. +------+
  64. 1 row in set (0.00 sec)
  65.  
  66. mysql>

调用存储过程案例展示二(删除创建存储过程的用户时无法调用的解决方案,使用SQL SECURITY INVOKER)

  创建函数案例一:

  1. mysql> set global log_bin_trust_function_creators=TRUE;
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql>
  5. mysql> CREATE FUNCTION hello(str CHAR(30))
  6. -> RETURNS CHAR(50)
  7. -> RETURN CONCAT('Hello,',str,'!');
  8. Query OK, 0 rows affected (0.00 sec)
  9.  
  10. mysql>

创建函数案例展示

  1. mysql> set global log_bin_trust_function_creators=TRUE;
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql>
  5. mysql> CREATE FUNCTION hello(str CHAR(30))
  6. -> RETURNS CHAR(50)
  7. -> RETURN CONCAT('Hello,',str,'!');
  8. Query OK, 0 rows affected (0.00 sec)
  9.  
  10. mysql>
  11. mysql> SELECT hello('尹正杰');
  12. +--------------------+
  13. | hello('尹正杰') |
  14. +--------------------+
  15. | Hello,尹正杰! |
  16. +--------------------+
  17. 1 row in set, 2 warnings (0.00 sec)
  18.  
  19. mysql>
  20. mysql>

调用函数案例展示

  创建函数案例二:

  1. mysql> SELECT * FROM yinzhengjie.teacher;
  2. +----+-----------+-----------+
  3. | id | name | course_id |
  4. +----+-----------+-----------+
  5. | 1 | 谢霆锋 | 11 |
  6. | 2 | 周杰伦 | 1 |
  7. | 3 | 蔡依林 | 13 |
  8. | 4 | 杨幂 | 2 |
  9. | 5 | 胡歌 | 12 |
  10. | 6 | 刘德华 | 3 |
  11. | 7 | 张学友 | 10 |
  12. | 8 | 郭德纲 | 4 |
  13. | 9 | 林俊杰 | 9 |
  14. | 16 | 尹正杰 | 8 |
  15. +----+-----------+-----------+
  16. 10 rows in set (0.00 sec)
  17.  
  18. mysql>
  19. mysql> DELIMITER //
  20. mysql> CREATE FUNCTION myfunc(args1 INT)
  21. -> RETURNS INT
  22. -> BEGIN
  23. -> UPDATE yinzhengjie.teacher SET name = 'Jason Yin' WHERE id = args1;
  24. -> SELECT COUNT(*) INTO @res FROM yinzhengjie.teacher WHERE course_id % args1 = 0;
  25. -> RETURN @res;
  26. -> END//
  27. Query OK, 0 rows affected (0.00 sec)
  28.  
  29. mysql>
  30. mysql> DELIMITER ;

创建函数案例展示二

  1. mysql> DELIMITER //
  2. mysql> CREATE FUNCTION myfunc(args1 INT)
  3. -> RETURNS INT
  4. -> BEGIN
  5. -> UPDATE yinzhengjie.teacher SET name = 'Jason Yin' WHERE id = args1;
  6. -> SELECT COUNT(*) INTO @res FROM yinzhengjie.teacher WHERE course_id % args1 = 0;
  7. -> RETURN @res;
  8. -> END//
  9. Query OK, 0 rows affected (0.00 sec)
  10.  
  11. mysql>
  12. mysql> DELIMITER ;
  13. mysql>
  14. mysql>
  15. mysql> SELECT * FROM yinzhengjie.teacher;
  16. +----+-----------+-----------+
  17. | id | name | course_id |
  18. +----+-----------+-----------+
  19. | 1 | 谢霆锋 | 11 |
  20. | 2 | 周杰伦 | 1 |
  21. | 3 | 蔡依林 | 13 |
  22. | 4 | 杨幂 | 2 |
  23. | 5 | 胡歌 | 12 |
  24. | 6 | 刘德华 | 3 |
  25. | 7 | 张学友 | 10 |
  26. | 8 | 郭德纲 | 4 |
  27. | 9 | 林俊杰 | 9 |
  28. | 16 | 尹正杰 | 8 |
  29. +----+-----------+-----------+
  30. rows in set (0.00 sec)
  31.  
  32. mysql>
  33. mysql> SELECT myfunc(16);
  34. +------------+
  35. | myfunc(16) |
  36. +------------+
  37. | 0 |
  38. +------------+
  39. row in set (0.00 sec)
  40.  
  41. mysql>
  42. mysql> SELECT * FROM yinzhengjie.teacher;
  43. +----+-----------+-----------+
  44. | id | name | course_id |
  45. +----+-----------+-----------+
  46. | 1 | 谢霆锋 | 11 |
  47. | 2 | 周杰伦 | 1 |
  48. | 3 | 蔡依林 | 13 |
  49. | 4 | 杨幂 | 2 |
  50. | 5 | 胡歌 | 12 |
  51. | 6 | 刘德华 | 3 |
  52. | 7 | 张学友 | 10 |
  53. | 8 | 郭德纲 | 4 |
  54. | 9 | 林俊杰 | 9 |
  55. | 16 | Jason Yin | 8 |
  56. +----+-----------+-----------+
  57. rows in set (0.01 sec)
  58.  
  59. mysql>
  60. mysql> SELECT COUNT(*) FROM yinzhengjie.teacher WHERE course_id % 16 = 0;
  61. +----------+
  62. | COUNT(*) |
  63. +----------+
  64. | 0 |
  65. +----------+
  66. 1 row in set (0.00 sec)
  67.  
  68. mysql>
  69. mysql> SELECT * FROM yinzhengjie.teacher WHERE course_id % 8 = 0;
  70. +----+-----------+-----------+
  71. | id | name | course_id |
  72. +----+-----------+-----------+
  73. | 16 | Jason Yin | 8 |
  74. +----+-----------+-----------+
  75. 1 row in set (0.00 sec)
  76.  
  77. mysql>

调用函数案例展示二

  删除创建过程案例:

  1. mysql> DROP PROCEDURE demo;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql>
  5. mysql> DROP PROCEDURE demo;
  6. ERROR 1305 (42000): PROCEDURE yinzhengjie.demo does not exist
  7. mysql>
  8. mysql>
  9. mysql>
  10. mysql> DROP PROCEDURE IF EXISTS demo;
  11. Query OK, 0 rows affected, 1 warning (0.00 sec)
  12.  
  13. mysql>
  14. mysql>

mysql> DROP PROCEDURE IF EXISTS demo;

  删除函数案例:

  1. mysql> DROP FUNCTION hello;
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql>
  5. mysql> DROP FUNCTION hello;
  6. ERROR 1305 (42000): FUNCTION yinzhengjie.hello does not exist
  7. mysql>
  8. mysql>
  9. mysql> DROP FUNCTION IF EXISTS hello;
  10. Query OK, 0 rows affected, 1 warning (0.00 sec)
  11.  
  12. mysql>
  13. mysql>

mysql> DROP FUNCTION IF EXISTS hello;

5>.查看已经存在的存储过程或者函数

  1. mysql> SELECT routine_name,routine_type FROM information_schema.routines WHERE routine_schema='course';
  2. +--------------+--------------+
  3. | ROUTINE_NAME | ROUTINE_TYPE |
  4. +--------------+--------------+
  5. | hello | FUNCTION |
  6. | myfunc | FUNCTION |
  7. | demo | PROCEDURE |      
  8. | myproc | PROCEDURE |
  9. +--------------+--------------+
  10. rows in set (0.01 sec)
  11.  
  12. mysql>
  13. mysql>  

二.标签语句

1>.标签语句注意事项

  1. 标签label可以加在begin...end语句以及loop, repeatwhile语句
  2.  
  3. 语句中通过iterateleave来控制流程,iterate表示返回指定标签 位置,leave表示跳出标签

2>.案例展示

三.

四.

五.

SQL语法基础之高级应用的更多相关文章

  1. SQL语法基础之DROP语句

    SQL语法基础之DROP语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看DROP帮助信息 mysql> ? DROP Many help items for yo ...

  2. SQL语法基础之SELECT

    SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...

  3. SQL语法基础之ALTER语句

    SQL语法基础之ALTER语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看ALTER的帮助信息 mysql> ? ALTER Many help items fo ...

  4. SQL语法基础之INSEART语句

    SQL语法基础之INSEART语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看INSERT方法的帮助信息 mysql> ? INSERT ...

  5. SQL语法基础之UPDATE语句

    SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...

  6. SQL语法基础之DELETE语句

    SQL语法基础之DELETE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看DELETE的帮助信息 mysql> ? DELETE Na ...

  7. SQL语法基础之CREATE语句

    SQL语法基础之CREATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.使用“?”来查看MySQL命令的帮助信息 mysql> ? CR ...

  8. oracle PL/SQL语法基础

    目录 数据类型 定义变量 PL/SQL控制结构 参考资料 Oracle10g数据类型总结 PL/SQL之基础篇 数据类型 学习总结 字符类型 char.nchar.varchar.nvarchar:有 ...

  9. 读书笔记(06) - 语法基础 - JavaScript高级程序设计

    写在开头 本篇是小红书笔记的第六篇,也许你会奇怪第六篇笔记才写语法基础,笔者是不是穿越了. 答案当然是没有,笔者在此分享自己的阅读心得,不少人翻书都是从头开始,结果永远就只在前几章. 对此,笔者换了随 ...

随机推荐

  1. BZOJ4128Matrix——hash+矩阵乘法+BSGS

    题目描述 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) 输入 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * n的矩阵A.接下来一个n * n的矩阵B 输出 输出 ...

  2. mpvue——引入antv-F2图表

    踩坑中~ 官方文档 https://www.yuque.com/antv/f2/intro 毕竟不像echarts接触过,所以还是先看看文档较好 github https://github.com/s ...

  3. Elasticsearch 聚合统计与SQL聚合统计语法对比(一)

    Es相比关系型数据库在数据检索方面有着极大的优势,在处理亿级数据时,可谓是毫秒级响应,我们在使用Es时不仅仅进行简单的查询,有时候会做一些数据统计与分析,如果你以前是使用的关系型数据库,那么Es的数据 ...

  4. Treap树 笔记

    预备知识:二叉查找树.堆(heap).平衡二叉树(AVL)的基本操作(左旋右旋) 定义: Treap.平衡二叉树.Tree+Heap.树堆. 每个结点两个键值(key.priority). 性质1. ...

  5. [WC2005]双面棋盘(并查集+分治)

    题目描述 题解 唉,还是码力不行,写了一个多小时发现想错了又重构了一个多小时. 这道题意图很显然,动态维护联通块,有一个经典做法就是用LCT维护按照删除时间维护的最大生成树. 网上还有一种神奇的做法, ...

  6. 【mysql】 mysql忘记密码

    先关闭mysql服务 [root@localhost ~]# service mysql status Redirecting to /bin/systemctl status mysql.servi ...

  7. 微信支付退款(PHP后端)

    应用场景 当交易发生之后一段时间内,由于买家或者卖家的原因需要退款时,卖家可以通过退款接口将支付款退还给买家,微信支付将在收到退款请求并且验证成功之后,按照退款规则将支付款按原路退到买家帐号上. 微信 ...

  8. Windows cmd命令

    运行操作 CMD命令锦集       1. gpedit.msc-----组策略 2. sndrec32-------录音机 3. Nslookup-------IP地址侦测器 ,是一个监测网络中DN ...

  9. Eureka

    Consul vs. Eureka Eureka is a service discovery tool. The architecture is primarily client/server, w ...

  10. css border 三角形

    当元素的宽高都为0时,只写border,就会发现形成的正方形有4个三角形组成. .triangle { width: 0px; height: 0px; border: 20px solid tran ...