Mysql的视图详解增删改查

视图

    简化查询语句 简化查询语句,从而使用户避免反复重复的操作 安全性 通过视图用户只能查询和修改他们所见的数据 数据库的其他数据既看不到也取不到 逻辑数据独立性 视图可以帮助用户屏蔽真实表结构变化带来的影响

开始代码

    准备工作,创建表
create table a(
	aid int primary key,
	aname varchar(20)
	);
	
create table b(
	bid int primary key,
	aid int not null ,
	foreign key (aid) references a(aid)
)
##添加几条语句
    创建一个基于多表的视图
create or replace view view_aWithB
as select a.*,b.bid from a,b where a.aid=b.aid;
    测试查询
select * from view_aWithB;
    测试插入语句 发现插入成功
insert into view_aWithB(aid,aname) values(3,小兰);
    测试修改 修改成功
update view_aWithB set aname=李四 where aid=1;
    测试删除 不成功
delete from view_aWithB where aid=3;
    但是测试一个基于单表的视图就可以删除成功
create or replace view view_b
as select * from b;
    测试
delete from view_b where bid=2; ##删除成功
经验分享 程序员 微信小程序 职场和发展