Saturday, January 10, 2009

Copy an existing MySQL table to a new table

This is a great set of two commands that allow the creation and population of a new table with the structure and data of an existing table. This provides a quick means of making a point-in-time copy of a table and is a safe, easy way to make a quick copy of a table for testing an application in development on live data without risking a production environment.

To make a copy of the table recipes which is in a different database called production into a new table called recipes_new in the currently selected database, use these two commands:

CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new SELECT * FROM production.recipes;

The first command creates the new table recipes_new by duplicating the structure of the existing table. The second command copies the data from old to new.

The nomenclature production.recipes is a means of specifying the database and table in the same way that a file can be specified by its directory path. It is optional. If production was left off, MySQL would assume that the recipes table was also in the currently selected database.

3 comments:

Anonymous said...

Sain baina uu,
bi ooroo gadaadad baidag mongol.
User interface n mongol kheleer baikh neg program khiikh gesen yum.
Ooroos chin neg zuil asuuia aa.
Mongol usegnuudiig MySql DB-ruu yaaj khadgaldag talaar ankan shatny tovchkhon bogood shuud khereglej bolokhoor praktik medeelliig khaanaas avch bolokh talaar medleg turshlaga baival zovlooch. Yamar negen URL address, tutorial, example source code?
Baiarlalaa.

Boldbayar said...

Sain bna uu. Ene odriin mend. Unicode demjdeg programchlaliin hel ashiglan program bichih gej bgaa bol (jishee ni visual studio .NET, Delphi 2007 ch yum uu) Unicode Db uusgeed mongoloor bichij DB ruugaa insert hiihed asuudalgui. Windows-iinhoo mongolian cryllic driver (MO)-aar bicheed oruulna. Butsaagaad view hiihed ch asuudalgui. Harin Unicode demjdeggui programchlaliin hel (jishee ni Delphi 7-oos omnoh huwilbar) ashiglah bol ASCII DB uusgeed programiinhaa control-uudiinhaa font-iig Arial mon bolgood medeellee Monkey ashiglan oruulah heregtei bolno.

Anonymous said...

Sain baina uu,
minii asuultand khariulsand baiarlalaa. Bi asuultaa jaakhan todruulakh kheregtei yum shig baina.
WindowsXP deer Java5, Mysql 5.1.3 -iig khereglene.
DB bolon table uusgekhdee yuun deer ankhaarakh yostoi ve?
Ene commanduud jisheelbel: CHARACTER SET, COLLATE gekh met param-uudtai yum baina. Ted nariig yaaj tokhiruulakh yostoi ve? utf8? esvel utf8_unicode_ci esvel cp1251 gekh meteer bur ondoogoor? Esvel DB bolon TABLE-iin CHARACTER SET yamar baikh n khamaagui yum uu?
Gol n mongoloor bichsen textee (problem bolood baidag ө, ү gesen khoior useg ch gesen) bustaagaad program deeree mongoloor n kharj chaddag baikh kheregtei baina. Tegeed bas deerees n bolj ogvol shuud DB-ruugaa orood jishee n SELECT * FROM BOOKS geed select khiikhed text maani bas unshigdakhaar garch irj baival bur sain baina, gekhdee ene select deer tegj kharagdakh n buur neg ikh chukhal bish l dee.
baiarlalaa.

Chuluun.