473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

INSERT query ( Special case )

539 512MB
Good day,

I dont know if this would be called a special case since this will be my first time to have an idea where i would like to insert a value from the table with a condition

Is it possible?

For example:

I have a table 'tablename', and has a column 'values' and expected to put millions of unique values in that table. The constraints would be, that column will also accept NULL value and should not be set to primary key or unique(just following the specification). Therefore i must check if the new value is already exists on that table.

ALGO:
1. receive new value
2. check if the new value exists on the table
3. if not exists, add, else, dont

The following algorithm is simple and straight forward, however, when the number of rows will already on the hundreds of thousands in count, i observed the slight overhead... and when those rows will be on approximately a million or more... the system will be on its worst performance. Therefore, a question arises and that would be:

Is it possible to insert a value with a condition? like:

Expand|Select|Wrap|Line Numbers
  1. insert newvalue into tablename where newvalue doesnt exists in tablename
Im looking forward to your replies experts
Jan 4 '10 #1

✓ answered by nbiswas

Suppose I have 2 tables.

TableSource

Expand|Select|Wrap|Line Numbers
  1. data
  2. abc -books
  3. new books
TableDestination
Expand|Select|Wrap|Line Numbers
  1. data
  2. abc -books
  3. acc -papers
Goal is to insert those records from TableSource that are not in TableDestination

Query1:

insert into TableDestination
select t2.data from TableSource t2
left join TableDestination t1 on t1.data = t2.data
where t1.data is null

select * from TableDestination

Query2:

insert into TableDestination
select data from TableSource
where data not in
(select data from TableDestination)

Output:

Expand|Select|Wrap|Line Numbers
  1. data
  2. abc -books
  3. acc -papers
  4. new books

3 2543
code green
1,726 Expert 1GB
Various ways.
SELECT all ids from the table into an array then use INSERT... WHERE... NOT IN(array).
INSERT with NOT IN sub-query that SELECT all ids.
Use SELECT to check existence of individual id before INSERT.
Use INSERT ON DUPLICATE KEY UPDATE but simply update with same value
Jan 5 '10 #2
nbiswas
149 100+
Suppose I have 2 tables.

TableSource

Expand|Select|Wrap|Line Numbers
  1. data
  2. abc -books
  3. new books
TableDestination
Expand|Select|Wrap|Line Numbers
  1. data
  2. abc -books
  3. acc -papers
Goal is to insert those records from TableSource that are not in TableDestination

Query1:

insert into TableDestination
select t2.data from TableSource t2
left join TableDestination t1 on t1.data = t2.data
where t1.data is null

select * from TableDestination

Query2:

insert into TableDestination
select data from TableSource
where data not in
(select data from TableDestination)

Output:

Expand|Select|Wrap|Line Numbers
  1. data
  2. abc -books
  3. acc -papers
  4. new books
Jan 6 '10 #3
sukatoa
539 512MB
Thank you codegreen and nbiswas for your replies :)
Jan 6 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: php newbie | last post by:
Hello, I am trying to load a simple tab-delimited data file to SQL Server. I created a format file to go with it, since the data file differs from the destination table in number of columns. ...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
11
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
20
by: John Bailo | last post by:
I have a c# program that loops through a table on a DB2 database. On each iteration it assigns data to values in the SqlParameter collection. The command text is an INSERT statement to a Sql...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
4
by: Bob | last post by:
Hi all, I'm trying to import data, modify the data then insert it into a new table. The code below works fine for it but it takes a really long time for 15,000 odd records. Is there a way I...
6
by: TheRealDan | last post by:
Hi all. I'm having a problem with a special characters. I have php script that reads from an xml file and writes to a mysql db. It's a script called phptunest that I found on the net, although the...
2
by: franc sutherland | last post by:
Hello, I am using Access 2003. Is it possible to use string variables in the INSERT INTO statement? I am using the INSERT INTO statement to add a long list of contacts to a group by looping...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.