# Database Discussions > MySQL >  Create a table if it does not exist

## fudun

Simple SQL question (mySQL):

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

How?

Please help....

----------


## ggarman

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

----------


## 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.

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

----------


## mist

> _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.

----------


## fudun

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...

----------


## gbarnett

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

----------

