Problem Solution Discussion Exporting Table Contents or Definitions in SQL Format

To dum p a t able as a dat afile, you m ust specify a - - t ab opt ion t hat indicat es t he direct ory where you want t he MySQL server t o writ e t he file. The direct ory m ust already exist ; t he server wont creat e it . For exam ple, t o dum p t he states t able from t he cookbook dat abase t o a file in t he t m p direct ory, use a com m and like t his: mysqldump --no-create-info --tab=tmp cookbook states m ysqldum p creat es a dat afile using t he t able nam e plus a .t xt suffix, so t his com m and will writ e a file nam ed t m p st at es.t xt . This form of m ysqldum p is in som e respect s t he com m and- line equivalent of SELECT ... INTO OUTFILE . For exam ple, it writ es out a t able as a dat afile on t he server host , and you m ust have t he FILE privilege t o use it . See Recipe 10.14 for a list of general propert ies of SELECT ... INTO OUTFILE . I f you om it t he - - no- creat e- info opt ion, m ysqldum p also will creat e a file t m p st at es.sql t hat cont ains t he CREATE TABLE st at em ent for t he t able. The lat t er file will be owned by you, unlike t he dat afile, which is owned by t he server. You can nam e m ult iple t ables aft er t he dat abase nam e, in which case m ysqldum p writ es out put files for each of t hem . I f you dont nam e any t ables, m ysqldum p writ es out put for every t able in t he dat abase. m ysqldum p creat es dat afiles in t ab-delim it ed, linefeed- t erm inat ed form at by default . To cont rol t he out put form at , use t he - - fields-enclosed-by, - - fields- t erm inat ed-by, and - - lines- t erm inat ed-by opt ions t hat is, t he sam e opt ions t hat m ysqlim port underst ands as form at specifiers . For exam ple, t o writ e t he states t able in CSV form at wit h CRLF line endings, use t his com m and: mysqldump --no-create-info --tab=tmp \ --fields-enclosed-by=\ --fields-terminated-by=, \ --lines-terminated-by=\r\n cookbook states A dat afile export ed t his way can be im port ed using LOAD DATA or m ysqlim port . Be sure t o use m at ching form at specifiers when im port ing if you didnt dum p t he t able using t he default form at .

10.16 Exporting Table Contents or Definitions in SQL Format

10.16.1 Problem

You want t o export t ables or dat abases as SQL st at em ent s t o m ake t hem easier t o im port lat er .

10.16.2 Solution

Use t he m ysqldum p program wit hout t he - - t ab opt ion.

10.16.3 Discussion

As discussed in Recipe 10.15 , m ysqldum p causes t he MySQL server t o writ e t ables as raw dat afiles on t he server host when it s invoked wit h t he - - t ab opt ion. I f you om it t he - - t ab, t he server form at s t he t able records as t he INSERT st at em ent s and ret urns t hem t o m ysqldum p. You can also generat e t he CREATE TABLE st at em ent for each t able. This provides a convenient form of out put t hat you can capt ure in a file and use lat er t o recreat e a t able or t ables. I t s com m on t o use such dum p files as backups or for copying t ables t o anot her MySQL server. This sect ion discusses how t o save dum p out put in a file; Recipe 10.17 shows how t o send it direct ly t o anot her server over t he net work. To export a t able in SQL form at t o a file, use a com m and like t his: mysqldump cookbook states dump.txt That creat es an out put file dum p.t xt t hat cont ains bot h t he CREATE TABLE st at em ent and a set of INSERT st at em ent s: MySQL dump 8.16 Host: localhost Database: cookbook -------------------------------------------------------- Server version 3.23.46-log Table structure for table states CREATE TABLE states name varchar30 NOT NULL default , abbrev char2 NOT NULL default , statehood date default NULL, pop bigint20 default NULL, PRIMARY KEY abbrev TYPE=MyISAM; Dumping data for table states INSERT INTO states VALUES Alaska,AK,1959-01-03,550043; INSERT INTO states VALUES Alabama,AL,1819-12-14,4040587; INSERT INTO states VALUES Arkansas,AR,1836-06-15,2350725; INSERT INTO states VALUES Arizona,AZ,1912-02-14,3665228; INSERT INTO states VALUES California,CA,1850-09-09,29760021; INSERT INTO states VALUES Colorado,CO,1876-08-01,3294394; ... To dum p m ult iple t ables, nam e t hem all following t he dat abase nam e argum ent . To dum p an ent ire dat abase, dont nam e any t ables aft er t he dat abase. I f you want t o dum p all t ables in all dat abases, invoke m ysqldum p like t his: mysqldump --all-databases dump.txt I n t his case, t he out put file also will include CREATE DATABASE and USE db_name st at em ent s at appropriat e places so t hat when you read in t he file lat er, each t able will be creat ed in t he proper dat abase. The - - all- dat abases opt ion is available as of MySQL 3.23.12. Ot her opt ions are available t o cont rol t he out put form at : - - no- creat e- info Suppr ess t he CREATE TABLE st at em ent s. Use t his opt ion w hen you w ant t o dum p t able cont ent s only. - - no- dat a Suppr ess t he INSERT st at em ent s. Use t his opt ion w hen you w ant t o dum p t able definit ions only. - - add- drop- t able Precede each CREATE TABLE st at em ent w it h a DROP TABLE st at em ent . This is useful for generat ing a file t hat you can use lat er t o r ecr eat e t ables fr om scr at ch. - - no- creat e-db Suppr ess t he CREATE DATABASE st at em ent s t hat t he - - all- dat abases opt ion nor m ally pr oduces. Suppose now t hat youve used m ysqldum p t o creat e a SQL- form at dum p file. How do you im port it t he file back int o MySQL? One com m on m ist ake at t his point is t o use m ysqlim port . Aft er all, it s logical t o assum e t hat if m ysqldum p export s t ables, m ysqlim port m ust im port t hem . Right ? Sorry, no. That m ight be logical, but it s not always correct . I t s t rue t hat if you use t he - - t ab opt ion wit h m ysqldum p, you can im port t he result ing dat afiles wit h m ysqlim port . But if you dum p a SQL- form at file, m ysqlim port wont process it properly. Use t he m ysql program inst ead. The way you do t his depends on what s in t he dum p file. I f you dum ped m ult iple dat abases using - - all- dat abases, t he file will cont ain t he appropriat e USE db_name st at em ent s t o select t he dat abases t o which each t able belongs, and you need no dat abase argum ent on t he com m and line: mysql dump.txt I f you dum ped t ables from a single dat abase, youll need t o t ell m ysql w hich dat abase t o im port t hem int o: mysql db_name dump.txt Not e t hat wit h t his second im port com m and, it s possible t o load t he t ables int o a different dat abase t han t he one from which t hey cam e originally. You can use t his fact , for exam ple, t o creat e copies of a t able or t ables in a t est dat abase t o use for t rying out som e dat a m anipulat ing st at em ent s t hat youre debugging, wit hout worrying about affect ing t he original t ables.

10.17 Copying Tables or Databases to Another Server