Solved

Inserting only unique into MySQL

Posted on 2004-09-11
4
1,460 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
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 500 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

13 Experts available now in Live!

Get 1:1 Help Now