0

How can I check if the column in MySQL exists using Bash script.

I want to write an if statement and create the column if it does not exist. For example:

col="thecolumn"

if [[ !col ]]; then
    db="use myDB; alter table myTable add column name varchar(30) not null;"
    mysql -u root -p123456aB "$db"
fi
Johny
  • 39
  • 1
  • 5
  • use information_Schema as shown in prior [stack article](http://stackoverflow.com/questions/5648420/get-all-columns-from-all-mysql-tables) `select * from information_schema.columns where table_schema = 'your_db' order by table_name,ordinal_position` – xQbert Aug 14 '15 at 13:53

1 Answers1

0

All the table structure info is in information_schema and you can query that.

https://dev.mysql.com/doc/refman/5.0/en/information-schema.html

Cosmin
  • 1,482
  • 12
  • 26