PDB1@ORCL> create table tablea( 2 mypk char(4) 3 ); Table created. PDB1@ORCL> create table tableb( 2 mypk char(4) 3 ); Table created. PDB1@ORCL> create view myview as select * from tablea union select * from tableb; View created. PDB1@ORCL> select * from myview; no rows selected PDB1@ORCL> desc myview; Name Null? Type ----------------------------------------- -------- ---------------------------- MYPK CHAR(4) PDB1@ORCL> insert into tablea values("newdata"); insert into tablea values("newdata") * ERROR at line 1: ORA-00984: column not allowed here PDB1@ORCL> insert into tablea values("data"); insert into tablea values("data") * ERROR at line 1: ORA-00984: column not allowed here PDB1@ORCL> insert into tablea values('data'); 1 row created. PDB1@ORCL> select * from myview; MYPK ---- data PDB1@ORCL> create table tablec( 2 youpk char(4) 3 ); Table created. PDB1@ORCL> create view youview as select * from tablea union select * from tablec; View created. PDB1@ORCL> desc youview; Name Null? Type ----------------------------------------- -------- ---------------------------- MYPK CHAR(4) PDB1@ORCL> create table tabled( 2 mypk char(8) 3 ); Table created. PDB1@ORCL> create view viewthird as select * from tablea union tabled; create view viewthird as select * from tablea union tabled * ERROR at line 1: ORA-00928: missing SELECT keyword PDB1@ORCL> create view viewthird as select * from tablea union select * from tabled; View created. PDB1@ORCL> desc viewthird; Name Null? Type ----------------------------------------- -------- ---------------------------- MYPK VARCHAR2(8) PDB1@ORCL> create table tablee( 2 mypk int 3 ); Table created. PDB1@ORCL> create view viewe as select * from tablea union select * from tablee; create view viewe as select * from tablea union select * from tablee * ERROR at line 1: ORA-01790: expression must have same datatype as corresponding expression PDB1@ORCL> create table tablef( 2 mypk varchar(6) 3 ) 4 ; Table created. PDB1@ORCL> create table viewf as select * from tablea union select * from tablef 2 ; Table created. PDB1@ORCL> desc viewf; Name Null? Type ----------------------------------------- -------- ---------------------------- MYPK VARCHAR2(6) PDB1@ORCL> insert into viewf values('a'); 1 row created. PDB1@ORCL> select * from table union select * from tablef; select * from table union select * from tablef * ERROR at line 1: ORA-00906: missing left parenthesis PDB1@ORCL> select * from tablea union select * from tablef; MYPK ------ data
基本結論以下:sql
使用union連接兩張表時列名以第一張表爲準express
當兩張表的對應咧的數據類型不一致時會發生自動轉換,若是自動轉換失敗則報錯code
向原始表中插入數據則view會同步更新同步
能夠直接向view中插入數據而在原始表中無反應io