Forum Moderators: coopster

Message Too Old, No Replies

how to prevent duplicate entries in mysql?

         

shams

12:49 am on Oct 21, 2006 (gmt 0)

10+ Year Member



hi,
there is mysql table patients with id auto_increment, name and faher/name, how i can prevent duplicate entries for the same patient with name and fahter/name? thanks i dvanced for your help.

jatar_k

1:08 am on Oct 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when someone tries to insert search those fields, if you get a return then there is already one in there. Inform your user of this fact and don't insert it.

shams

1:22 am on Oct 21, 2006 (gmt 0)

10+ Year Member



how i can use unique in patients table for that purpos?

jatar_k

3:00 am on Oct 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



at some poitn when you have complex keys, which I assume is what you are looking for you are just better off doing a quick select first.

it is easy to do a select with name and father's name in your query and see if you get a result

coopster

6:47 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The only time that would be an issue is if you are trying to INSERT/UPDATE rows outside of using your PHP application, say from the command line or perhaps even using LOAD DATA INFILE.

You can create UNIQUE instances on a single key value real easy by adding the UNIQUE keyword to the column definition. If, however, it is a compound key (two or more columns combined must be unique), then you should add a UNIQUE index to your table definition (CREATE TABLE [dev.mysql.com]).


A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.