Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Checking for Dupes in MySQL

by bagelesque (Initiate)
on Nov 29, 2001 at 14:41 UTC ( [id://128318]=perlquestion: print w/replies, xml ) Need Help??

bagelesque has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of e-mail addresses for members of my site. Now I want to be able to prevent duplicate instances of the same member but I also don't want to have to do a SELECT each time I add a group of people. Is there something to issue in PERL to check without connecting to MySQL or is there a MySQL technique to be able to do this without so many connections?

Replies are listed 'Best First'.
Re: Checking for Dupes in MySQL
by jeroenes (Priest) on Nov 29, 2001 at 14:54 UTC
    Standard SQL (dunno whether mysql supports it): add a UNIQUE modifier in the CREATE TABLE statement at the e-mail column. Afterwards, your dbase won't allow duplicate e-mail addresses.

    Be sure to add decent error-checking to inform your prospective users why their entry was not allowed.

    Jeroen
    "We are not alone"(FZ)

      MySQL is SQL92 compliant.

      --
      perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Checking for Dupes in MySQL
by MZSanford (Curate) on Nov 29, 2001 at 14:59 UTC
    If you make email address a primary key on the table storring the information in mysql, it will prevent dups. This may have other effects (performance, etc), i have only used mysql once, so i am not sure. Also, once you do this it will actually error when you try to insert new rows, so it makes it trickier to do inert error checking.

    Also, a word of caution, to prevent dup emailing, normalize the email addresses before you load them. This may seem silly, but the RFC allows for the same email address to be written a million ways (well, nearly infinte if you think about number of character/position combonations).

    you could also query the whole table out into a hash.BUT i do NOT suggest that ... talk about missing the scalability bus.
    i had a memory leak once, and it ruined my favorite shirt.
      SELECT DISTINCT $field FROM $table

      Will omit all duplicate values occurring in field

        SELECT DISTINCT will only prevent you from seeing duplicates. It will not prevent duplicates from being inserted.

Re: Checking for Dupes in MySQL
by graq (Curate) on Nov 29, 2001 at 20:38 UTC
    Is there something to issue in PERL to check without connecting to MySQL

    No. Your data is held in the database, you will need to connect to and query the database to find out what is in it. So this appears very much to be a mySQL question, not Perl.

    To get it back to Perl, perhaps you could have a button (on the page where you add new people) that fetches the names and email address of those currently in the database. You could put these in nice table tags (and hidden tags) and check against them in the target perl script, using CGI, before doing your database connection.

    But, as stated in previous replies, the mySQL homepage is an adequate place to learn about the PRIMARY KEY and UNIQUE attributes available in mySQL.

    To be honest, if what you really want is exactly (and no more than) what you described in your question, I might consider PHP. But don't tell anyone that I said that!

    <a href="http://www.graq.co.uk">Graq</a>

Re: Checking for Dupes in MySQL
by grinder (Bishop) on Nov 29, 2001 at 22:52 UTC

    Many good suggestions concerning unique.

    Another technique is available for checking whether a given value appears in a column, without any needing to fetch any data from your backend, and that is:

    select 1 from mytable where mycol='foo'

    You'll either get a row back, or nothing. In the general case, this is about as cheap as it gets.

    --
    g r i n d e r
      I agree with grinder here. I would think this is the best method of inserting unique rows into a database.

      The table should have the unique constraint enabled to enforce the integrity. Good application design would *not* have the integrity enforced externaly

      In my mind it would be better to make to graceful calls to the database, rather than making one and traping any errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128318]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found