当前位置:网站首页>MySQL Chapter 6 Summary

MySQL Chapter 6 Summary

2022-06-26 10:21:00 m0_ sixty-one million nine hundred and sixty-one thousand eight

Create stored procedure .
have access to CREATE PROCEDURE Statement to create a stored procedure , The syntax is as follows :

CREATE PROCEDURE < The process of > ( [ Process parameters [,…] ] ) < The process of body > [ Process parameters [,…] ] Format [ IN | OUT
| INOUT ] < Parameter name > < type >

The grammar is as follows :

The process of
The name of the stored procedure , By default, it is created in the current database . If you need to create stored procedures in a specific database , Add the name of the database before the name , namely db_name.sp_name.
It should be noted that , Names should be avoided as much as possible MySQL The same name as the built-in function , Otherwise, there will be mistakes .
2) Process parameters
Parameter list of stored procedure . among ,< Parameter name > For the parameter name ,< type > Is the type of parameter ( It can be anything that works MySQL data type ). When there are multiple parameters , The parameter list is separated from each other by commas . Stored procedures can have no parameters ( At this point, the name of the stored procedure still needs to be followed by a pair of parentheses ), There can be 1 Two or more parameters .

MySQL Stored procedures support three types of parameters , Input parameters 、 Output parameters and inputs / Output parameters , Use them separately IN、OUT and INOUT Three keywords identify . among , Input parameters can be passed to a stored procedure , The output parameter is used when the stored procedure needs to return an operation result , And input / The output parameter can act as either an input parameter or an output parameter .

It should be noted that , The name of the parameter should not be the same as the column name of the data table , Otherwise, although no error message will be returned , But the stored procedure SQL Statement will treat the parameter name as a column name , This leads to unpredictable results .
3) The process of body
The main part of the stored procedure , Also known as stored procedure body , Contains what must be executed when calling a procedure SQL sentence . This part is based on the keyword BEGIN Start , With keywords END end . If there is only one in the stored procedure body SQL sentence , You can omit BEGIN-END sign .

In the creation of stored procedures , A very important... Is often used MySQL command , namely DELIMITER command , Especially for operating through the command line MySQL Users of the database , It is more important to learn to use this command .

stay MySQL in , The server processes SQL By default, the statement ends with a semicolon . However , When the stored procedure is created , The stored procedure body may contain multiple SQL sentence , these SQL If the statement still ends with a semicolon , that MySQL When processing, the server will start with the first SQL The semicolon at the end of the statement is used as the terminator of the whole program , Instead of dealing with the following... In the stored procedure body SQL sentence , It's obviously not going to work .

To solve the above problems , Usually use DELIMITER Command changes the end command to another character . The syntax is as follows :
DELIMITER $$

The grammar is as follows :
$$ Is a user-defined Terminator , Usually this symbol can be some special symbols , Like two “?” Or two “¥” etc. .
When using DELIMITER On command , Backslashes should be avoided “\” character , Because it is MySQL The escape character of .

stay MySQL The command line client input is as follows SQL sentence .
mysql > DELIMITER ??

Successfully execute this SQL After the statement , Any order 、 The end flag of a statement or program is replaced by two question marks “??” 了 .

If you want to switch back to the default semicolon “;” As an end sign , It's in MySQL The command line client can enter the following statement :
mysql > DELIMITER ;

Be careful :DELIMITER And semicolon “;” There must be a space between . When the stored procedure is created , Must possess CREATE ROUTINE jurisdiction .

1, Create stored procedure

delimiter $

create procedure in_out_procedure(in procedure_id int,out count_number
int) begin select count(*) into count_number from temp_url where
id=procedure_id; end $ delimiter ;

Be careful :delimiter $ Change the separator ,MySQL In the database , Use delimiter Keyword to change the separator , When we are creating triggers or stored procedures , Often used .
2, Execute stored procedures :

call in_out_procedure(1,count_number); MySQL How to call a stored procedure with parameters ? call
in_out_procedure(1,@param1); select @param1;

Be careful , With output out Stored procedure , When we use , You need to get the output first , Here we use parameters param1, It is equivalent to receiving the returned result , Then you can use this return result .

Create a stored procedure that applies cursors
1、 What is a cursor ?

Cursors are like iterators or pointers , It points to the first record in the database , Every time a record is fetched, the cursor moves backward one bit

2、 Cursors 4 Parts of

1、declare Affirming – declare You name cursor for select statement

2、open open --open You name

3、fetch Value --fetch You name into var1,var2,[…]

4、close close –close You name

3、 Cursor to and select Statement with

declare c cursor for select * from user

This sentence means a cursor c Point to select * from user The first record in the query result set ( The record has not been taken from the database , Here you can write logic to decide whether to return the record to the caller , Every time you pick it up , The cursor moves back one bit )

Create and use transactions
A:atomicity Atomicity
C:Consistency Uniformity
I: Isolation Isolation,
D:durabiliity reliability

The basic way to write a transaction
View table engine commands
show engines;

innodb Support transactions ,myisam Unsupported transaction

The opening of business :
begin perhaps start transaction
..

原网站

版权声明
本文为[m0_ sixty-one million nine hundred and sixty-one thousand eight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260930071646.html