469,167 Members | 1,146 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,167 developers. It's quick & easy.

HELP: Adding a field to a database table

When adding a field to a table using ALTER TABLE is it possible to check if
the field already exits before the ADD command is run? If so how do I do
this?

Thanks
Hamilton
Jul 23 '05 #1
3 1657
Funnyweb wrote:
When adding a field to a table using ALTER TABLE is it possible to check if
the field already exits before the ADD command is run? If so how do I do
this?


Read the following pages and see if you can find a solution that meets
your needs (you didn't state what programming language or environment
you are using):
http://dev.mysql.com/doc/mysql/en/show-columns.html
http://dev.mysql.com/doc/mysql/en/describe.html
http://dev.mysql.com/doc/mysql/en/mysqlshow.html

Also see a similar thread from earlier this week on this group:
http://groups-beta.google.com/group/...46b4f04e52c84/

Regards,
Bill K.
Jul 23 '05 #2
Thanks for the information.

I should have explained that I was talking about writing .sql files.

You can check if a table (and some other things) exist by using IF EXISTS.
I gather from reading the links you quoted that there is no such test for
columns (fields) in a table?

Hamilton

"Bill Karwin" <bi**@karwin.com> wrote in message
news:cv********@enews2.newsguy.com...
Funnyweb wrote:
When adding a field to a table using ALTER TABLE is it possible to check
if the field already exits before the ADD command is run? If so how do I
do this?


Read the following pages and see if you can find a solution that meets
your needs (you didn't state what programming language or environment you
are using):
http://dev.mysql.com/doc/mysql/en/show-columns.html
http://dev.mysql.com/doc/mysql/en/describe.html
http://dev.mysql.com/doc/mysql/en/mysqlshow.html

Also see a similar thread from earlier this week on this group:
http://groups-beta.google.com/group/...46b4f04e52c84/

Regards,
Bill K.

Jul 23 '05 #3
Funnyweb wrote:
Thanks for the information.

I should have explained that I was talking about writing .sql files.

You can check if a table (and some other things) exist by using IF EXISTS.
I gather from reading the links you quoted that there is no such test for
columns (fields) in a table?


As far as I know, that is correct. I don't think there is any solution
that can be written purely in .sql files.

If you have a scripting language, you could use the SHOW COLUMNS,
examine the output, and conditionally create the columns. But .sql
files are not a replacement for a more fully-featured scripting
language. That is, there's no SQL script equivalent to run a statement
conditionally based on the output of the previous statement.

One possibility is to run a script that contains ALTER TABLE ADD COLUMN
statements, which will fail if there is already a column by that name.
But you can run the script using the "mysql --force" so that the
command-line interpreter will continue even if some statements result in
an SQL error.

Regards,
Bill K.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Preston Landers | last post: by
3 posts views Thread by Jim Heavey | last post: by
15 posts views Thread by Jay | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.