Use of stored procedures, functions, triggers, and views

Stored procedure

Stored Procedure (Stored Procedure) is a set of SQL statements in the database to complete a specific function. It is stored in the database and is permanently valid after a compilation. The user specifies the name of the stored procedure and gives parameters (optional). carried out

Advantages of stored procedures

  • Pre-compiled SQL to improve execution efficiency
  • The execution logic can be hidden, only the name and parameters are exposed
  • Compared with the program, it is more convenient to modify

Disadvantages of stored procedures

  • As the number of SQL rows increases, the maintenance complexity increases linearly
  • Unable to debug, high risk during iteration

format

delimiter //

drop procedure if exists p1;

//

create procedure p1()

begin

  declare var1 int default 0;

    set var1 = 1;

  while var1 <700000 DO

   set var1 = var1 + 4;

   INSERT INTO oyk_record_charging (organization_id,account_uid, cabinet_uid,charging_type, charging_status, return_battery_id, get_battery_id, return_box_id, get_box_id)

    VALUES('23',REPLACE(UUID(),'-',''),REPLACE(UUID(),'-',''),'1','-1','G0014802512BLD0190703001','G0014802512BLD0190703002','184','183');

  end while;

  select var1;

end;

//           

 

call p1();

//

=============================

#Stored procedure with parameters

drop procedure if exists p2;

//

create procedure p2(in a int, out b int)

begin

set b = a*a;

end;

//

 

call p2(5, @v1); //

select @v1; //

function

create function fn1(a int) - function can be replaced, stored procedure cannot

returns varchar(255) - defines the return type

begin

declare r int;

set r = a*a;

return r;

end;

The difference between stored procedures and functions:

Function must have a return value

 Functions are used for reuse (called by various stored procedures, SELECT statements), stored procedures are used to solve a specific problem. (For example, this month's product sales data)

view:

而一个或多个表依照某个条件组合而成的结果集 ; 视图是虚拟的内存表

use:

create or replace view  v1

as

check sentence

trigger:

Triggers are database objects related to the table, which are triggered when the defined conditions are met

use:

delimiter //

 

create trigger tb1_trigger_insert

before insert on tb1 for each row

begin

set new.created=now();

set new.updated=now();

end;

//

Actual use in the project:

Stored procedure:

In the previous project, I wrote a stored procedure for the test to insert a large amount of data for testing.

delimiter //

drop procedure if exists p1;

//

create procedure p1()

begin

declare var1 int default 0;

set var1 = 1;

while var1 <700000 DO

set var1 = var1 + 4;

INSERT INTO oyk_record_charging (organization_id,account_uid, cabinet_uid,charging_type, charging_status, return_battery_id, get_battery_id, return_box_id, get_box_id)

VALUES('23',REPLACE(UUID(),'-',''),REPLACE(UUID(),'-',''),'1','-1','G0014802512BLD0190703001','G0014802512BLD0190703002','184','183');

end while;

select var1;

end;

//    

call p1();

//       

function:

Used to create custom uuid

CREATE OR REPLACE
FUNCTION "RPTEYADM"."GET_UUID" AS
BEGIN

 guid: = sys_guid ();

RETURN

 substr(guid,1,8)||'-'||substr(guid,9,4)||'-'||substr(guid,13,4)||'-'||substr(guid,17,4)||'-'||substr(guid,21,12);

END GET_UUID;

trigger:

-- ----------------------------
-- Triggers structure for table manager_employee
-- ----------------------------
DROP TRIGGER IF EXISTS `oyk_employee_before_insert`;
delimiter ;;
CREATE TRIGGER `oyk_employee_before_insert` BEFORE INSERT ON `manager_employee` FOR EACH ROW BEGIN

SET @x := (
    SELECT
        MAX(employee_id)
    FROM
        oyk_employee
);


SET NEW.employee_id = @x + 1;

view:

In the early stage of the development of the report system, the data warehouse development provided temporary data for development and testing through the development view. Later, the view will be replaced with the actual table.

 

Guess you like

Origin blog.csdn.net/qq_24271537/article/details/113211096