Problem Solution Discussion Cloning a Table Exactly

happens. This m eans it can be prudent t o issue t he following st at em ent prior t o creat ing a t em porary t able, j ust in case it s st ill hanging around from t he previous execut ion of t he script : DROP TABLE IF EXISTS tbl_name • I f you m odify a t em porary t able t hat hides a perm anent t able wit h t he sam e nam e, be sure t o t est for errors result ing from dropped connect ions. I f a client program aut om at ically reconnect s aft er a dropped connect ion, youll be m odifying t he original t able aft er t he reconnect .

3.26 Cloning a Table Exactly

3.26.1 Problem

You need an exact copy of a t able, and CREATE TABLE ... SELECT doesnt suit your purposes because t he copy m ust include t he sam e indexes, default values, and so fort h.

3.26.2 Solution

Use SHOW CREATE TABLE t o get a CREATE TABLE st at em ent t hat specifies t he source t ables st ruct ure, indexes and all. Then m odify t he st at em ent t o change t he t able nam e t o t hat of t he clone t able and execut e t he st at em ent . I f you need t he t able cont ent s copied as well, issue an INSERT INTO ... SELECT st at em ent , t oo.

3.26.3 Discussion

Because CREATE TABLE ... SELECT does not copy indexes or t he full set of colum n at t ribut es, it doesnt necessarily creat e a dest inat ion t able as an exact copy of t he source t able. Because of t hat , you m ight find it m ore useful t o issue a SHOW CREATE TABLE query for t he source t able. This st at em ent is available as of MySQL 3.23.20; it ret urns a row cont aining t he t able nam e and a CREATE TABLE st at em ent t hat corresponds t o t he t ables st ruct ure—including it s indexes keys , colum n at t ribut es, and t able t ype: mysql SHOW CREATE TABLE mail\G 1. row Table: mail Create Table: CREATE TABLE `mail` `t` datetime default NULL, `srcuser` char8 default NULL, `srchost` char20 default NULL, `dstuser` char8 default NULL, `dsthost` char20 default NULL, `size` bigint20 default NULL, KEY `t` `t` TYPE=MyISAM By issuing a SHOW CREATE TABLE st at em ent from wit hin a program and perform ing a st ring replacem ent t o change t he t able nam e, you obt ain a st at em ent t hat can be execut ed t o creat e a new t able wit h t he sam e st ruct ure as t he original. The following Pyt hon funct ion t akes t hree argum ent s a connect ion obj ect , and t he nam es of t he source and dest inat ion t ables . I t ret rieves t he CREATE TABLE st at em ent for t he source t able, m odifies it t o nam e t he dest inat ion t able, and ret urns t he result : Generate a CREATE TABLE statement to create dst_tbl with the same structure as the existing table src_tbl. Return None if an error occurs. Requires the re module. def gen_clone_query conn, src_tbl, dst_tbl: try: cursor = conn.cursor cursor.execute SHOW CREATE TABLE + src_tbl row = cursor.fetchone cursor.close if row == None: query = None else: Replace src_tbl with dst_tbl in the CREATE TABLE statement query = re.sub CREATE TABLE .` + src_tbl + `, CREATE TABLE ` + dst_tbl + `, row[1] except: query = None return query You can execut e t he result ing st at em ent as is t o creat e t he new t able if you like: query = gen_clone_query conn, old_tbl, new_tbl cursor = conn.cursor cursor.execute query cursor.close Or you can get m ore creat ive. For exam ple, t o creat e a t em porary t able rat her t han a perm anent one, change CREATE t o CREATE TEMPORARY before execut ing t he st at em ent : query = gen_clone_query conn, old_tbl, new_tbl query = re.sub CREATE , CREATE TEMPORARY , query cursor = conn.cursor cursor.execute query cursor.close Execut ing t he st at em ent ret urned by gen_clone_query creat es an em pt y copy of t he source t able. To copy t he cont ent s as well, do som et hing like t his aft er creat ing t he copy: cursor = conn.cursor cursor.execute INSERT INTO + new_tbl + SELECT FROM + old_tbl cursor.close Prior t o MySQL 3.23.50, t here are a few at t ribut es t hat you can specify in a CREATE TABLE st at em ent t hat SHOW CREATE TABLE does not display. I f your source t able was creat ed wit h any of t hese at t ribut es, t he cloning t echnique shown here will creat e a dest inat ion t able t hat does not have quit e t he sam e st ruct ure. t able t hat does not have quit e t he sam e st ruct ure.

3.27 Generating Unique Table Names