0

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

Yoshi
  • 54,081
  • 14
  • 89
  • 103
ali raha
  • 211
  • 1
  • 2
  • 14
  • By "update," do you mean alter the table to add a column, or what? In SQL the word `update` means change the content of one or more rows. Please clarify your question. – O. Jones Dec 28 '14 at 14:01
  • Look up `alter table add column`. – Gordon Linoff Dec 28 '14 at 14:05
  • You have this tagged as php, suggesting that you are looking to create or alter tables on the fly using web pages. That seems like a very bad idea. Maybe you should take a look at what you are trying to accomplish. – Dan Bracuk Dec 28 '14 at 14:36

1 Answers1

0

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;
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172