Mysql Create Table (non-existing)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Mysql Create Table (non-existing)

Post by tecktalkcm0391 »

I have a page that creates a table in a database. How can I make this code so if the table doesn't exist create it. Do I just do a select table before and if it returns something don't create the table?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

http://dev.mysql.com/doc/refman/5.0/en/ ... table.html:

Code: Select all

CREATE TABLE IF NOT EXISTS tbl_name
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Mysql has an extension to the CREATE TABLE syntax
http://dev.mysql.com/doc/refman/5.0/en/create-table.html wrote:CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
but that's mysql specific.

But why not simply trying to create the table. If it already exists you get a specific error signaled.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP - Code :arrow: Databases. :?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks. I got it working...
feyd wrote:PHP - Code :arrow: Databases. :?
sorry. i forgot :oops:
Post Reply