?
Solved

MySQL > INSERT IGNORE INTO > Only inserts one row

Posted on 2013-10-28
4
Medium Priority
?
424 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 1400 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 600 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

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.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 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