Data Application Master's SQL Basic Tutorial Sharing 13 - Stored Procedures and Transactions

Storage is not easy

2. Stored procedure

1. What is a stored procedure

【Introduction to knowledge points】

A simple explanation, a stored procedure is to save one or more SQL statements to facilitate repeated use in the future.
Moreover, the stored procedure has a feature, especially in the case of multiple SQL statements, if one statement fails to execute, all will not be executed, and only when all the statements are successfully executed will it pass.

(Access and SQLite do not support stored procedures)

2. Execute storage

【Introduction to knowledge points】

Executing a stored procedure is far more complicated than writing it. Although the statement is simple, the EXECUTE keyword is used in SQL to execute a stored procedure.
EXECUTE receives the stored procedure name and the parameters that need to be passed to it.

EXECUTE stored procedure name
(parameter1,parameter2,...)

 

 

3. Create a stored procedure

【Introduction to knowledge points】

Creating a stored procedure is not a simple matter. If you want to know the details, we recommend that you learn from specific database software.
Here, we just give an example to let everyone have a perceptual understanding of creating stored procedures.

We create a myprocess stored procedure for the student table to add a new student to the student. The passed parameters are ID and SName:

CREATE PROCEDURE myprocess(
ID INT IN,
SName VARCHAR IN)
IS
N_ID INT,
N_Name VARCHAR;
BEGIN
    INSERT INTO student
    VALUES (N_ID,N_Name);
END;

 

 

With the stored procedure created, we can use EXECUTE to execute the stored procedure that inserts the student data:

EXECUTE myprocess(20161555,'Bill');

 

Of course, you can request to add more parameters when creating the stored procedure.

business steward

3. Transaction management

1. What is transaction management

【Introduction to knowledge points】

Transaction actually refers to one or more SQL statements, and transaction management is to manage transactions to maintain the integrity of the database. Simply put, SQL statements are either completely executed or not executed at all, which is Transaction Processing in SQL.
Transaction management has the following keywords:
TRANSACTION, transaction, refers to one or a group of SQL statements;
ROLLBACK, rollback, refers to the process of undoing the specified SQL statement, that is, revocation;
COMMIT, commit, refers to the process of revoking the unexecuted SQL statement The result is written to the database, i.e. changes are kept;
SAVEPOINT, the save point, refers to the placeholder _ set in the transaction to which a rollback can be issued (unlike rolling back the entire transaction).

Different database software controls transaction management in different ways, such as:

-- MySQL
START TRANSACTION
......

-- Oracle
SET TRANSACTION
......

 

2、ROLLBACK

【Introduction to knowledge points】

The ROLLBACK command can be used to roll back and undo SQL statements, but not all SQL statements can be undone.
We can undo INSERT, UPDATE, and DELETE statements, but they are invalid for SELECT statements (because SELECT does not need to be undone), CREATE, and DROP operations.

If we use DELETE to delete a row value in the student table, it can be undone by ROLLBACK:

DELETE FROM student
WHERE ID = 20160014;
ROLLBACK;

 

3. COMMIT and reserved points

【Introduction to knowledge points】

Since the usage of different databases is different, we only make a brief introduction to COMMIT and retention points.
COMMIT is a keyword to ensure the complete execution of data, if we now have such a piece of code

START TRANSACTION
DELETE FROM 表;
DELETE FROM 表2;
COMMIT;

 

The role of COMMIT is that if our first DELETE statement is correct, but the second DELETE statement is wrong, this TRANSACTION will not be executed, indicating that COMMIT will only be true when all statements are correct. Execute, thus protecting the data.

The reserved point, the usage in MySQL is:

SAVEPOINT delete1;

 

Retention points are mostly used for more complex transaction management, that is, adding placeholders (that is, retention points) in the process of transaction processing. If you need to roll back, you can fall back to the retention point we specified. From the perspective of maintaining data, when processing transactions, in principle, the more retention points are set, the better.

If you want to learn more about SQL transaction management, we still recommend starting with a specific database software language.

 

To be continued below. . . . . .

 

Welcome to visit our official website:

http://www.datanew.com/datanew/homepage

http://www.lechuangzhe.com/homepage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326230175&siteId=291194637