|
Home > Archive > MySQL ODBC Connector > September 2005 > add a column if not exists
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
add a column if not exists
|
|
|
|
| Peter Brawley 2005-09-26, 1:23 pm |
| 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.
If you are using MySQL 5.0, query information_schema.columns
(http://dev.mysql.com/doc/mysql/en/columns-table.html) for the table and
column. Otherwise use SHOW COLUMNS FROM tablename.
PB
http://www.artfulsoftware.com
-----
Claire Lee wrote:
>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.
>
>Claire
>
> ____________________
____________________
__________
>Do You Yahoo!?
>Tired of spam? Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
>
>
>
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.6/111 - Release Date: 9/23/2005
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql? unsub...sie.nctu.edu.tw
| |
|
| Hi,
2005/9/26, Peter Brawley <peter.brawley@earthlink.net>:[color=darkred]
> Claire,
>
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)
--
Pooly
Webzine Rock : http://www.w-fenec.org/
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql? unsub...sie.nctu.edu.tw
| |
|
|
|
|
|