1. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    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).]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Rep Power
    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.
    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.

Similar Threads

  1. No primary key needed for entity bean.
    By dsho in forum Java Help
    Replies: 0
    Last Post: October 29th, 2003, 03:09 AM
  2. newbie with postgres
    By edwinx in forum PostgreSQL Help
    Replies: 5
    Last Post: October 9th, 2003, 05:21 PM
  3. Replies: 10
    Last Post: August 21st, 2001, 04:46 PM
  4. Database design question
    By mcsimon in forum Dev Shed Lounge
    Replies: 2
    Last Post: August 5th, 2001, 12:17 AM
  5. primary key problem
    By lalala in forum MySQL Help
    Replies: 2
    Last Post: November 14th, 2000, 08:51 PM

IMN logo majestic logo threadwatch logo seochat tools logo