#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Frankfurt am Main, Germany
    Posts
    16
    Rep Power
    0

    Question How to check if index exists ?


    Hi everyone,

    is there any way to find out if an index already exists in a table for a specific field?

    I made an install script that automatically creates some tables and places some indexes. Just wanted to make sure that an index isn't set twice just in cace a user runs the script twice.

    Thougt of something like:
    IF NOT EXISTS INDEX(whatever).....

    This is my original PHP-query to create the index:
    PHP Code:
     @$setindex mysql_query("ALTER TABLE ".$tablename." ADD INDEX (browserstamp)"); 
    Any ideas on how to change that line would be very helpfull!

    Greetings
    Echodyne
  2. #2
  3. Wiking
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    3,608
    Rep Power
    26
    Use either DESCRIBE table or SHOW INDEX FROM table and parse the output from that. Then you'll know if there's an index or not.
    But if you fear that they run the script twice, why not go for checking if the table exists first?
    Like CREATE TABLE IF NOT EXISTS...

    //NoXcuz
    UN*X is sexy!
    who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep

IMN logo majestic logo threadwatch logo seochat tools logo