drop database if exists view; create database view; use view; # ## test view schema weak-binding # #simple drop table if exists t1; drop view if exists v, vv; create table t1(c1 int, c2 int); create view v as select c1, c2 from t1; create view vv as select c1 from v; select c1, c2 from v; drop table t1; create table t1(c1 int); --error 5231 select c1 from v; --error 5231 select c2 from v; --error 5231 select c1, c2 from v; drop table t1; --error 5231 select c1 from v; --error 5231 select c2 from v; --error 5231 select c1, c2 from v; create table t1(c1 int); --error 5231 select c1 from vv; --error 5231 select c2 from vv; --error 5231 select c1, c2 from vv; --error 5231 select 1 as a from vv; drop table if exists t1; drop view if exists v, vv; create table t1(c1 int, c2 int); create view v as select c1, c2 from t1; create view vv as select c2 from v; select c1, c2 from v; drop table t1; create table t1(c1 int); --error 5231 select c1 from v; --error 5231 select c2 from v; --error 5231 select c1, c2 from v; drop table t1; --error 5231 select c1 from v; --error 5231 select c2 from v; --error 5231 select c1, c2 from v; create table t1(c1 int); --error 5231 select c1 from vv; --error 5231 select c2 from vv; --error 5231 select c1, c2 from vv; --error 5231 select c2, c1 from vv; --error 5231 select 1 as a from vv; #select * from view drop table if exists t1,t2; drop view if exists v,vv; create table t1(c1 int ,c2 int); create table t2(c1 int ,c2 int); create view v as select c1, c2 from t1; create view vv as select c1 from v; drop table t1; create table t1(c1 int); --error 5231 select * from v; --error 5231 select v.* from v; --error 5231 select * from vv; --error 5231 select vv.* from vv; #join on and using drop table if exists t1,t2; drop view if exists v; create table t1(c1 int,c2 int); create table t2(c1 int,c2 int); create view v as select c1, c2 from t1; drop table t1; create table t1(c1 int); --error 5231 select v.c1 from v join t2 on v.c1 = t2.c1; --error 5231 select v.c1 from v join t2 on v.c2 = t2.c2; --error 5231 select v.c1 from v join t2 using(c1); --error 5231 select v.c1 from v join t2 using(c2); #column in where drop table if exists t1,t2; drop view if exists v; create table t1(c1 int,c2 int); create view v as select c1, c2 from t1; select c1 from v where c2 = 1; select c1 from v where (select c2) = 1; drop table t1; create table t1(c1 int); --error 5231 select c1 from v where c2 = 1; --error 5231 select c1 from v where (select c2) = 1; #column in order by drop table if exists t1,t2; drop view if exists v; create table t1(c1 int,c2 int); create view v as select c1, c2 from t1; select c1 from v order by c2; select c1 from v order by (select c2); drop table t1; create table t1(c1 int); --error 5231 select c1 from v order by c2; --error 5231 select c1 from v order by (select c2); #column in group by drop table if exists t1,t2; drop view if exists v; create table t1(c1 int,c2 int); create view v as select c1, c2 from t1; select c1 from v group by c2; select c1 from v group by (select c2); drop table t1; create table t1(c1 int); --error 5231 select c1 from v group by c2; --error 5231 select c1 from v group by (select c2); #column in having #drop table if exists t1,t2; #drop view if exists v; #create table t1(c1 int,c2 int); #create view v as select c1, c2 from t1; #select c1 from v having c2 = 1; #select c1 from v having (select c2) = 1; #alter table t1 drop column c2; #--error 5217 #select c1 from v having c2 = 1; #--error 5231 #select c1 from v having (select c2) = 1; drop table if exists t1; drop view if exists v; create table t1(c1 int,c2 int); insert into t1 values(1,2),(2,3),(3,4); create view v as select c1,c2 from t1 order by c2; drop table t1; create table t1(c1 int); #order by 即使是从select里找到了c2(select中的c2不报错是因为view_stmt),因为select里面的c2也是无效列,因此最终结果也是视图无效 --error 5231 select c1 from v; drop table if exists t1; drop view if exists v; create table t1(c1 int,c2 int); insert into t1 values(1,2),(2,3),(3,4); create view v as select c1, -1 as c2 from t1 order by (select c2); select c1 from v; drop table t1; create table t1(c1 int); # a select c2提升上来之后从t1基础列里面找不到就直接报错,不会再去select_items找 --error 5231 select c1 from v; # b (bug) #select c1 from (select c1, -1 as c2 from t1 order by (select c2)) v; drop table if exists t1; drop view if exists v; create table t1(c1 int,c2 int); create view v as select c1,c2 from t1 where c2=1; drop table t1; create table t1(c1 int); --error 5231 select c1 from v; drop table if exists t1; drop view if exists v; create table t1(c1 int,c2 int); insert into t1 values(1,2),(2,3),(3,4); create view v as select c1, -1 as c2 from t1 order by (select c2); drop table t1; create table t1(c1 int); --error 5231 select c1 from v; # agg drop table if exists t1; drop view if exists v; create table t1(c1 int,c2 int); create view v as select c1,sum(c2) from t1; drop table t1; create table t1(c1 int); --error 5231 select c1 from v; # set op drop table if exists t1,t2; drop view if exists v; create table t1(c1 int,c2 int); create table t2(c3 int,c4 int); create view v as (select c1,c2 from t1) union (select c3,c4 from t2); drop table t1,t2; create table t1(c1 int); create table t2(c3 int); --error 5231 select c1 from v; --error 5231 select c2 from v; drop database if exists view;