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
Claire Lee

2005-09-26, 1:23 pm

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

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql? unsub...sie.nctu.edu.tw

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

Pooly

2005-09-26, 8:24 pm

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

Gleb Paharenko

2005-09-27, 7:23 am

Hello.

You can parse the output of 'SHOW CREATE TABLE' or 'SHOW COLUMNS'. See:
http://dev.mysql.com/doc/mysql/en/show-columns.html
http://dev.mysql.com/doc/mysql/en/s...eate-table.html



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
>



--
For technical support contracts, goto https://order.mysql.com/?ref=ensita
This email is sponsored by Ensita.NET http://www.ensita.net/
__ ___ ___ ____ __
/ |/ /_ __/ __/ __ \/ / Gleb Paharenko
/ /|_/ / // /\ \/ /_/ / /__ Gleb.Paharenko@ensita.net
/_/ /_/\_, /___/\___\_\___/ MySQL AB / Ensita.NET
<___/ www.mysql.com




--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql? unsub...sie.nctu.edu.tw

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com