Results 1 to 6 of 6

Thread: Create a table if it does not exist

  1. #1
    Join Date
    Nov 2002
    Posts
    3

    Question Create a table if it does not exist

    Simple SQL question (mySQL):

    I want to create a table if it does not already exist.

    How?

    Please help....

  2. #2
    Join Date
    Nov 2002
    Posts
    6
    I'm not familiar with MySQL, but here's a link to the SQL reference on msdn:

    http://msdn.microsoft.com/library/de...eate2_8g9x.asp

    This is the code I used:

    Create Table MyNewTable ({field-name} {field type}).

    For example: Create Table MyNewTable (MyField memo)

    Hope this helps,
    Greg

  3. #3
    Join Date
    Nov 2002
    Posts
    3
    Hello?

    How does that check whether the table already exists?

    I think you misunderstood my question.

    I know there are some advanced SQL functions, like IF EXISTS <table> or something.

    But I haven't found any examples describing it. It is a really simple task, and I can make it work by some other ugly code in my application.

    But what I really wanted, was a nice SQL sentence that did all the work and looked a lot nicer...


    HÃ¥kon

  4. #4
    Join Date
    Oct 2002
    Posts
    42
    Originally posted by fudun
    Hello?

    How does that check whether the table already exists?

    I think you misunderstood my question.

    I know there are some advanced SQL functions, like IF EXISTS <table> or something.

    if not exists (select * from dbo.sysobjects where name = 'table_name')

    is how you would do it in Sql Server. You probably need to find the equivalent of the sysobjects table in mysql, and query that. sysobjects is the table which contains all the objects in the database, I would imagine that there is a similar thing in mysql.

  5. #5
    Join Date
    Nov 2002
    Posts
    3
    Thanks.

    I'll look into that when I get time for it.

    But if anyone can tell me exactly how this is done in mySQL, I'd be more than happy...

  6. #6
    Join Date
    Dec 2002
    Posts
    4
    This is the sql syntax for create table. Utilise the IF NOT EXISTS clause....

    CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]

    HTH
    Graeme

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •