Solved

MySQL > INSERT IGNORE INTO > Only inserts one row

Posted on 2013-10-28
4
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 350 total points
ID: 39607358
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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 150 total points
ID: 39607390
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
ID: 39607426
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
ID: 39607427
Thanks so much, that fixed it!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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