Joinutility seperatorLogin utility separator Infobright.com
   
1 of 2
1
Renaming tables in Infobright
Posted: 08 May 2009 06:47 PM   Ignore ]  
Member
Avatar
RankRankRank
Total Posts:  191
Joined  2008-10-20

I get asked on a regular basis as to how to rename a table. Unfortunately, today, we don’t have the alter table command. It is fortcoming later this year but for the meantime, I’ve written this simple script to do this and this appears to do the very same thing as alter table <table> rename to <new table> but outside the database.

The assumptions are -
1. you are running Infobright on Linux
2. you installed in the default locations, if not change the DATADIR variable to the correct location
3. you have table names without special characters that would throw the script off

#!/bin/bash
###########################################################
###                   Infobright 2009                   ###
###           Developed by: Client Services             ###
###           Authors: Carl Gelbart                     ###
###                     Version 0.1                     ###
###                                                     ###
###    FOR TESTING AND EVALUATION PURPOSES ONLY.  NO    ###
###   WARRANTY IS OFFERED AND NO LIABILITY IS ACCEPTED  ###
###        FOR INAPPROPRIATE USE OF THIS SOFTWARE.      ###
###########################################################
# Usage: rename_table <database name> <table_name> <new_table_name> 

#   rename_table will do the same as the alter SQL statement
#   alter table <table name> rename to <new table name>
#
if $# != 3 ]; then
    
echo; echo "syntax: rename_table "
    
echo "            <database name> "
    
echo "            <table name> "
    
echo "            <new table name> "
    
echo
    exit 
2
fi

DATADIR
=/usr/local/infobright/data
DBNAME
=$1
TABLE
=$2
NEW=$3

if -f $DATADIR/$DBNAME/$TABLE.frm ]
then
   mv $DATADIR
/$DBNAME/$TABLE.bht  $DATADIR/$DBNAME/$NEW.bht
   mv $DATADIR
/$DBNAME/$TABLE.frm  $DATADIR/$DBNAME/$NEW.frm
   sync
   
echo table renamed
else
   echo 
table not foundinsert quarter and try again
   
exit 28
fi
exit 
[ Edited: 26 May 2009 03:23 PM by CarlGelbart]
Signature 
Profile
 
