Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

Thanks

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

1 Answer 1

up vote 2 down vote accepted

You could do this with a bash script and a pipe

#!/bin/bash
DBNAME="YourDatabaseName"

TNAME=shard_
TVAL="0"

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

    echo "ALTER TABLE $TNAME$TVAL ...;"
    TVAL=$[$TVAL+1]
done

and to use it

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

Your Answer

 
discard

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.