mysql function and storage + view
- Database function
-
- Mathematical function
- truncate(x,y) returns the value of the number x truncated to y decimal places
- Aggregate function
-
- avg() returns the average value of the specified column
- count() returns the number of non-NULL values in the specified column
- min() returns the minimum value of the specified column
- max() returns the maximum value of the specified column
- sum() returns the sum of all values in the specified column
- String function
-
- length(x) returns the length of the string x
- trim() returns the value of the specified format removed
- concat(x,y) concatenate the provided parameters x and y into a string
- upper(x) turns all letters of string x into uppercase letters
- left(x,y) returns the first y characters of the string x
- right(x,y) returns the last y characters of the string x
- repeat(x,y) repeat the string x y times
- space(x) returns x spaces
- replace(x,y,z) replace the string z with the string y in the string x
- substring(x,y,z) gets a string of length z starting from the yth position in the string x
- reverse(x) reverse the string x
- Date and time functions
-
- curdate() returns the year, month and day of the current time
- curtime() returns the hour, minute, and second of the current time
- now() returns the date and time of the current time
- month(x) returns the month value in date x
- week(x) returns the date x is the first few weeks of the year
- hour(x) returns the hour value in x
- minute(x) returns the minute value in x
- second(x) returns the second value in x
- dayofweek(x) returns x is the day of the week, 1 Sunday, 2 Monday
- dayofmonth(x) Calculate the date x is the day of the month
- dayofyear(x) Calculate the date x is the day of the year
- Introduction to stored procedures
- view
Database function
■ MySQL provides functions to implement various functions.
■ Commonly used function classifications
● Mathematical functions
● Aggregate functions
● String functions
● Date and time functions
Mathematical function
■ Commonly used mathematical functions
abs(x) returns the absolute value of x
mysql> select abs(3);
mysql> select abs(-2);
rand() returns a random number from 0 to 1
mysql> select rand();
mod(x,y) returns the remainder of x divided by y
mysql> select mod(10,4);
mysql> select mod(19,2);
round(x) returns the integer closest to x
mysql> select round(9.10);
mysql> select round(10.12);
mysql> select round(13.16);
round(x,y) retains the value of x's y decimal place after rounding
mysql> select round(4.654,2);
mysql> select round(4.656,2);
sqrt(x) returns the square root of x
mysql> select sqrt(81);
mysql> select sqrt(24);
truncate(x,y) returns the value of the number x truncated to y decimal places
mysql> select truncate(9.762,2);
mysql> select truncate(9.769,2);
ceil(x) returns the smallest integer greater than or equal to x
mysql> select ceil(10.0001);
mysql> select ceil(12.908);
floor(x) returns the largest integer less than or equal to x
mysql> select floor(13.541);
mysql> select floor(14.128);
### greatest(x1,x2…) 返回集合中最大的值
mysql> select greatest(15,110,150);
mysql> select greatest(118,66,898);
least(x1,x2...) returns the smallest value in the set
mysql> select least(127,22,45);
mysql> select least(929,67,332);
Aggregate function
■ A class of functions designed to centrally summarize the data records in the table
■ Commonly used aggregate functions
avg() returns the average value of the specified column
mysql> select * from cj1;
mysql> select avg(chengji) from cj1;
count() returns the number of non-NULL values in the specified column
mysql> select count(NULL) from cj1;
mysql> select count(chengji) from cj1;
min() returns the minimum value of the specified column
mysql> select min(chengji) from cj1;
max() returns the maximum value of the specified column
mysql> select max(chengji) from cj1;
sum() returns the sum of all values in the specified column
mysql> select sum(chengji) from cj1;
String function
Commonly used string functions
length(x) returns the length of the string x
mysql> select length('abc');
mysql> select length('abcde');
trim() returns the value of the specified format removed
mysql> select trim('abcd');
concat(x,y) concatenate the provided parameters x and y into a string
mysql> select concat('abc','def');
upper(x) turns all letters of string x into uppercase letters
mysql> select upper('Goodnight');
left(x,y) returns the first y characters of the string x
mysql> select left('flowers',2);
right(x,y) returns the last y characters of the string x
mysql> select right('flowers',2);
repeat(x,y) repeat the string x y times
mysql> select repeat('night',2);
space(x) returns x spaces
mysql> select space(5);
mysql> select space(20);
replace(x,y,z) replace the string z with the string y in the string x
mysql> select replace('goodnight','o','a');
strcmp(x,y)比较x和y,返回的值可以为-1,0,1
mysql> select strcmp(2,3);
mysql> select strcmp(2,2);
mysql> select strcmp(3,2);
substring(x,y,z) gets a string of length z starting from the yth position in the string x
mysql> select substring('abceagting',3,4);
reverse(x) reverse the string x
mysql> select reverse('night');
Date and time functions
■ Commonly used date and time functions
curdate() returns the year, month and day of the current time
mysql> select curdate();
curtime() returns the hour, minute, and second of the current time
mysql> select curtime();
now() returns the date and time of the current time
mysql> select now();
month(x) returns the month value in date x
mysql> select month('2020-12-29');
week(x) returns the date x is the first few weeks of the year
mysql> select week('2020-12-29');
hour(x) returns the hour value in x
mysql> select hour('23:27:26');
minute(x) returns the minute value in x
mysql> select minute('23:27:26');
second(x) returns the second value in x
mysql> select second('23:27:26');
dayofweek(x) returns x is the day of the week, 1 Sunday, 2 Monday
mysql> select dayofweek('2020-12-29');
dayofmonth(x) Calculate the date x is the day of the month
mysql> select dayofmonth('2020-12-29');
dayofyear(x) Calculate the date x is the day of the year
mysql> select dayofyear('2020-12-29');
Introduction to stored procedures
■ It is a set of SQL statements to complete specific functions
■ It is faster and more efficient than traditional SQL
Advantages of stored procedures
■ After one execution, the generated binary code will reside in the buffer to improve execution efficiency.
■ SQL statements plus a collection of control statements, high flexibility
■ Store on the server side and reduce network load when called by the client
■ Multiple times It is called repeatedly and can be modified at any time without affecting client calls.
■ It can complete all database operations and control the information access permissions of the database.
■ To create a stored procedure, you must have the CREATE ROUTINE permission.
■ After the stored procedure is created, you can do more Repeated calls, it encapsulates multiple SQLs together, and can modify the SQL statement at any time without affecting the client that calls it
Create a stored procedure
■ Use the CREATE PROCEDURE statement to create a stored procedure
■ Create a grammatical structure of the stored procedure
CREATE PROCEDURE <过程名> ( [过程参数[..]]) <过程体> #尽量避免与内置的函数或字段重名
[过程参数[,...]]格式
[ IN| OUT | INOUT] <参数名> <类型>
■ Parameters are divided into
● Input parameters: IN
● Output parameters: OUT
● Input/output parameters: INOUT
■ The main part of the stored procedure is called the procedure body
■ It starts with BEGIN and ends with END. If there is only one SQL statement, You can omit BEGIN-END
■ Start and end with DELIMITER
mysql> DELIMITER // /// / User-defined terminator
// stored procedure is omitted other steps
mysql> DELIMITER; // a space before the semicolon
Stored procedure without parameters
mysql> use zz;
mysql> delimiter $$ #定义结束符
mysql> create procedure zzz() #定义存储过程名字
-> begin
-> create table qq(name varchar(64),score int(3));
-> insert into qq values('zhangsan',90),('lisi',60);
-> select * from qq;
-> end $$
mysql> delimiter ; # 分号前有空格
mysql> call qq();
Stored procedure with parameters
mysql> use zz;
mysql> delimiter &&
mysql> create procedure getscore(IN a varchar(64))
-> begin
-> select * from zzz1 where name=a;
-> end &&
mysql> delimiter ;
mysql> call getscore('lisi');
Modify the stored procedure
■ The modification of the stored procedure is divided into feature modification and content modification.
■ Feature modification method
ALTER PROCEDURE <procedure name> [<feature>…]
■ Content modification can delete the original stored procedure first, and then create it later
Delete stored procedure
■ Delete the syntax of the stored procedure
DROP {
PROCEDURE | FUNCTION}[ IF EXISTS ] <过程名> #防止因删除不存在的存储过程而引发的错误
■ Specific usage of deletion
mysql> DROP PROCEDURE PlayerRole; #删除之前确认有无依赖关系
Query OK, 0 rows affected (0.0C sec)
mysql> CALL PlayerRole;
ERROR 1305 (42000): PROCEDURE test.PlayerRole does not exist
delete
mysql> drop procedure zzz;
mysql> call zzz();
ERROR 1305 (42000): PROCEDURE aa.cr does not exist
Examples and summary of stored procedure in, out, inout parameters
mysql> use zz;
mysql> set @num1=1, @num2=2, @num3=3;
mysql> delimiter $$
mysql> create procedure p(in num1 int, out num2 int, inout num3 int)
-> begin
-> select num1,num2,num3;
-> set num1=10,num2=20,num3=30;
-> select num1,num2,num3;
-> end $$
mysql> delimiter ;
mysql> call p(@num1,@num2,@num3);
mysql> select @num1,@num2,@num3; #查看全局变量的值
Summary 1: The in and inout parameters will pass the value of the global variable into the stored procedure, and the out parameter will not pass the value of the global variable into the stored procedure. In the use of the stored procedure, the parameter values in, out, and inout will all change.
Summary 2: After calling the stored procedure, it is found that the in parameter will not change the value of the global variable, and the out and inout parameters will change the value of the global variable after calling the stored procedure, and the value after the stored procedure will be referenced Assign values to global variables.
The in parameter assignment type can be a variable and a fixed value, while the out and inout parameter assignment types must be variables.
view
mysql> create table sushe(id int(10),num int(3));
mysql> insert into sushe values(1,101),(2,102),(3,103),(4,104),(5,105);
mysql> create view stu as select cj.name,sushe.num from cj1 inner join sushe where cj.xuehao=sushe.id;
mysql> select * from stu;