MySQL Advanced learning Note five: the creation of MySQL functions! (Video serial Number: Advanced _13)

Source: Internet
Author: User
Tags mysql functions

Knowledge Point six: Creation of MySQL functions (13)

Built-in functions:

Custom functions:

  First, check to see if the function that created the function has been turned on:

SHOW VARIABLES like '%fun% ';

  If the value of the variable is off, you need to turn on

Set global Log_bin_trust_function_creators=1;

  syntax for creating a function:

CREATE function name (variable 1, variable 2 ...)

RETURNS Data types

BEGIN

...... Program code to execute

RETURN data;

END;

1 --Default Data2 CREATE TABLE IF  not EXISTSUserInfo (3Useridint( One) not NULLAuto_incrementKEY,4Usernamevarchar( -)DEFAULT NULL,5Userpwdvarchar( -)DEFAULT NULL,6 );7 8 INSERT  intoUserInfoVALUES(1,'Admin','123'),(2,'Test','Test'),(4,'yy','Ko'),(5,'yy','Ko');
Default Data
1 --13 Creation of functions2 --ask for two numbers and3DELIMITER//4 CREATE  FUNCTIONFun_add (AintBint)5 RETURNS int6 BEGIN7     RETURNA+b;8 END 9 //Ten  One DELIMITER; A SELECTFun_add (1,2); -  -SHOWCREATE FUNCTIONFun_add; the  -  - --User Login Detection -DELIMITER// + CREATE  FUNCTIONFun_login (unameVARCHAR( -), PWDVARCHAR( -)) - RETURNS VARCHAR( -) + BEGIN A     DECLARERowresultint DEFAULT 0; at     SELECT COUNT(*) intoRowresult fromUserInfowhereUsername=uname; -     IFRowresult=0  Then -         RETURN 'user does not exist! '; -     END IF; -     SELECT COUNT(*) intoRowresult fromUserInfowhereUsername=Uname andUserpwd=pwd; -     IFRowresult=0  Then in         RETURN 'Wrong password! '; -     ELSE to     RETURN 'Landing Success! '; +     END IF; - END  the // *  $ DELIMITER;Panax Notoginseng SELECTFun_login ('Admin','123');
creating tests for functions

Management of functions:

To view the functions under the database:

SELECT * from Mysql.func; or SHOW FUNCTION status;

To view function contents:

SHOW CREATE FUNCTION Fun_name;

To delete a function:

DROP FUNCTION IF EXISTS fun_name;

MySQL Advanced learning Note five: the creation of MySQL functions! (Video serial Number: Advanced _13)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.