Database (view, events, triggers, functions, stored, variable)

07.21 self-summary

A. View

1. What is is a view

Is essentially a virtual table, that is visible but does not actually exist

  • View modified within the content, the content of the original table will also be affected
  • Modify the contents of the original table view will also be affected

目的: Simplify sql statement

2. deletions view change search

  • increase

    create view view name as select statement

  • delete

    drop view view name

  • change

    alter view view name as the new select statement

  • check

    desc view_name

    show create view view name

II. Trigger

1. What is the trigger

When occurs within a table, add, delete, change the antithesis of data are subject to change, we can give him specific changes in content, division some select statements, and logical judgment

2. Trigger CRUD

  • increase

    create trigger trigger event on the time table name name for each row

    begin

    sql statement. . . . . :

    end

    • Time: before the event occurs before | after the incident after
    • 事件:update delete insert
  • delete

    drop trigger trigger name

  • check

    show triggers; see all flip-flops

    show create trigger trigger name

  • change

    Recommendation deleted and then add

3. Examples

python # 将结束符设置为| delimiter | create trigger cmd_insert after insert on cmd for each row begin if new.success = "no" then insert into errlog values(null,new.cmd,new.sub_time); end if; end| # 在还原之前的结束符 delimiter ;

4.ord and new

  • ord: update, delete available
  • new: update, insert available

III. Things

1. What is the thing

The transaction is a combination of a series of sql statements, as a whole

2. Features of things

  • Atomic, refers to this transaction sql statement as a whole, it can not be split, either perform or all fail

  • Consistency, after the end of transaction execution, the associated table must be correct, it does not send data confusion

  • Isolation, isolation between transactions, data not affect each other, even if the operation of the same table, essentially lock The lock granularity is divided into several different isolation levels

  • Persistent, after the successful implementation of transaction data will be stored permanently, can not be restored

3. The syntax of things

mysql #开启事务 start transaction #sql 语句...... #sql 语句...... #sql 语句...... commit #提交事务 一旦提交就持久化了

4. Archiving

1.savepoint

Set archive file save point sql statement savepoint name

2. Return the archive points

  • rollback to save point name
  • rollback return to the beginning of the event
  • read committed modify the information uploaded

Note: If an event is submitted, will not return to archive points

Fourth, the stored procedure

1. What is a stored procedure

Any combination sql statement, are placed one stored procedure, a function similar to, a function, there are parameters, or the function body

2. Grammar

1. Create

create procedure p_name(p_type p_name p_date_type)
begin
sql.......
end

  • p_type: There are two in input, out output, inout Input Output
  • p_name: Parameter name
  • p_date_type: Parameter Type

2. Call

call add1 (parameters);

Which was passed in for specific values ​​of the parameters, out for variable names in the database

3. Delete

drop procedure name;

4. Find

  • Find All select name from mysql.proc where db = database name and type = 'PROCEDURE';
  • Find Individual: show create procedure name;

V. defined variables

set @ variable name = value

VI. Functions

A mathematical function

function Introduction
abs(x) Returns the absolute value of x
am (x) Return of binary x (oct return octal, hex return hexadecimal)
ceiling(x) Returns the smallest integer greater than x
exp(x) Return value e (natural logarithm of the bottom) of the power of x
floor(x) Returns the greatest integer less than x
greatest(x1,x2,...,xn) Returns a collection of greatest value
least(x1,x2,...,xn) Returns the minimum value set
LN (x) Returns the natural logarithm of x
log(x)y) Returns the number of x and y is in the bottom of
v (x) y) Back x / y modulus (remainder)
pi() Returns the value of pi (pi)
rand() Return random value within 0 to 1, can be made by providing a parameter rand (seed) () random number generator generates a specified value.
round(x,y) Returns rounding parameter x are y bits decimal value
sign(x) Return Value x represents a number of symbols
sqrt(x) Returns the square root of a number
truncate(x,y) Returns the number x of y bit result truncated decimal

Second, aggregate functions (commonly used to select the group by clause in the query)

function Introduction
avg (col) Returns the specified column average
count(col) Returns the number of null values ​​in the column of the specified Central Africa
min (col) Returns the specified minimum value of the column
max(col) Returns the maximum value of the specified column
sum(col) Returns all column values ​​and
group_concat(col) Connected by return result column values ​​belonging to a group formed by combining

Third, String Functions

function Introduction
ascii(char) Returns the ascii value of the character code
bit_length(str) Returns the bit length of the string
concat(s1,s2...,sn) The s1, s2 ..., sn are connected to a string
concat_ws(sep,s1,s2...,sn) The s1, s2 ..., sn are connected to a string, and the character spacing sep with
insert(str,x,y,instr) The string str from the start position x, y characters long substring is replaced with a string instr, return results
find_in_set(str,list) Analysis of a comma-separated list list, if found str, returns the position in the list of str
lcase (str) or lower (str) Returns the string str in all the characters change as the result of lower case
left(str,x) Returns the string str leftmost character x
length(s) Returns the number of characters in the string str
ltrim(str) Cut from the beginning of the string str spaces
position(substr in str) Returns the position of the substring in the string str substr first occurrence
quote(str) A backslash str single quotes
repeat(str,srchstr,rplcstr) Returns the string str repeated x times results
reverse(str) Return result reversed a string of str
right(str,x) Returns the string str rightmost characters x
rtrim(str) Returns the string str trailing spaces
strcmp(s1,s2) Compare strings s1 and s2
trim(str) Remove all spaces of the head and tail string
ucase(str)或upper(str) Returns the string str in all the characters into uppercase results

四、日期和时间函数

函数 介绍
curdate()或current_date() 返回当前的日期
curtime()或current_time() 返回当前的时间
date_add(date,interval int keyword) 返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:selectdate_add(current_date,interval 6 month);
date_format(date,fmt) 依照指定的fmt格式格式化日期date值
date_sub(date,interval int keyword) 返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:selectdate_sub(current_date,interval 6 month);
dayofweek(date) 返回date所代表的一星期中的第几天(1~7)
dayofmonth(date) 返回date是一个月的第几天(1~31)
dayofyear(date) 返回date是一年的第几天(1~366)
dayname(date) 返回date的星期名,如:select dayname(current_date);
from_unixtime(ts,fmt) 根据指定的fmt格式,格式化unix时间戳ts
hour(time) 返回time的小时值(0~23)
minute(time) 返回time的分钟值(0~59)
month(date) 返回date的月份值(1~12)
monthname(date) 返回date的月份名,如:select monthname(current_date);
now() 返回当前的日期和时间
quarter(date) 返回date在一年中的季度(1~4),如select quarter(current_date);
week(date) 返回日期date为一年中第几周(0~53)
year(date) 返回日期date的年份(1000~9999)

五.自定义函数

语法

CREATE FUNCTION f_name(paramters)
returns dataType;
return value;

说明:
paramters 只能是in 输入参数 参数名 类型
必须有返回值
不能呢加begin 和end
returns 后面是返回值的类型 这里不加分号
return 后面是要返回的值

注意

函数只能返回一个值
函数一般不涉及数据的增删改查 就是一个通用的功能
调用自定义的函数 与调用系统的一致 不需要call 使用select 可获得返回值
函数中不能使用sql语句
就像在java中不能识别sql语句一样

案例

delimiter |

create function addfuntion(a int,b int)
returns int return a + b;

delimiter ;

执行函数

select addfuntion(1,1);

七.关键字delimiter

delimiter改变命令行语言执行的方法
/
如默认是;进行运行

而delimiter | 表示末尾输入|表示执行

Guess you like

Origin www.cnblogs.com/pythonywy/p/11221244.html