0

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

Continuation
  • 3,080
  • 5
  • 30
  • 38

1 Answers1

2

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
user9517
  • 115,471
  • 20
  • 215
  • 297