TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MySQL Functions

MySQL Functions for beginners and professionals on mysql tutorial, examples, functions, programming, mysql, literals, cursor, procedure, trigger, regexp_like(), regexp_replace operator, regular expression, regexp_instr(), crud etc.

<< Back to MYSQL

MySQL Functions

Creating a function

In MySQL, Function can also be created. A function always returns a value using the return statement. The function can be used in SQL queries.

Syntax

CREATE FUNCTION function_name [ (parameter datatype [, parameter datatype]) ] 
RETURNS return_datatype
BEGIN
Declaration_section
Executable_section
END;

Parameter:

Function_name: name of the function

Parameter: number of parameter. It can be one or more than one.

return_datatype: return value datatype of the function

declaration_section: all variables are declared.

executable_section: code for the function is written here.

Example 1

Step 1: Create database and table.

Database: employee

MySQL Functions

Table 1 : designation

MySQL Functions

Table 2 : staff

MySQL Functions

Step 2: Create a function

Function query:

DELIMITER $$ 
CREATE FUNCTION get_designation_name(d_id INT) RETURNS VARCHAR( 20 ) 
BEGIN 
DECLARE de_name VARCHAR( 20 ) DEFAULT "";
SELECT name INTO de_name FROM designation WHERE id = d_id;
RETURN de_name;
END $$
MySQL Functions

Step 3: Execute the function

Query :

SELECT id, get_designation1(`d_id`) as DESIGNATION, name FROM 'staff'

MySQL Functions

Drop a function

In MySQL Function can also be dropped. When A function id dropped, it is removed from the database.

Syntax:

Drop function [ IF EXISTS ] function_name;

Parameter

function_name: name of the function to be dropped.

Example 1:

drop function get_designation_name;

MySQL Functions
Next TopicMySQL ROLLUP




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf