mysql和oracle数据库的union all操作的区别

(1)测试表

> show create table test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `utime` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.01 sec)


> show create table test2\G
*************************** 1. row ***************************
       Table: test2
Create Table: CREATE TABLE `test2` (
  `id` int(11) DEFAULT NULL,
  `utime` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

(2)union 操作

create view test_view as select id,utime from test1 union all select id,utime from test2;

> desc test_view;
+-------+-----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| id    | int(11)   | YES  |     | NULL    |       |
| utime | timestamp | YES  |     | NULL    |       |
+-------+-----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 create view test_view2 as select id,utime from test1 union all select utime,id  from test2;

> desc test_view2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(19) | YES  |     | NULL    |       |
| utime | varchar(19) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

可以看到在mysql数据库中union all前后字段数据类型不一致,会影响最终表的字段类型。

(3)在oracle中测试如下

SQL> create table test1(id int,utime timestamp);

Table created.

SQL> create table test2(id int,utime timestamp);

Table created.

SQL> create view test_view as select id,utime from test1 union all select id,utime from test2;

View created.

SQL> desc test_view;
 Name					   Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID						    NUMBER(38)
 UTIME						    TIMESTAMP(6)

SQL> create view test_view2 as select id,utime from test1 union all select utime,id  from test2;
create view test_view2 as select id,utime from test1 union all select utime,id	from test2
                                 *
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression

在oracle中,如果union all字段的数据类型不一致会直接报错。

猜你喜欢

转载自blog.csdn.net/weixin_41561862/article/details/109275149
今日推荐