oracle view 的研究(一 union)

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

相关文章
相关标签/搜索