Problem Solution Discussion Creating a Destination Table on the Fly from a Result Set

3.23 Creating a Destination Table on the Fly from a Result Set

3.23.1 Problem

You w ant t o run a SELECT query and save t he result set int o anot her t able, but t hat t able doesnt exist yet .

3.23.2 Solution

Creat e t he dest inat ion t able first , or creat e it direct ly from t he result of t he SELECT .

3.23.3 Discussion

I f t he dest inat ion t able does not exist , you can creat e it first wit h a CREATE TABLE st at em ent , t hen copy rows int o it wit h INSERT ... SELECT as described in Recipe 3.22 . This t echnique works for any version of MySQL. I n MySQL 3.23 and up, a second opt ion is t o use CREATE TABLE ... SELECT , which creat es t he dest inat ion t able direct ly from t he result of a SELECT . For exam ple, t o creat e dst_tbl and copy t he ent ire cont ent s of src_tbl int o it , do t his: CREATE TABLE dst_tbl SELECT FROM src_tbl; MySQL creat es t he colum ns in dst_tbl based on t he nam e, num ber, and t ype of t he colum ns in src_tbl . Add an appropriat e WHERE clause, should you wish t o copy only cert ain rows. I f you want t o creat e an em pt y t able, use a WHERE clause t hat is always false: CREATE TABLE dst_tbl SELECT FROM src_tbl WHERE 0; To copy only som e of t he colum ns, nam e t he ones you want in t he SELECT part of t he st at em ent . For exam ple, if src_tbl cont ains colum ns a , b , c , and d , you can copy j ust b and d like t his: CREATE TABLE dst_tbl SELECT b, d FROM src_tbl; To creat e colum ns in a different order t han t hat in which t hey appear in t he source t able, j ust nam e t hem in t he desired order. I f t he source t able cont ains colum ns a , b , and c , but you want t hem t o appear in t he dest inat ion t able in t he order c , a , and b , do t his: CREATE TABLE dst_tbl SELECT c, a, b FROM src_tbl; To creat e addit ional colum ns in t he dest inat ion t able besides t hose select ed from t he source t able, provide appropriat e colum n definit ions in t he CREATE TABLE part of t he st at em ent . The following st at em ent creat es id as an AUTO_INCREMENT colum n in dst_tbl , and adds colum ns a , b , and c from src_tbl : CREATE TABLE dst_tbl id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY id SELECT a, b, c FROM src_tbl; The result ing t able cont ains four colum ns in t he order id , a , b , c . Defined colum ns are assigned t heir default values. This m eans t hat id , being an AUTO_INCREMENT colum n, will be assigned successive sequence num bers st art ing from one. See Recipe 11.2 . I f you derive a colum ns values from an expression, it s prudent t o provide an alias t o give t he colum n a nam e. Suppose src_tbl cont ains invoice inform at ion list ing it em s in each invoice. Then t he following st at em ent generat es a sum m ary of each invoice nam ed in t he t able, along wit h t he t ot al cost of it s it em s. The second colum n includes an alias because t he default nam e for an expression is t he expression it self, which is difficult t o work wit h: CREATE TABLE dst_tbl SELECT inv_no, SUMunit_costquantity AS total_cost FROM src_tbl GROUP BY inv_no; I n fact , prior t o MySQL 3.23.6, t he alias is required, not j ust advisable; colum n nam ing rules are st rict er and an expression is not a legal nam e for a colum n in a t able. CREATE TABLE ... SELECT is ext rem ely convenient , but does have som e lim it at ions. These st em prim arily from t he fact t hat t he inform at ion available from a result set is not as ext ensive as what you can specify in a CREATE TABLE st at em ent . I f you derive a t able colum n from an expression, for exam ple, MySQL has no idea whet her or not t he colum n should be indexed or what it s default value is. I f it s im port ant t o include t his inform at ion in t he dest inat ion t able, use t he following t echniques: • I f you want indexes in t he dest inat ion t able, you can specify t hem explicit ly. For exam ple, if src_tbl has a PRIMARY KEY on t he id colum n, and a m ult iple-colum n index on state and city , you can specify t hem for dst_tbl as well: • CREATE TABLE dst_tbl PRIMARY KEY id, INDEXstate,city SELECT FROM src_tbl; • Colum n at t ribut es such as AUTO_INCREMENT and a colum ns default value are not copied t o t he dest inat ion t able. To preserve t hese at t ribut es, creat e t he t able, t hen use ALTER TABLE t o apply t he appropriat e m odificat ions t o t he colum n definit ion. For exam ple, if src_tbl has an id colum n t hat is not only a PRIMARY KEY but an AUTO_INCREMENT colum n, copy t he t able, t hen m odify it : • CREATE TABLE dst_tbl PRIMARY KEY id SELECT FROM src_tbl; ALTER TABLE dst_tbl MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT; • I f you want t o m ake t he dest inat ion t able an exact copy of t he source t able, use t he cloning t echnique described in Recipe 3.26 .

3.24 Moving Records Between Tables Safely