Views, functions, stored procedures, triggers, and

view

cause:

  If there is a frequent sql statement will be used to, for example:

    select * from t1 where id > 12;

  You can engage in a map, the above sql statement with a view correspondence

Create a view

create view v1 as select * from t1 where id > 12;

View View

select * from v1;

Modify View

alter view v1 as sql statements;

Delete View

drop view v1;

important point:

  1, the view is just mapping between sql statements, views Once created, if the original data changes, the view will follow to make the appropriate changes

  2, the view can only be performed to view data operations, data can not delete, add, modify

function

MySQL built-in functions in common

CHAR_LENGTH(str)
        The return value is the length of string str, length, in characters. A multi-byte character counts as a single character.
        For a two-byte character set contains five, the LENGTH () returns a value of 10 , while the CHAR_LENGTH () returns a value of 5.
    CONCAT(str1,str2,...)
        String concatenation
        If any parameter is NULL, the return value is NULL.
    FORMAT(X,D)
        Write format for digital X ' #, ###, ###. ## " , in a rounded way to keep D decimal places, and returns the result as a string. If the D is 0, the result is returned without a decimal point, or no fractional part.
        E.g:
            The FORMAT the SELECT ( 12332.1,4); results: ' 12,332.1000 '
    INSTR(str,substr)
        Back in string str first occurrence position.
    LEFT(str,len)
        Returns the string str len characters from the sequence of the start position.
    LOWER(str)
        To lower case
    UPPER(str)
        Variable capital
    LTRIM(str)
        Returns the string str, its boot space character is deleted.
    RTRIM(str)
        Returns the string str, ending space characters are deleted.
    SUBSTRING (p, pos, len)
        Gets a string subsequence
    LOCATE(substr,str,pos)
        Gets the index position of subsequence
    REPEAT(str,count)
        Repeating returns a string consisting of character string str, equal to the number of string str count.
        If COUNT <= 0, then an empty string is returned.
        If str or count is NULL, NULL is returned.
    REPLACE(str,from_str,to_str)
        Returns the string str and all the strings are replaced to_str string from_str.
    REVERSE(str)
        Returns the string str, and the character sequence in reverse order.
    RIGHT (str, len)
        Beginning from the string str, returns back from the subsequence start len-character
MySQL Common Functions

Note: Do not use the function, if you need to convert the value of the conversion in python into complete re-transmission

   Execution greatly affect the efficiency function in mysql

Stored Procedures

The encapsulated long list sql statement, similar to the function, the result is stored procedure

Simple stored procedure

// DELIMITER    # change the sql statement marks the end of 
the Create Procedure p1 ()    # Create a stored procedure p1 
BEGIN
    the SELECT * from T1;
     # ... (SQL statements) 
END // 
DELIMITER; # the end marker and then change it back

# Used in the program 
call p1 ();
Simple Storage

Pass parameters (in)

delimiter //
create procedure p2(
    in N1 int,           # mass participation: in the parameter type 
    in n2 int)
BEGIN
    select * from t1 where id > n1;
END //
delimiter ;

# Used in the program 
call p2 (12,2)
Parameter passing

Outgoing parameters (out)

delimiter //
create procedure p3(
    int N1 OUT)     # outgoing Parameters: out parameter type 
BEGIN
    select * from t1;
    N1 SET =. 1;      # settings outgoing 
END //
delimiter ;

# Used in the program 
SET @ V1 = 12334;   # must be set a first value (value custom) 
Call P3 (@ V1);
@ v1 the SELECT;      # View outgoing value
Outgoing parameters

trigger

Add a record into a table, but also to add a record in another table

Example: Add a record to be added at the same time t2 to t1 in a record

// DELIMITER 
the CREATE TRIGGER c1 the BEFORE INSERT ON t1 the FOR EACH ROW   # create a trigger c1, add the following will be added to t1 in 
BEGIN
    insert into t2 (name,money) values ('xxx','123');
END //
delimiter ;

Guess you like

Origin www.cnblogs.com/hesujian/p/11031057.html