Posted: 09 May 2009 09:16 AM   Ignore ]   [ # 1 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  738
Joined  2008-08-18
CarlGelbart - 08 May 2009 06:47 PM

insert quarter and try again

How do I get the quarter out of my DVD bay?

Signature 
Profile
 
Posted: 09 May 2009 10:16 AM   Ignore ]   [ # 2 ]  
Member
Avatar
RankRankRank
Total Posts:  191
Joined  2008-10-20

Do not take the quarters out, keep inserting them and I’ll be by to collect the quarters to help fund my research.

[ Edited: 09 May 2009 10:28 AM by CarlGelbart]
Signature 
Profile
 
Posted: 09 May 2009 10:21 AM   Ignore ]   [ # 3 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  738
Joined  2008-08-18

I was able to remove the Canadian quarters with a magnet but now my laptop is acting weird…

Signature 
Profile
 
Posted: 09 May 2009 10:49 AM   Ignore ]   [ # 4 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  738
Joined  2008-08-18
CarlGelbart - 09 May 2009 10:16 AM

Do not take the quarters out, keep inserting them and I’ll be by to collect the quarters to help fund my research.

And what research would that be, sir?

Signature 
Profile
 
Posted: 09 May 2009 11:03 AM   Ignore ]   [ # 5 ]  
Member
Avatar
RankRankRank
Total Posts:  191
Joined  2008-10-20

To build a complete database written entirely using bash shell scripts.

Signature 
Profile
 
Posted: 09 May 2009 12:55 PM   Ignore ]   [ # 6 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  738
Joined  2008-08-18

I commend your Don Quixote-esque quest.  For reassurance, I will tell you that I’ve seen entire data mining engines written in shell scripts!

Signature 
Profile
 
Posted: 11 May 2009 01:28 AM   Ignore ]   [ # 7 ]  
Jr. Member
RankRank
Total Posts:  75
Joined  2008-10-22

entire databases written in shell scripts ! Ha, what a beginner’s quest! A DB Engine based on AWK is the real thing

Signature 

Chris (cvh@LE),
Leipzig, Germany

Profile
 
Posted: 11 May 2009 09:22 AM   Ignore ]   [ # 8 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  505
Joined  2008-08-18
CarlGelbart - 08 May 2009 06:47 PM
mv $DATADIR/$DBNAME/$TABLE.bht  $DATADIR/$DBNAME/$NEW.bht
   mv $DATADIR
/$DBNAME/$TABLE.frm  $DATADIR/$DBNAME/$NEW.frm 

Are you sure this won’t screw up the Knowledge Grid? How will it know how to find these tables again?

Geoffrey

Signature 
Profile
 
Posted: 11 May 2009 10:52 AM   Ignore ]   [ # 9 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  764
Joined  2008-08-18

Hi,

No problems with KG, they’re using internal table numbers, independent on table names. The only problem I can see is that logs in “bh.err” may contain old names.

Regards,

Signature 
Profile
 
Posted: 11 May 2009 11:02 AM   Ignore ]   [ # 10 ]  
Member
Avatar
RankRankRank
Total Posts:  191
Joined  2008-10-20

I also want to make sure that people understood that this works no better than alter table rename in that privileges specificallty granted to the table have to be dealt with. This is an excerpt from the MySQL 5.1 manual in regards to alter table rename -

Any privileges granted specifically for the renamed table or view are not migrated to the new name. They must be changed manually.

Signature 
Profile
 
Posted: 11 May 2009 11:04 AM   Ignore ]   [ # 11 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  738
Joined  2008-08-18

I’ve tested it 6 ways to Sunday and have had no issues.  But I’m not an official tester nor do I play one on TV.

Signature 
Profile
 
Posted: 11 May 2009 11:49 AM   Ignore ]   [ # 12 ]  
Administrator
Avatar
Rank
Total Posts:  41
Joined  2009-01-19

Thanks for stepping into the white spaces, Carl.

The only things that I would add to this (which I didn’t see in your original script) are:

1. Users should ensure that the mysqld-ib server is not running when the script is run.
2. There is a tacit assumption in this script that the file system structure contains the files and naming conventions as described

(1) is important because there are a number of in memory structures that depend on the file system - no internal files should be changed while the server is running.

For (2) - we should make sure that the users know that our file system is not a supported interface - we can, and very likely will, change the file system structure in the future in ways that may or may not affect the script above. Perhaps a good way of saying it is that the script will work for version 3.1.1-p1, but that it will probably not work for future versions.


Of course, ALTER TABLE ... RENAME ... is on the product roadmap for IB 3.5, so the wait should not be too long.

.g.

Profile
 
Posted: 11 May 2009 12:30 PM   Ignore ]   [ # 13 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  738
Joined  2008-08-18
cvh@LE (Chris) - 11 May 2009 01:28 AM

A DB Engine based on AWK is the real thing

That would be awksome!

Signature 
Profile
 
Posted: 13 May 2009 01:17 PM   Ignore ]   [ # 14 ]  
Member
Avatar
RankRankRank
Total Posts:  191
Joined  2008-10-20

This works well for me with the database running. After the rename_table script is run, doing a show tables shows the new names and the new names can be queried.

Signature 
Profile
 
Posted: 15 May 2009 04:04 AM   Ignore ]   [ # 15 ]  
Jr. Member
RankRank
Total Posts:  87
Joined  2008-08-18

Hi,

Renaming table in this way is not safe if the server is running. This will for sure cause some memory leaks but also may lead to more serious consequences. If the table that is being renamed is currently in use by some query then this operation may crash the server or even corrupt the data.
I would recommend making sure that the mysqld-ib is not running before executing the script. It just will be safer.
I would also add call of ‘sync’ to the end of the script. To make sure that the new folders structure is physically written to the hard drive.

Cheers,

Profile
 
   
1 of 2
1