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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 29th, 2000, 04:54 PM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Is there a better way to check and see if a certain value has already been inserted into a primary key field besides just doing a select * from .. where .. and seeing if it returns any rows before inserting the new record?

[This message has been edited by RyanP (edited June 29, 2000).]

Reply With Quote
  #2  
Old June 30th, 2000, 04:25 AM
lhallusa lhallusa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 43 lhallusa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to lhallusa
The only better way is to not have to check the primary key. If you use NOT NULL AUTO_INCREMENT then the primary key is automatically incremented every time you do an INSERT.
i.e.
CREATE TABLE sometable ( sid INTEGER NOT NULL AUTO_INCREMENT,
text1 VARCHAR(255),
text2 VARCHAR(255),
number1 INTEGER,
date1 DATE,
PRIMARY KEY (sid)
);
Then the key is automatically incremented when inserted into. i.e.

$query = "INSERT INTO sometable VALUES (NULL, '$text1val', '$text2val', $num1, now() )";

After executing the query the key is inserted automatically when it detects the NULL.

Then if you need to know the primary key value. In PHP you would use:
$primaryKey = mysql_insert_id($mysql_result);
Other than that I don't know of a better way.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Checking to see if an identical value for a primary key field already exists


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 3 hosted by Hostway