Solved

Inserting only unique into MySQL

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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