ali raha
ali raha

Reputation: 221

Create if not exists else update tables

I want to create table if not exists,else update it.

this code is for create table:

CREATE TABLE Book 
ID     INT(10) PRIMARY KEY AUTOINCREMENT,
Name   VARCHAR(60) UNIQUE,
TypeID INT(10),
Level  INT(10),
Seen   INT(10)

how can I change it to support update too?

//EDIT

I mean if I add a column,only add a column...not remove last data

If I remove a columns (for example remove TypeID INT(10) from the command) just that columns be remove...not all data

Upvotes: 1

Views: 2983

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93734

You can use INFORMATION_SCHEMA.TABLES to check the existence of tables

IF EXISTS(SELECT table_name 
            FROM INFORMATION_SCHEMA.TABLES
           WHERE table_schema = 'Databasename'
             AND table_name = 'tablename')

THEN
   ....
   ALTER TABLE Tablename...
   ....
ELSE  
   ....
   CREATE TABLE tablename...
   ....
END IF;

Upvotes: 1

Related Questions