Database renewal

Database continued

****CASE statement* *** Conditional judgment parameter when data indicates the value

The CASE statement can achieve more complex conditional judgments than the IF statement. The basic form of its grammar is as follows:

CASE case_value

WHEN when_value THEN statement_list

WHEN when_value THEN statement_list

ELSE statement_list

END CASE

Select one from multiple sets of data

Among them, the parameter case_value represents the variable of the condition judgment; the parameter when_value represents the value of the variable; the parameter statement_list represents the execution statement with different when_value values.

CASE level

WHEN 20 THEN SET attack = attack + 5; //If the level value is 20, then set

WHEN 30 THEN SET attack = attack + 10;

WHEN 40 THEN SET attack = attack + 15;

ELSE SET attack = attack + 1; // ELSE SET otherwise set

END CASE


*LOOP statement* loop control

The LOOP statement can make certain specific statements be executed repeatedly, realizing a simple loop. The LOOP statement itself does not stop the loop, and the loop can only be stopped when it encounters the LEAVE statement, etc.

delimiter $$//Set the end character

create procedure proc_trst_loop(IN input int,OUT output int)//Create a stored procedure

begin//start

add_numm:LOOP //add_numm: a keyword that represents the label used to break out of the loop loop loop

​ set input=input+1;//Set this template to add

​ if input=100 then //if added to 100

​ LEAVE add_num;//Leave back to the position of the label (ITERATE add_num// means the end of this loop and execute the next time)

​ end if;// if statement end condition

end LOOP add_numm; //End position of loop body

end; //The end condition of the creation process

$$ //end

delimiter ;//Set the end character


*REPEAT statement*

The REPEAT statement is a loop statement with conditional control. When certain conditions are met, the loop statement will jump out. The basic grammatical form of the REPEAT statement is as follows: Used when creating a procedure

REPEAT

SET @count=@count+1;

UNTIL @count=100 //UNTIL means to jump out if the conditions are met

END REPEAT;


*WHILE statement*

The WHILE statement is also a loop statement with conditional control, but the WHILE statement is different from the REPEAT statement. The WHILE statement is to execute the statement in the loop when the condition is met. The basic grammatical form of the WHILE statement is as follows:

WHILE @count<100 DO //DO loop start

SET @count = @count + 1;

END WHILE;


*Comprehensive gameplay*

use school;#Select database school

DELIMITER $$ #Set the end character

create procedure query_all_students (IN sid int, OUT cname varchar(128), OUT cid int) // Create a stored procedure

​ BEGIN //Start

​ declare tmp_name varchar(128); //Declare a variable *#Must be defined before declaring the cursor*

​ declare tmp_cid int; // declare a variable

​ declare done int default 0; //Declare the default value of the variable to be 0, and execute the end operation with the if statement

​ declare cur_student CURSOR FOR SELECT name, class_id FROM student; //Declare the cursor to specify the condition and specify the table name

​ declare continue handler for not found set done = 1; //Bind the end flag to the cursor (fixed to write like this)

​ open cur_student; //Open the cursor

​ read_loop:LOOP //Loop reading

​ fetch cur_student into tmp_name, tmp_cid; //Cursor data read into variable

​ IF done=1 then //end condition

​ Leave read_loop; //Leave the loop

​ END IF; //if statement end flag

​ select tmp_name, tmp_cid; //Print the value obtained from the cursor

​ END LOOP read_loop; //Loop end flag

​ close cur_student; //Close the cursor

​ set cname = tmp_name, cid = tmp_cid; //Assign temporary variables to the passed in variables

​ END; // the end of BEGIN

​ DELIMITER; #Set terminator

​ call proc_xxxx(1); call this stored procedure

select last_insert_id(); Query the last self-increment value inserted by the machine

++++++++++++++++

*View stored procedure*

After the stored procedure is created, the user can view the status of the stored procedure through the SHOW STATUS statement, or view the definition of the stored procedure through the SHOW CREATE statement. Users can also view the information of the stored procedure by querying the Routines table in the information_schema database.

SHOW PROCEDURE stored procedure name'% stored procedure template name%';

SHOW CREATE PROCEDURE proc_name; // proc_name stored procedure name

SHOW CREATE PROCEDURE proc_name\G // proc_name stored procedure name\G means that the terminator is equivalent;

View the information of the stored procedure from the information_schema.Routine table

select routine_definition from information_schema.Routines where routine_name='proc_delete_student'; //routine_definition represents the definition information of this stored procedure information_schema.Routines is the query keyword routine_name='proc_delete_student' is the name of the conditional stored procedure

*Deletion of stored procedure*

The name of the DROP PROCEDURE stored procedure;//The output stored procedure

show engines;//Display the engines of the database


path command line input shows the location of the installation into the directory lib include

The console application needs to be adjusted to 64 to set the vc include directory to include directory (header file)

In the same way, include the library file lib connector input additional dependency libmysql.lib

Copy lib\libmysql.dll in the mysql installation directory to c:\windows\system32


create table users(id int(11) PRIMARY KEY NOT NULL UNIQUE AUTO_INCREMENT
,username varchar(64) NOT NULL UNIQUE
,psaaword varchar(32) NOT NULL
,level_id int DEFAULT 1); //创建用户表

insert into users values(1000,'Sun Wukong',md5('123456'),1);//Insert data

create table levels(id int NOT NULL DEFAULT 1 PRIMARY KEY, name varchar(64) NOT NULL UNIQUE,
map_row int NOT NULL, map_column int NOT NULL, map_data varchar(4096) NOT NULL,
next_level_id int DEFAULT 0);//创建游戏数据表

insert into levels values(1,'Small test knife',9,12,'

0,0,0,0,0,0,0,0,0,0,0,0|0,1,0,1,1,1,1,1,1,1,0,0|0,1,4,1,0,2,1,0,2,1,0,0|

0,1,0,1,0,1,0,0,1,1,1,0|0,1,0,2,0,1,1,4,1,1,1,0|0,1,1,1,0,3,1,1,1,4,1,0|

0,1,2,1,1,4,1,1,1,1,1,0|0,1,0,0,1,0,1,1,0,0,1,0|0,0,0,0,0,0,0,0,0,0,0,0’,

0);//Insert game data

Guess you like

Origin blog.csdn.net/qq_45743563/article/details/107462890