视图,触发器

一、视图

视图是一个虚拟表(非真实存在),其本质是根据SQL语句获取动态的数据集,并为其命名,

使用视图我们可以把查询过程中的临时表摘出来,用视图去实现,这样以后再想操作该临时表的数据时就无需重写复杂的sql了,直接去视图中查找即可,但视图有明显地效率问题,并且视图是存放在数据库中的,如果我们程序中使用的sql过分依赖数据库中的视图,即强耦合,那就意味着扩展sql极为不便,因此并不推荐使用。

#两张有关系的表
mysql> select * from course;
+-----+--------+------------+
| cid | cname  | teacher_id |
+-----+--------+------------+
|   1 | 生物   |          1 |
|   2 | 物理   |          2 |
|   3 | 体育   |          3 |
|   4 | 美术   |          2 |
+-----+--------+------------+
4 rows in set (0.00 sec)
mysql> select * from teacher;
+-----+-----------------+
| tid | tname           |
+-----+-----------------+
|   1 | 张磊老师        |
|   2 | 李平老师        |
|   3 | 刘海燕老师      |
|   4 | 朱云海老师      |
|   5 | 李杰老师        |
+-----+-----------------+
5 rows in set (0.00 sec)
#查询李平老师教授的课程名
mysql> select cname from course inner teacher on teacher_id = tid where tname = ‘李平’;
创建视图
#语法:CREATE VIEW 视图名称 AS  SQL语句
create view teacher_view as select tid from teacher where tname='李平老师';

修改视图

语法:ALTER VIEW 视图名称 AS SQL语句
mysql> alter view teacher_view as select * from course where cid>3;
Query OK, 0 rows affected (0.04 sec)

mysql> select * from teacher_view;
+-----+-------+------------+
| cid | cname | teacher_id |
+-----+-------+------------+
|   4 | xxx   |          2 |
|   5 | yyy   |          2 |
+-----+-------+------------+
2 rows in set (0.00 sec)

 

#修改视图,原始表也跟着改
mysql> select * from course;
+-----+--------+------------+
| cid | cname  | teacher_id |
+-----+--------+------------+
|   1 | 生物   |          1 |
|   2 | 物理   |          2 |
|   3 | 体育   |          3 |
|   4 | 美术   |          2 |
+-----+--------+------------+
4 rows in set (0.00 sec)

mysql> create view course_view as select * from course; #创建表course的视图
Query OK, 0 rows affected (0.52 sec)

mysql> select * from course_view;
+-----+--------+------------+
| cid | cname  | teacher_id |
+-----+--------+------------+
|   1 | 生物   |          1 |
|   2 | 物理   |          2 |
|   3 | 体育   |          3 |
|   4 | 美术   |          2 |
+-----+--------+------------+
4 rows in set (0.00 sec)

删除视图
语法:DROP VIEW 视图名称

DROP VIEW teacher_view

二、触发器
使用触发器可以定制用户对表进行【增、删、改】操作时前后的行为,注意:没有查询
增:
1 create trigger tri_before_insert_t1 before insert on t1 for each row
2 begin
3     sql语句;
4 end

 

1 create trigger tri_after_insert_t1 after insert on t1 for each row
2 begin
3     sql语句;
4 end

 

删:

 1 # 删除=》delete
 2 create trigger tri_before_delete_t1 before delete on t1 for each row
 3 begin
 4     sql语句;
 5 end
 6 
 7 create trigger tri_after_delete_t1 after delete on t1 for each row
 8 begin
 9     sql语句;
10 end

改:

 1 # 更新前
 2 CREATE TRIGGER tri_before_update_tb1 BEFORE UPDATE ON tb1 FOR EACH ROW
 3 BEGIN
 4     ...
 5 END
 6 
 7 # 更新后
 8 CREATE TRIGGER tri_after_update_tb1 AFTER UPDATE ON tb1 FOR EACH ROW
 9 BEGIN
10     ...
11 END

1、创建触发器:

 1 #准备表
 2 CREATE TABLE cmd (
 3     id INT PRIMARY KEY auto_increment,
 4     USER CHAR (32),
 5     priv CHAR (10),
 6     cmd CHAR (64),
 7     sub_time datetime, #提交时间
 8     success enum ('yes', 'no') #0代表执行失败
 9 );
10 
11 CREATE TABLE errlog (
12     id INT PRIMARY KEY auto_increment,
13     err_cmd CHAR (64),
14     err_time datetime
15 );
16 
17 #创建触发器
18 delimiter //
19 CREATE TRIGGER tri_after_insert_cmd AFTER INSERT ON cmd FOR EACH ROW
20 BEGIN
21     IF NEW.success = 'no' THEN #等值判断只有一个等号
22             INSERT INTO errlog(err_cmd, err_time) VALUES(NEW.cmd, NEW.sub_time) ; #必须加分号
23       END IF ; #必须加分号
24 END//
25 delimiter ;
26 
27 
28 #往表cmd中插入记录,触发触发器,根据IF的条件决定是否插入错误日志
29 INSERT INTO cmd (
30     USER,
31     priv,
32     cmd,
33     sub_time,
34     success
35 )
36 VALUES
37     ('egon','0755','ls -l /etc',NOW(),'yes'),
38     ('egon','0755','cat /etc/passwd',NOW(),'no'),
39     ('egon','0755','useradd xxx',NOW(),'no'),
40     ('egon','0755','ps aux',NOW(),'yes');
41 
42 
43 #查询错误日志,发现有两条
44 mysql> select * from errlog;
45 +----+-----------------+---------------------+
46 | id | err_cmd         | err_time            |
47 +----+-----------------+---------------------+
48 |  1 | cat /etc/passwd | 2017-09-14 22:18:48 |
49 |  2 | useradd xxx     | 2017-09-14 22:18:48 |
50 +----+-----------------+---------------------+
51 2 rows in set (0.00 sec)

2、使用触发器

触发器无法由用户直接调用,而知由于对表的【增/删/改】操作被动引发的。

3、删除触发器

drop trigger tri_after_insert_cmd;

 

 


posted @ 2020-09-09 15:58  Οo白麒麟оΟ  阅读(205)  评论(0编辑  收藏  举报