前言:
如今兄弟们对“mysql视图删除”可能比较着重,看官们都需要学习一些“mysql视图删除”的相关内容。那么小编同时在网络上搜集了一些关于“mysql视图删除””的相关资讯,希望咱们能喜欢,朋友们一起来学习一下吧!视图
视图操作
触发器
语法:
示例一
示例二
记录数据
视图
关系型数据库中的数据是由一张一张的二维关系表所组成,简单的单表查询只需要遍历一个表,而复杂的多表查询需要将多个表连接起来进行查询任务。对于复杂的查询事件,每次查询都需要编写MySQL代码效率低下。为了解决这个问题,数据库提供了视图(view)功能;
视图实际就是一串存储下来的select语句,删除视图不会影响原来的表,通畅都是用来展示使用的;
视图操作
创建:create view view_name as select_statement
修改:alter view view_name as select_statement
删除:drop view [ if exsits] view_name[,view_name...]
[cce]>show tables;
+---------------+
| Tables_in_cce |
+---------------+
| cce |
+---------------+
1 row in set (0.01 sec)
[cce]>select * from cce;
+----+------+------+--------+
| id | name | ages | gender |
+----+------+------+--------+
| 1 | cce | 30 | m |
| 2 | cfj | 10 | f |
| 3 | csw | 40 | m |
| 4 | dxf | 39 | f |
+----+------+------+--------+
4 rows in set (0.00 sec)
[cce]>create view view_cce as select name,gender from cce where id > 2;
Query OK, 0 rows affected (0.09 sec)
[cce]>show tables;
+---------------+
| Tables_in_cce |
+---------------+
| cce |
| view_cce |
+---------------+
2 rows in set (0.00 sec)
[cce]>select * from view_cce;
+------+--------+
| name | gender |
+------+--------+
| csw | m |
| dxf | f |
+------+--------+
2 rows in set (0.01 sec)
触发器
相当于python的触发器,当某一个事件激活之后再执行相关的操作,比如有这么一个需求,当一个用户注册之后,我们需要在用户表插入一条数据,还要在另一个库插入数据,那么此时我们也可以借助触发器来实现;
语法:
# 插入前
create trigger trigger_name before insert on tab1 from each row
begin
...
end
在tab1表每插入一条数据之前,begin后面的逻辑都会执行一遍;
# 插入后
create trigger trigger_name after insert on tab1 from each row
begin
...
end
在tab1表每插入一条数据之后,begin后面的逻辑都会执行一遍;
# 删除前
create trigger trigger_name before delete on tab1 for each row
begin
...
end
在tab1表每删除一条数据之前,begin后面的逻辑都会执行一遍;
# 删除后
create trigger trigger_name after delete on tab1 for each row
begin
...
end
在tab1表每删除一条数据之后,begin后面的逻辑都会执行一遍;
# 更新前
create trigger trigger_name before update on tab1 for each row
begin
...
end
在tab1表每删除一条更新之前,begin后面的逻辑都会执行一遍;
# 更新后
create trigger trigger_name after update on tab1 for each row
begin
...
end
在tab1表每删除一条更新之后,begin后面的逻辑都会执行一遍;
示例一
在新增用户之后,然后在用户统计表,使用户总数+1;
-- 创建用户表
create table if not exists user(id int auto_increment,user_name char(32) not null,age int not null,gender enum('男','女') default'男',primary key(id)) engine=innodb default charset=utf8;
-- 用户统计表
create table if not exists cun(user_count int not null) engine=innodb default charset=utf8;
-- 创建触发器
create trigger user_count before insert on user for each row
begin
insert into cun select count(*) +1 as cun from user;
end
# 测试
[cce]>select * from cun;
Empty set (0.00 sec)
[cce]>select * from user;
Empty set (0.05 sec)
[cce]>insert into user(user_name,age) values ('cce',18);
Query OK, 1 row affected (0.08 sec)
[cce]>select * from cun;
+------------+
| user_count |
+------------+
| 1 |
+------------+
1 row in set (0.00 sec)
[cce]>insert into user(user_name,age) values ('cce',18);
Query OK, 1 row affected (0.04 sec)
[cce]>select * from cun;
+------------+
| user_count |
+------------+
| 1 |
| 2 |
+------------+
2 rows in set (0.00 sec)
示例二
-- 修改结束符
[cce]>delimiter @
-- 创建用户表
[cce]>create table if not exists user(id int auto_increment,user_name char(32) not null,age int not null,gender enum('男','女') default'男',primary key(id)) engine=innodb default charset=utf8@
-- 用户统计表,初次创建需要手动增加一条数据并且值为0,因为触发器使用的是update,所以需要表内至少有一条数据
[cce]>create table if not exists cun(user_count int not null) engine=innodb default charset=utf8@
[cce]>insert into cun values(0)@
-- 创建触发器
[cce]>create trigger user_count before insert on user for each row
begin
update cun set user_count=(select count(*) +1 as cun from user);
end
-- 测试插入
[cce]>insert into user(user_name,age) VALUES ( 'cce', 18)@
[cce]>select * from cun@
+------------+
| user_count |
+------------+
| 1 |
+------------+
1 row in set (0.01 sec
记录数据
NEW:是一个对象,里面存储这我们insert的数据,如示例举例NEW.age相当于就是insert语句里面的age字段的值;
OLD:是一个对象,里面存储这我们delete的数据;
示例如下:
create table if not exists user(id int auto_increment,user_name char(32) not null,age int not null,gender enum('男','女') default'男',primary key(id)) engine=innodb default charset=utf8@;
create table if not exists cun(user_count int not null) engine=innodb default charset=utf8;
create trigger user_count before insert on user for each row
begin
insert into cun values (NEW.age);
end
insert into user(user_name,age) VALUES ( 'cce', 18);
标签: #mysql视图删除