MySQL——5

review

1.SQL statement 
1. Data line
1. temporary table () AS A
2. specify the mapping
SELECT ID, name. 1, SUM (X) from T1
3. Condition
Case When ID>. 8 the then the else XX XX End
4. ternary operator
IF (ISNULL (xx), 0, 1)
5.where by xx xx Group
2. section on practice
-based structure of user rights management ---- the table, resulting in some operations more complex
role-based permissions management
1. user information table
2. table permission type
3. role table
4. rights management role

Learning Content

1.union-- even down table, to automatically re- 
union all-- even down table, not heavy
2. View
1.create view xx as SQL statements
alter view xx as SQL statements
drop View XX
3. trigger
when a pair of Zhang table: when CRUD, using triggers custom behavior associated
new the new data is
old data and old
4. functions
1. built-in functions
to execute the function: select xx ();
common: DATE_FORMAT
2. custom function
The stored procedure ( than the previous one important point)
1. an alias stored on the MySQL ---> cook SQL statement
2. call
alias ()
3. programmers write SQL statements used in place of
4. Method 1:
Mysql: stored Procedures
programmer : call a stored procedure
Second way:
Mysql: check the
programmer: SQL statements
three ways:
Mysql :. .
Programmer: a frame (classes and objects ---> SQL statements)
5. Statement
1. no parameters
2. Reference containing (in)
3. containing a reference (OUT)
6. The characteristic
1. transmissibility parameters: in, OUT, INOUT
2. both return a result, there the return value
7 by the code, transaction support

8. implement cursor cursor operation cycle

9. perform dynamic SQL, anti-injection

Code area

1. Trigger

delimiter //
    create trigger xx before insert on student for each row
    begin
        insert into teacher(tname) values ('sss');
    end //
    delimiter ;

2. Custom Functions

-- SET GLOBAL log_bin_trust_function_creators = 1;
        delimiter //
        create function f1(
            i1 int,
            i2 int)
        returns int
        begin
            declare num int default 0;
            set num = i1+i2;
            return(num);
        end //
        delimiter ;

3. Stored Procedures

delimiter //
CREATE procedure p3(
    in i1 int,
    out i2 int
)
BEGIN
    set i2 = 123123;
    SELECT * from student where sid >i1 and sid <i2;
end //
delimiter ;

set @v1 = 0;
SELECT @v1;
call p3(12,@v1);
SELECT @v1
                  

4. The stored procedure, implemented transaction operation

delimiter \\
create PROCEDURE p1(
    OUT p_return_code tinyint
)
BEGIN 
  DECLARE exit handler for sqlexception 
  BEGIN 
    -- ERROR 
    set p_return_code = 1; 
    rollback; 
  END; 
 
  DECLARE exit handler for sqlwarning 
  BEGIN 
    -- WARNING 
    set p_return_code = 2; 
    rollback; 
  END; 
 
  START TRANSACTION; 
    DELETE from tb1;
    insert into tb2(name)values('seven');
  COMMIT; 
 
  -- SUCCESS 
  set p_return_code = 0; 
 
  END\\
delimiter ;

The stored procedure, to achieve circulation, cursors

delimiter //
CREATE procedure p6()
BEGIN
    declare row_id int;
    declare row_num int;
    declare temp int;
    declare done int default false;
    
    declare my_cursor CURSOR for select id,num from a;
    declare continue handler for not found set done=true;
    
    open my_cursor;
        xx:loop
            fetch my_cursor into row_id, row_num;
            if done then 
                leave xx;
            end if;
            set temp=row_id+row_num;
            insert into b(num) values (temp);
        end loop xx;
    close my_cursor;
end //
delimiter ;
call p6()

6. Dynamic SQL execution, Anti-SQL injection

delimiter \\
DROP PROCEDURE IF EXISTS proc_sql \\
CREATE PROCEDURE proc_sql ()
BEGIN
    declare p1 int;
    set p1 = 11;
    set @p1 = p1;

    PREPARE prod FROM 'select * from tb2 where nid > ?';
    EXECUTE prod USING @p1;
    DEALLOCATE prepare prod; 

END\\
delimiter ;

 

Guess you like

Origin www.cnblogs.com/wan2-0/p/10966238.html