List:General Discussion« Previous MessageNext Message »
From:Pooly Date:September 26 2005 9:06pm
Subject:Re: add a column if not exists
View as plain text  

2005/9/26, Peter Brawley <peter.brawley@stripped>:
> Claire,
>  >I want to check if a column exists in a table before I
>  >do an alter table to add it. How do I do this in
>  >mysql? Thanks.

other solution, do your query in all case and check for the return
error (if any).
mysql> alter table board add message varchar(255) not null default '';
ERROR 1060 (42S21): Duplicate column name 'message'

So, if you get back that error, the column already exists. So if it
exists the table is left untouched, otherwise it does what you want.
(yeah, Information_schema would be better, but 5.0 is in gamma)

Webzine Rock :
add a column if not existsClaire Lee26 Sep
  • Re: add a column if not existsPeter Brawley26 Sep
    • Re: add a column if not existsPooly26 Sep
  • Re: add a column if not existsGleb Paharenko27 Sep