Solved

MySQL > INSERT IGNORE INTO > Only inserts one row

Posted on 2013-10-28
4
422 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Server - Looking to filter rows based on column value 3 60
MySQL recovery 7 57
Formating field inside mysql query 2 47
MySQL 6 47
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…

734 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