MySQL Quick Check - Programming in MySQL

MySQL quick check

Because I often forget some mysql statements, keywords, operations, etc. in my daily work and study, I recently took some time to write the following content about mysql. It's like a dictionary


Reset mysql password
Data type
operators
Common functions
Data integrity
Basic operations of the database
Operations on the table itself Operations
on data in the table Subquery Multi-table connection Index view Preprocessing SQL statements Custom functions and stored procedures This article







variable

In MySQL, after the client successfully connects to the server, a corresponding session will be generated. During a session, the MySQL service instance generates session system variables corresponding to the session in the MySQL server memory. The initial values ​​of these variables are copies of global system variables. Of course, users can also customize variables. There are two types of custom variables: one is user session variables starting with @, and the other is local variables.

session variables

Session variables include system session variables and user session variables.
What both have in common:

  • Variable names are not case sensitive

The difference between the two:

  • User session variables generally start with one "@"; system session variables start with two "@".
  • System session variables can be used directly without definition; user session variables need to be defined first.
# 例如保存MySQL版本的系统会话变量
select @@version;
+-------------------------+
| @@version               |
+-------------------------+
| 8.0.26-0ubuntu0.20.04.3 |
+-------------------------+
1 row in set (0.01 sec)

How to use user session variables

After a user session variable is successfully created, it can be used in other SQL statements as an expression or a component of an expression. The MySQL client's session variables in its own session cannot be accessed by other clients, and the client cannot access the session variables of other clients. The session variables are automatically released after the client disconnects, that is, the session ends.

Definition and assignment of user session variables

SET @user_variable1 = expression1[, @user_variable2 = expression2, ...]; # 方式一
SELECT expression1 into @user_variable1[, expression2 into @user_variable2, ...];  #方式二
# 定义用户会话变量
# @user_varable 变量名
# expression 表达式

# 例

# 简单的定义变量并赋值
set @name = '老王';
select @name := '老王';

# 从表people中id为1的记录取出name字段的值赋值给用户会话变量@name
set @name = (select name from people where id = 1);
select name from people where id = 1 into @name; 

# 简单地查看变量值
select @name;

local variables

Local variables are variables that are valid within the scope of a local program where they are defined.

Definition and assignment of local variables

DECLARE variable type [default default_value];

Use of local variables

Local variables must be defined in stored procedures such as functions, triggers, and stored procedures. The scope of local variables is limited to stored procedures.

delimiter //
create procedure pro_test(in p_id char(10), out p_name char(10))
begin
	declare local_name char(10) default 'null';  # 定义一个局部变量,默认值为‘null’
	select name into local_name from people where id = p_id;
	select local_name;
	set p_name = local_name;
end //
delimiter ;

# 使用
set @test_name = 'null';

call pro_test(2, @test_name);
+------------+
| local_name |
+------------+
| 潘子       |
+------------+
1 row in set (0.00 sec)

# 查看一下@test_name
select @test_name;
+------------+
| @test_name |
+------------+
| 潘子       |
+------------+
1 row in set (0.00 sec)

Comment

Three kinds of comments for MySQL:

  • # pound sign comment single line comment
  • --Double hyphen comments single line comments
  • / / Forward slash asterisk comments multi-line comments

Conditional statements

if statement

IF 条件 then
	...
[ELSEIF 条件 then]
	...
[ELSE]
	...
END IF

# 例
set @a = 1;
set @b = 2;
set @c = '';
delimiter //
create procedure pro_test2(in a int, in b int, out c char(10))
begin
	if @a > @b then set @c = 'a比较大';
	else set @c = 'b比较大';
	end if;
end //
delimiter ;

call pro_test2(@a, @b, @c);
Query OK, 0 rows affected (0.00 sec)

select @c;
+------------+
| @c         |
+------------+
| b比较大    |
+------------+
1 row in set (0.00 sec)

case statement

CASE case_value
    WHEN when_value THEN ...
    [WHEN when_value THEN ...]
    ...
    [ELSE statement_list]
END CASE
或者
CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE
# 类似于一些编程语言中的switch语句

# 例
# 使用第一种case
DELIMITER //
CREATE PROCEDURE p()
  BEGIN
    DECLARE v INT DEFAULT 1;

    CASE v
      WHEN 1 THEN SELECT 'OK';
      WHEN 2 THEN SELECT v;
      WHEN 3 THEN SELECT 0;
      ELSE
        BEGIN
        END;
    END CASE;
  END//
DELIMITER ;

# 使用第二张case
DELIMITER //
CREATE PROCEDURE p()
  BEGIN
    DECLARE v INT DEFAULT 1;

    CASE
      WHEN v = 1 THEN SELECT 'OK';
      WHEN v = 2 THEN SELECT v;
      WHEN v = 3 THEN SELECT 0;
      ELSE
        BEGIN
        END;
    END CASE;
  END//
DELIMITER ;

# 两者执行的输出结果一样
call p;
+----+
| OK |
+----+
| OK |
+----+
1 row in set (0.00 sec)

case

loop statement

leave & iterate

Anyone who has studied programming languages ​​should know that there are two keywords for loops in programming languages ​​- break and continue . Leave here is like break in programming languages; iterate is like continue.

while loop statement

[begin_label:] WHILE search_condition DO
    statement_list
END WHILE [end_label]

# 例
DELIMITER //
CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;

  WHILE v1 > 0 DO
    SELECT concat('Hello MySQL', v1); # 输出
    SET v1 = v1 - 1;
  END WHILE;
END//
DELIMITER ;

# 输出结果
CALL dowhile;
+---------------------------+
| concat('Hello MySQL', v1) |
+---------------------------+
| Hello MySQL5              |
+---------------------------+
1 row in set (0.00 sec)

+---------------------------+
| concat('Hello MySQL', v1) |
+---------------------------+
| Hello MySQL4              |
+---------------------------+
1 row in set (0.00 sec)

+---------------------------+
| concat('Hello MySQL', v1) |
+---------------------------+
| Hello MySQL3              |
+---------------------------+
1 row in set (0.00 sec)

+---------------------------+
| concat('Hello MySQL', v1) |
+---------------------------+
| Hello MySQL2              |
+---------------------------+
1 row in set (0.00 sec)

+---------------------------+
| concat('Hello MySQL', v1) |
+---------------------------+
| Hello MySQL1              |
+---------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

while

loop statement

[begin_label:] LOOP
    statement_list
END LOOP [end_label]

# 例
DELIMITER //
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
  label1: LOOP
    SET p1 = p1 + 1;
    IF p1 < 10 THEN
      ITERATE label1; # 结束本次循环并回到label1处继续之后的内容
    END IF;
    LEAVE label1;
  END LOOP label1;
  SET @x = p1;
END//
DELIMITER ;

# 结果
call doiterate(0);
Query OK, 0 rows affected (0.00 sec)

mysql> select @x;
+------+
| @x   |
+------+
|   10 |
+------+
1 row in set (0.00 sec)

repeat loop statement

[begin_label:] REPEAT
    statement_list
UNTIL search_condition
END REPEAT [end_label]

Guess you like

Origin blog.csdn.net/weixin_45345384/article/details/120893212