Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have multiple MySQL tables with names of the form "Shard_0", "Shard_1", "Shard_2" ... "Shard_n" All of them have identical table structure. They all live in the same database.

Say I want to add a column to all those tables. Is there a way to do that programmatically?

Something like:

# pseudo code    
for i in range(n):
    tablename = "shard_"+str(i)
    ALTER TABLE tablename ...

Is it possible to do something like that? If so what language and/or library do I need?


share|improve this question
Cross-posting does not reflect well upon oneself, since all it does is show everyone that one has no idea of what they're doing. – Ignacio Vazquez-Abrams Oct 27 '10 at 6:43
up vote 2 down vote accepted

You could do this with a bash script and a pipe



echo "USE $DBNAME;"
while [ $TVAL -lt 10 ]

    echo "ALTER TABLE $TNAME$TVAL ...;"

and to use it

./scriptname | mysql -u user -p
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.