14 MySQL 内置功能--视图/触发器

wanjichun 2020-02-17

#视图

select * from course inner join teacher on course.teacher_id=teacher.tid;

create view course2teacher as select * from course inner join teacher on course.teacher_id=teacher.tid;

#触发器

CREATE TABLE cmd (
    id INT PRIMARY KEY auto_increment,
    USER CHAR (32),
    priv CHAR (10),
    cmd CHAR (64),
    sub_time datetime, #提交时间
    success enum (‘yes‘, ‘no‘) #0代表执行失败
);

CREATE TABLE errlog (
    id INT PRIMARY KEY auto_increment,
    err_cmd CHAR (64),
    err_time datetime
);

#创建触发器
delimiter //
CREATE TRIGGER tri_after_insert_cmd AFTER INSERT ON cmd FOR EACH ROW
BEGIN
    IF NEW.success = ‘no‘ THEN #等值判断只有一个等号
            INSERT INTO errlog(err_cmd, err_time) VALUES(NEW.cmd, NEW.sub_time) ; #必须加分号
      END IF ; #必须加分号
END//
delimiter ;

#存储过程

delimiter //
create procedure p1()
BEGIN
    select * from db7.teacher;
END//
delimiter;

#有参存储过程


delimiter //
create procedure p2(in n1, in n2 int, out res int)
BEGIN
    select * from db7.teacher where tid>n1 and tid<n2;
set res=1;
END//
delimiter;


#MySQL中调用
set @x=0
call p2(2,4,@x);

#python 中调用

相关推荐