Solved

Inserting only unique into MySQL

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

8 Experts available now in Live!

Get 1:1 Help Now