Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Issue while inserting records with union of select statements records

Hi
I have a siuation with the below example. I have table like this
create table t1(col 1 interger , col2 primary key, col3)
Now im trying to insert data into using union of multiple select statments and my query goes like this
insert into t1(col1, col2,col3)
select seqname.nexval,A,B from
(select A, B from TT1
union
Select A, B from T2
union
Select A, B from T3)
As I know union should remove duplicates and my insert statement should work fine without any duplicates. But rurnning the above query is giving unique constraint voilated error. So can anybody suggest where my assumption is going wrong
Answers
-
select 1 A, 2 B from dual
union
select 2 A, 2 B from dual;
notice something? You get both rows, with B having the value 2 in both - and if B is the primary key....
Union eliminates whole rows that are duplicates - not individual duplicate columns.
-
Hi,
As Paul said, UNION guarantees that the result set will not contain 2 identical rows; it says nothing about duplicate values in each column. That is, UNION guarantees that, if 2 (or more) rows of its output have the same value for col2, then they will have different values for col1 and/or col3. However, the primary key constraint will be violated if 2 (or more) rows have the same value from col2, no matter what is in col1 and col3.
Also, if t1 has any data when you do the INSERT, some of the new rows may have the same col2 value as an existing row in t1, and that could cause the error. You might want to use MERGE instead of INSERT in this case.
What are the results you are looking for? Post CREATE TABLE and INSERT statements for a little sample data for all the table involved (as the appear before the INSERT) and the results you want from that sample data (that is, the contents of t1 after the INSERT). Test (and, if necessary, correct) the CREATE TABLE and INSERT statements before you post them. Remember, you have to specify a datatype for each column.
See the forum FAQ: https://forums.oracle.com/message/9362002
-
Hi Frank,
Thanks for your reply. I have explained small part of my whole situation with some psuedo queries. I have a brand new table with no data existing prior to this insert statement. So I am stuck here to fill the table with union of this select queries..