This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes insert unique name into the table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "insert unique name into the table" Watch "insert unique name into the table" New topic
Author

insert unique name into the table

vikas sharmaa
Ranch Hand

Joined: Jun 28, 2007
Posts: 191
i've an Emp table with empid, name, and loc columns. now, in java, query is written to insert records into Emp table from MyTable as follows:

insert into Emp (empid, name, loc) select myseq.nextval, name, loc from MyTable

in MyTable table, there are duplicate records for name column. So, those duplicate name are getting inserted into the Emp table.

how could i modify the query to insert unique name into the Emp table.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Vikas,

Please refer to the Oracle documentation for more details.

Good Luck,
Avi.
vikas sharmaa
Ranch Hand

Joined: Jun 28, 2007
Posts: 191
avi, distinct can be used in simple select sql query.

here if i use, distinct as below:

insert into Emp (empid, name, loc) select myseq.nextval, distinct name, loc from MyTable

it will give sql error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
You use distinct after "select." If you want the names to be unique (which is probably incorrect; there will be people with the same name as each other), you need to
  • Label the column UNIQUE when you create the table or
  • Use an ALTER TABLE names MODIFY COLUMN . . . instruction.
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: insert unique name into the table
     
    Similar Threads
    how can i get column name of a table
    How to delete duplicate rows
    delete sql
    Report Query
    Hibernate association mapping