![]() |
Duplicate entry avoidance on INSERT
I'm working on a game compatibility database. Here is the submission form.
http://oplinfo.hj.cx/insert.php I'd like to avoid duplicate entries into the database. Here is my INSERT INTO. Code:
$sql="INSERT INTO $tbl_name(gamename, gamecode, region, mode, vmc, smb, hdd, usb, notes, comp)VALUES('$gamename', '$gamecode', '$region', '$mode', '$vmc', '$smb', '$hdd', '$usb', '$notes', '$comp')";So, if user A submits a game to the database that has the gamecode "ABCD-12345", and user B comes along and tries to supply the same gamecode for their entry, I'd like that submit to fail. Any tip appreciated! |
gamecode should be the primary key for the table.
|
And if for some reason you already have a different primary key on the table (why?), then just add an index:
Code:
CREATE UNIQUE INDEX gamecode_index ON yourTableName(gamecode);PLEASE don't tell us that means you have more than one table with this same structure. If so, odds are 20 to 1 you have a poor database structure. |
Code:
$tbl_name="opl_comp";What if I had the same table structure for other tables representing different versions of software compatibility... why would this be bad structure? I use a primary key column called "ID", which is set to A_I in order to call each row individually so users can perform edits and update the list publicly. Code:
$game_id = isset($_GET['game_id']) ? (int)$_GET['game_id'] : 0;I have added an index as suggested, but how can I use this to avoid duplicate entries on INSERT? |
Well, the best idea would be to drop your useless ID column and then set GAMECODE as your primary key.
Adding an auto_increment as a primary key is kind of a "last resort" when there is no other good candidate. Since you stated that GAMECODE must be unique, then clearly it is the perfect choice for a primary key. In any case, whether primary key or unique key, if you try to insert another record with the same GAMECODE value, you *will* get an error. Trap that error in your (assumed) PHP code, and presto. You are there. ********** There are many reasons NOT to have multiple tables with the same structure and many reasons to combine them into one table. Think about it: If you need multiple tables only because of one feature ("compatibility") then simply add one column to your single table (named "compatibility" or whatever makes sense to you) and now you have all the benefits of multiple tables plus the huge benefit of a unified table. Let's just take one "for instance": Quote:
And *PLEASE* don't fall victim to that old "But I'll have too many entries in such a big table" plaint. MySQL can QUITE EASILY handle *MILLIONS* of records in a single table without batting an eye. By the way, if your GAMECODE does *NOT* imply a particular "compatibilty" and you want to have games with the same GAMECODE but differing COMPATIBILITY values, the answer is easy: Make a *COMBINED* PRIMARY KEY, consisting of both those columns. |
Quote:
For any situation other than that simply adding an extra field is the most efficient alternative. It is really amazing the number of people who add unnecessary id fields to their database tables and then wonder why they have trouble detecting duplicate inserts - which would not occur if they used the right key in the first place. |
We are in violent agreement. On all points. How strange <grin style="sheepish" />
And I amazed at the number of people who don't realize that a primary key can be a composite key. I actually have some tables with only 3 fields where the primary key is...all three fields! (And because of MySQL's mildly braindead use of indexes, it's mightily important what order the field are listed in the composite primary key. But oh man, when it's done right does it give wonderful performance!) |
| All times are GMT +1. The time now is 03:36 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.