Sql must know and know (fifth edition) learning summary (three)-mysql view, stored procedure, transaction processing, cursor, trigger summary

table of Contents

One: View

1.1 Advantages of using views:

1.2. Create a view

Two: stored procedure

2.1 Advantages of stored procedures:

2.2 Create a stored procedure

2.3 execute a stored procedure

2.4 Delete stored procedures

2.5 Check the stored procedure

2.6 Show the results of the stored procedure

Three: cursor

Four transaction processing

4.1 The role of transaction processing:

4.2 Several terms of transaction processing

 Five flip-flops


One: View

1.1 Advantages of using views:

  • Reuse SQL statements.
  • Simplify complex SQL operations. After writing a query, you can easily reuse it without knowing its basic query details.
  • Use parts of the table instead of the entire table.
  • Protect data. You can grant users access to specific parts of the table instead of access to the entire table.
  • Change the data format and presentation. The view can return data that is different from the representation and format of the underlying table.
  • Simplify complex connections with views

【note】:

1) After creating view functions, you can use them in the same way as tables. You can perform SELECT operations, filter, sort data, combine views with other views or tables, and even add and update data.

2) Know that the view is only used to view the data stored elsewhere. It does not contain data. The data is retrieved from other tables. When the data in the table is changed or added, the view will return the changed data.

3) Generally, the view should be used for retrieval (SELECT statement) and not for update (INSERT, UPDATE, DELETE).

1.2. Create a view

CREATE VIEW Statement to create a view. To delete the view  DROP VIEW viewname;

CREATE VIEW ProductCustomers AS
SELECT cust_name, cust_contact, prod_id
FROM Customers, Orders, OrderItems
WHERE Customers.cust_id = Orders.cust_id
AND OrderItems.order_num = Orders.order_num;

Retrieve data from the ProductCustomers table

SELECT cust_name, cust_contact
FROM ProductCustomers
WHERE prod_id = 'RGAN01';

Use the view to reformat the retrieved data

SELECT CONCAT(vend_name, ' (', vend_country, ')') 
AS vend_title 
FROM Vendors
ORDER BY vend_name;

Use views to filter data

CREATE VIEW CustomerEMAIList AS
SELECT cust_id, cust_name, cust_email
FROM Customers
WHERE cust_email IS NOT NULL;

Two: stored procedure

2.1 Advantages of stored procedures:

A stored procedure is a collection of one or more MySQL statements saved for future use.

There are three main benefits of using stored procedures:  simple, safe, and high-performance  .

1) Simplify complex operations by encapsulating the processing in easy-to-use units;

2) To prevent errors, since a series of processing steps are not required to be established repeatedly, this ensures the integrity of the data;

3) Simplify the management of changes. If there are changes in table names, column names, or business logic, you only need to change the code of the stored procedure, and the people who use it don't even need to know these changes;

4) Improve performance, using stored procedures is faster than using separate SQL statements.

There are also some disadvantages to using stored procedures:

First of all, generally speaking, the writing of stored procedures is more complex than basic SQL statements, and writing stored procedures requires higher skills.

More experience. In addition, you need to restrict the creation of stored procedures, allowing users to use stored procedures, but not allowing them to create stored procedures.

2.2 Create a stored procedure

create procedure productpricing(
        out pl decimal(8,2),
        out ph decimal(8,2),
        out pa decimal(8,2)
)
begin
         select min(prod_price)  into pl  from products;
         select max(prod_price)  into ph  from products; 
         select avg(prod_price)  into pa  from products;
end;

2.3 execute a stored procedure

CALL productpricing(@pricelow, @pricehigh, @priceaverage);

2.4 Delete stored procedures

drop procedure productpricing;

2.5 Check the stored procedure

show create procedure productpricing

2.6 Show the results of the stored procedure

show @priceaverage;

Three: cursor

3.1 The definition of the cursor:

Is a database query stored on the MySQL server, it is not a SELECT statement ,

It is the result set retrieved by the sentence. After storing the cursor, the application can scroll or browse the data as needed.

It is mainly used in interactive applications, where users need to scroll through the data on the screen and browse or make changes to the data.

3.2 Use cursors

(1) Before the cursor can be used, it must be declared. This process does not actually retrieve data.

Just define the SELECT statement to be used. The cursor is created with the DECLARE statement , such as:

create procedure processorders()
begin
DECLARE ordernumbers cursor
for
select order_num from orders;
end;

(2) Open the cursor

open ordernumbers;

(3) For the cursor filled with data, take out (retrieve) each row as needed

    需要用fetch检索当前行的order_num列到一个名为o的局部声明的变量中

create procedure processorders()
begin 
         declare o int;
         declare ordernumbers cursor
         for
         select order_num from orders;
         open ordernumbers;
         FETCH ordernumbers into o;
         close ordernumbers;
end;

(4) At the end of the use of the cursor, the cursor must be closed.

close ordernumbers;

Four transaction processing

4.1 The role of transaction processing:

  It can be used to maintain the integrity of the data, to ensure that the SQL operation is either completely executed or not executed at all. If an error occurs, it will be undone.

  • Ensure the integrity of the data.
  • Ensure that the data is not affected by external influences.

4.2 Several terms of transaction processing

  • Transaction (transaction) A set of SQL statements
  • Rollback (rollback) undo the process of executing SQL statements
  • Submit (commit) will write the executed SQL statement to the database table
  • Savepoint (savepoint) Temporary storage point for release and return

 Five flip-flops

   Triggers are special stored procedures that are automatically executed when specific database activities occur.

Triggers can be associated with insert, update, delete operations on a specific table

Unlike stored procedures, stored procedures are simply stored sql statements, and triggers are associated with a single table.

Triggers can be executed before or after a specific execution operation .

Guess you like

Origin blog.csdn.net/yezonghui/article/details/113036409