MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 31st, 2000, 05:35 AM
ranesh_w ranesh_w is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Colombo, Western, Sri Lanka
Posts: 41 ranesh_w User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I'm in the process of creating a product catalog. Each product has a unique code which has to be entered by the user at the time of entering new products into the table.

How can I prevent duplicate codes from being inserted into the table??

Thanks,

Ranesh.


Reply With Quote
  #2  
Old August 31st, 2000, 09:26 AM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
do a select query with the new number, if the query returns any rows (mysql_num_rows($qh) > 0) display error, if not, insert the record into the table

Reply With Quote
  #3  
Old August 31st, 2000, 11:13 PM
ranesh_w ranesh_w is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Colombo, Western, Sri Lanka
Posts: 41 ranesh_w User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
What is $qh ?

(Please excuse my ignorance....I'm a newbie.)

thnx.

Ranesh.

Reply With Quote
  #4  
Old August 31st, 2000, 11:52 PM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
that's your query handler variable

Reply With Quote
  #5  
Old September 1st, 2000, 03:35 AM
christucker2 christucker2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Posts: 81 christucker2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
You definitely don't want to be doing it the way Ryan's just suggested unless you can possibly help it (sorry Ryan!). There are some major issues with the method that's been outlined which I'll detail in a moment. First, here's how you'd be better off doing it, if possible.

All you need to do to ensure that the DB doesn't get multiple rows with the same ID (which should be unique) is to specify that the column containing that unique ID can only contain unique values. This is done by specifying a key on the column -- it can either be the primary key (basically, just a unique indexed key), or simply a unique key. To add a unique key to your table use the following:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
ALTER TABLE mytable ADD UNIQUE (my_unique_column)
[/code]

If you're going to be doing a lot of lookups via this column, you can also make it all a lot faster by adding an index:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
ALTER TABLE mytable ADD INDEX (my_unique_column)
[/code]

So, now the reasons for not using the other method suggested:

1) You're relying on application logic to maintain the integrity of your data -- what if at some point you or someone else writes some other application logic that works with the same table, but forgets to add that integrity checking (or simply implements it incorrectly)

2) For every insert, you're needing at least two roundtrips to the server (one to check if the row exists and then one to insert it). This will double the time it takes for an insert to occur, and as a round trip can be quite slow (especially if at some point the DB is moved elsewhere on a network or something) it can have a serious impact on application performance

3) Probably most importantly, it doesn't work. You introduce an unpleasant race condition because you're working in a multi-user environment. Imagine the following sequence of events where User A and User B are both trying to insert '123' into the table:
User A checks if '123' is free. Query says yes.
User B checks if '123' is free. Query says yes.
User A inserts '123'
User B inserts '123'!
Because you can't control the interleaving of query/insert statements across multiple users (without using transactions, which are sloooow), it's possible for multiple users to check for the availability of a value before any of them insert, leading to the DB erroneously reporting to multiple users that they can use a single given value.

As you can imagine, situations like this are absolute buggers to debug, so you want to try to avoid them before they happen. Using a unique key approach to this problem, only one round trip is needed on average -- if the unique key constraint is violated (i.e. two people try to insert the same value), the DB will chuck an error back at you, which you can then trap and alert the user about to get a new key (or do what you will).

Reply With Quote
  #6  
Old September 1st, 2000, 11:17 AM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Chris,
No problem, I always welcome a better way to do things, and after your explanation I have seen the error of my ways. This will also prompt me to learn about PHP's error handling features.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > MySql newbie.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway