Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inserting only unique into MySQL

Posted on 2004-09-11
4
Medium Priority
?
1,467 Views
Last Modified: 2012-08-13
I want to build a table in mysql of all of the unique url's that are accessing my script.  I don't want to do an INSERT for every record, as that will cause double records to be formed.   Is setting the table field which will hold the url as a primary key the way to do this?  I think mysql generates an error if you try to insert a duplicate primary key, so this doesn't feel like the most elegant way to build a table that contains only one of each value.  I'd like to avoid having to do a table lookup and then an insert, if possible.
0
Comment
Question by:siteup
[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
4 Comments
 
LVL 1

Expert Comment

by:theandrew
ID: 12035487
I have a script that does this.

Here are two ways of doing it:
1) First, do a DELETE in the database, to delete all of the records from the database that match that IP and that URL. Then do your insert. If you have a date field you will be able to see the last date/time that they were at that page.

2) Insert it anyways, leave the duplicates there (nice later for detailed reporting). Adjust your script for reporting the number of hits or whatever to: SELECT DISTINCT IP,URL FROM........   this will only pull the unique ip and url page combinations and wont show the duplicates.

Additionally:
If you did a SELECT URL,IP,COUNT(IP) AS COUNT from table GROUP BY IP,URL......

This would show you a count of how many times that person has viewed that page.

If you post your actual table names and fields i can spell out the exact code for ya.
0
 
LVL 3

Expert Comment

by:pat5star
ID: 12035555
You can add a unique index to your URL column which will then only allow unique values to be inserted. Then you have 2 options: 1) you can either query the database first to see if it contains the URL you will be attempting to insert or 2) you can just insert each time and catch the error that MySQL will produce if it already contains that URL.

Myself, I would choose option number 2. This way you aren't wasting time or effort querying the database first and your guaranteed that you won't have duplicate values.

-Pat
0
 
LVL 15

Expert Comment

by:JakobA
ID: 12036074
You can use the IGNORE keyword in your INSERT statement, then data rows containing an existing primary key value or unique index value are just ignored and not inserted.
see: http://dev.mysql.com/doc/mysql/en/INSERT.html
0
 
LVL 2

Accepted Solution

by:
afano earned 2000 total points
ID: 12040201
What you need is REPLACE instead of INSERT.

REPLACE, if it finds a pre-existing primary key will replace the row.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this article, we’ll look at how to deploy ProxySQL.
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

664 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