Solved

MySQL > INSERT IGNORE INTO > Only inserts one row

Posted on 2013-10-28
4
411 Views
Last Modified: 2013-10-28
Why is this query only inserting one row?
CREATE TABLE search_terms_new  ( 
	search_terms_id	mediumint(9) NOT NULL,
	search_terms   	varchar(255) NOT NULL,
	PRIMARY KEY(search_terms_id)
)
ENGINE = InnoDB
AUTO_INCREMENT = 0
GO
ALTER TABLE search_terms_new
	ADD CONSTRAINT the_unique
	UNIQUE (search_terms)
GO

Open in new window

INSERT IGNORE INTO server_logs.search_terms_new (search_terms_id,search_terms)
SELECT
    LAST_INSERT_ID()+1,
    search_terms
FROM
    server_logs.search_terms

Open in new window

Result:
 1 record(s) affected 

 [Executed: 10/28/2013 4:35:49 PM] [Execution: 5s] 

Open in new window


If I run the query again, I get:
 0 record(s) affected 

 [Executed: 10/28/2013 4:36:25 PM] [Execution: 4s] 

Open in new window

0
Comment
Question by:Geoff Millikan
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 350 total points
Comment Utility
You have forgotten to add AUTO_INCREMENT to the column definition for search_terms_id.
The proper table structure should have been:
CREATE TABLE search_terms_new  ( 
	search_terms_id	mediumint(9) AUTO_INCREMENT NOT NULL,
	search_terms   	varchar(255) NOT NULL,
	PRIMARY KEY(search_terms_id)
)

Open in new window

0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 150 total points
Comment Utility
LAST_INSERT_ID will only return the last automatically generated ID. Because you are manually inserting the ID, LAST_INSERT_ID won't change!
0
 

Author Comment

by:Geoff Millikan
Comment Utility
Computers are so dang picky.  Why can't they just do what I want, instead of exactly what I tell them to do?  I mean really, who ever says EXACTLY what they want all the time?  These machines need to stop being so literal.
0
 

Author Closing Comment

by:Geoff Millikan
Comment Utility
Thanks so much, that fixed it!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now