Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Actually, I have the exact same question as this topic, but none of those answers were quite what I was looking for. The response by blokhead:

CREATE TABLE IF NOT EXISTS my_table (
foo INT,
bar VARCHAR(25)
);

was close, but it didn't continue with what to do then. After (or before . . . ) I run something like that, how can I INSERT if it didn't run (or if I run it before and the table doesn't exist)?

Replies are listed 'Best First'.
Re: MySQL: Checking if a Table Exists
by Tomte (Priest) on Feb 18, 2003 at 07:28 UTC

    Without further information, what it is that you want to do and that doesn't work, it is hard to help you; nevertheless a few remarks:

    After you ran this CREATE IF NOT EXISTS statement, the table will exist, if there wasn't an error. See perldoc DBI for error-reporting and -handling

    You can then INSERT, UPDATE, DELETE as you like; maybe REPLACE is what you're looking for...but have a closer look on the searchable html docs with comments if you're using MySQL

    regards,
    tomte