javasqlderbyjavadb

How we can add new Coulmn if not exist using SQL for Apache Derby Database/JavaDB


How we can add new Coulmn if not exist using SQL for Apache Derby Database/JavaDB

I know that we can add new coulmn using the SQL:

alter table Country ADD  Language  varchar(10)

But I need to add if not exist because sometimes this coulmn exisit and derby give me errors.


Solution

  • You can find out which columns already exist in the table by consulting the DatabaseMetadata: call the getColumns method. Here's the Javadoc: https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String)