oracle in the Union and Union all the difference

Previously did not know the Union and Union All in the end what is the difference, look good today, found on the Internet result looks like this, but still not very understanding, will personally verify yourself:

Union: the results of the two sets and set operations, not including duplicates, while default sorting rule;

Union All: two result sets and set operations, including duplicates, do not sort;

 

Here's a simple test (because it is a test, so sql code is written in a very simple, nothing very strictly regulated, just to understand the difference between the two)

Strict standard wording should first determine the database exists, the table exists and so constrained

The first step, building a database:

view plain

  1. Create database Test  
  2. go  
  3.   
  4. use Test  
  5. go  

The second step, to build the table:

view plain

  1. Create table Table1  
  2. (  
  3.     id int not null,  
  4.     name varchar(20) not null  
  5. )  
  6.   
  7. Create table Table2  
  8. (  
  9.     id int not null,  
  10.     name varchar(20) not null  
  11. )  

 

A third step of inserting the test data:

view plain

  1. Insert into Table1 values (1,'姚羽')  
  2. Insert into Table1 values ​​(2, 'side Bingbing')  
  3. Insert into Table1 values (3,'袁磊')  
  4.   
  5. Insert into Table2 values (1,'姚羽')  
  6. Insert into Table2 values (2,'柳春平')  
  7. Insert into Table2 values (3,'张永超')  
  8. Insert into Table2 values (4,'刘华健') 

The fourth step, the test begins:

view plain

  1. select * from Table1  
  2. select * from Table2  

 

Query results following the implementation of two tables

Can easily be seen, is inserted among the above test data, there is a duplicate

Then we look at the implementation of union look

view plain

  1. select * from Table1  
  2. union   
  3. select * from Table2  

 

 

Then perform union all to see

view plain

  1. select * from Table1  
  2. union all  
  3. select * from Table2  

Guess you like

Origin blog.csdn.net/mn_kw/article/details/89550914