Database Management and High Availability Chapter 5 Database Functions and Database Creation Process
One: database functions
1.1: Mathematical functions
Mathematical function | description | |
---|---|---|
abs(x) | Returns the absolute value of x | Absolute value |
rand() | Returns a random number from 0 to 1 | rand edge |
mod (x, y) | Returns the remainder of x divided by y | Take the remainder |
power (x, y) | Returns x to the power of y | power fast forward |
round(x) | Returns the integer closest to x | round integer |
round(x,y) | Keep the value of x after rounding to y decimal places | |
sqrt(x) | Returns the square root of x | sqrt square root |
truncate(x,y) | Returns the value of the number x truncated to y decimal places | truncate interception |
ceil(x) | Returns the smallest integer greater than or equal to x | Ceil mounted ceiling |
floor(x) | Returns the largest integer less than or equal to x | floor |
greatest(x1,x2...) | Returns the largest value in the collection | |
least(x1,x2...) | Return the smallest value in the set |
1.2: Aggregate functions
Aggregate function | description | |
---|---|---|
avg() | Returns the average value of the specified column | avg average |
count() | Returns the number of non-NULL values in the specified column | count count |
min () | Returns the minimum value of the specified column | |
max() | Returns the maximum value of the specified column | |
sum(x) | Returns the sum of all values in the specified column |
1.3: String functions
String function | description | |
---|---|---|
length(x) | Returns the length of string x | length |
trim() | Return the value without the specified format | trim trim, cut, remove spaces can only remove the spaces at both ends |
concat(x,y) | Concatenate the provided parameters x and y into a string | concat merge |
upper(x) | Change all letters of string x to uppercase letters | upper |
lower(x) | Change all letters of string x to lowercase letters | lower |
left(x,y) | Returns the first y characters of the string x | Value to the left |
right(x,y) | Returns the last y characters of the string x | Value to the right |
repeat (x, y) | Repeat string x y times | repeat repeat |
space(x) | X spaces are returned | space space |
replace(x,y,z) | Replace string z with string y in string x | replace |
strcmp(x,y) | Comparing x and y, the returned value can be -1,0,1 and converted to ASCII code comparison | strcmp compares strings |
substring(x,y,z) | Get a string of length z starting from the yth position in the string x | substring intercept string |
reverse(x) | Reverse string x | reverse, reverse |
1.4: Date and time functions
String function | description | |
---|---|---|
curdate () | Returns the year, month and day of the current time | Get the current date |
curtime () | Returns the hour, minute and second of the current time | Get current time |
now() | Returns the date and time of the current time | What time is it now |
month(x) | Returns the month value in date x | |
week(x) | Return 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 seconds value in x | |
dayofweek (x) | Return x is the day of the week, 1 Sunday, 2 Monday | |
dayofmonth(x) | Calculation date x is the day of the month | |
dayofyear(x) | Calculation date x is the day of the year |
1.5: Stored procedures
//Format
#define stored procedure
delimiter $$
create procedure存储过程名(in 参数名 参数类型)
begin
#定义变量
declare变量名变量类型
#变量赋值
set 变量名 = 值
sql语句1;
sql语句2;
..
sql语句n;
end $$
delimiter ;
#Call stored procedure
call stored procedure name (actual parameters);
#Query stored procedure
show procedure status where db='database';
#Delete stored procedure
drop procedure stored procedure name;
Example: 1
mysql> delimiter $$
mysql> create procedure myrole() //创建存储过程,名字为myrole
-> begin
-> select name,score from accp;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call myrole();
Example: 2
mysql> delimiter $$
mysql> create procedure myschool(in my_name varchar(10))
-> begin
-> select name score from accp where name=my_name;
-> end$$
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> show procedure status where db='school';
Example 3:
mysql> delimiter $$
mysql> create procedure myupdate(in my_score decimal(5,2))
-> begin
-> declare hob int(3);
-> if my_score >= 80 then
-> set hob = 1;
-> else
-> set hob = 2;
-> end if;
-> update accp set score=my_score,hobby=hob where name='zhangsan';
-> end$$
Query OK, 0 rows affected (0.06 sec)
//修改名为张三的分数和hobby,修改zhangsan的hobby=2,score=75
mysql> delimiter ;
mysql> call myupdate(75);