<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; tips</title>
	<atom:link href="http://www.codediesel.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Copying tables in MySQL</title>
		<link>http://www.codediesel.com/mysql/copying-tables-in-mysql/</link>
		<comments>http://www.codediesel.com/mysql/copying-tables-in-mysql/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:16:13 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1368</guid>
		<description><![CDATA[easily copy tables in mysql]]></description>
			<content:encoded><![CDATA[<p>Whether you need to copy a test table to a production database, or you need to duplicate a table with only some selected rows or you just need to backup the original table, copying tables is a frequent task most of us regularly do. In this post we will see some quick methods to make copies of MySQL database tables.<br />
<span id="more-1368"></span><br />
For these examples I&#8217;ve used the following table structure.</p>

<div class="wp_codebox"><table><tr id="p13681"><td class="code" id="p1368code1"><pre class="sql" style="font-family:monospace;">id      username    password
<span style="color: #808080; font-style: italic;">-----------------------------------</span>
<span style="color: #cc66cc;">1</span>       admin       <span style="color: #66cc66;">*************</span>
<span style="color: #cc66cc;">2</span>       sameer      <span style="color: #66cc66;">*************</span>
<span style="color: #cc66cc;">3</span>       stewart     <span style="color: #66cc66;">*************</span>
&nbsp;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`admin`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">auto_increment</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">default</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`password`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">default</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1 <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span> ;</pre></td></tr></table></div>

<h4>Quickly copying tables.</h4>
<p>The most common task is to copy the table structures. The following statement copies the structure of the &#8216;admin&#8217; table to a new table called &#8216;newadmin&#8217;.</p>

<div class="wp_codebox"><table><tr id="p13682"><td class="code" id="p1368code2"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> admin</pre></td></tr></table></div>

<p>Note that only the structure and not the data is copied to the new table. To also copy the data use the following statement.</p>

<div class="wp_codebox"><table><tr id="p13683"><td class="code" id="p1368code3"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span>
    <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>In the above examples it is assumed that the source and the destination tables which we want to copy are in the same database, but you can also copy tables from another database by prefixing the database name before the table. The following for example will copy the structure of the &#8216;admin&#8217; table from the &#8216;shop&#8217; database to the &#8216;newadmin&#8217; table in the current database.</p>

<div class="wp_codebox"><table><tr id="p13684"><td class="code" id="p1368code4"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> shop<span style="color: #66cc66;">.</span>admin</pre></td></tr></table></div>

<p>You can also specify the destination database in the query.</p>

<div class="wp_codebox"><table><tr id="p13685"><td class="code" id="p1368code5"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newshop<span style="color: #66cc66;">.</span>newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> shop<span style="color: #66cc66;">.</span>admin</pre></td></tr></table></div>

<p>This will copy the &#8216;admin&#8217; table from the &#8216;shop&#8217; database to the new table &#8216;newadmin&#8217; in the &#8216;newshop&#8217; database.</p>
<p><strong>Note</strong>: In all the above examples the DATA DIRECTORY or INDEX DIRECTORY table options are <em>not copied</em> to the new table. So if you need an exact replica of a table you have to create two separate statements.</p>

<div class="wp_codebox"><table><tr id="p13686"><td class="code" id="p1368code6"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> admin;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> newadmin <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin;</pre></td></tr></table></div>

<p>This will copy the table &#8216;admin&#8217; <em>exactly</em> to the &#8216;newadmin&#8217; table with all the correct table definitions, but we lose the flexibility we can have with the other method, which we will be seeing next.</p>
<p>To copy some select columns to our new table, specify the column names in a SELECT statement.</p>

<div class="wp_codebox"><table><tr id="p13687"><td class="code" id="p1368code7"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> username<span style="color: #66cc66;">,</span> password <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This will only copy the &#8216;username&#8217; and &#8216;password&#8217; columns to the new table. This can be really useful when the original table contains over fifty columns and you want to copy only a few.</p>
<p>While copying a table you can also change the column names of the new table.</p>

<div class="wp_codebox"><table><tr id="p13688"><td class="code" id="p1368code8"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> username <span style="color: #993333; font-weight: bold;">AS</span> uname<span style="color: #66cc66;">,</span> password <span style="color: #993333; font-weight: bold;">AS</span> pass <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>By now you may have already guessed that you could copy any queried data to the new table using the SELECT statement. The following for example copies all the rows from &#8216;admin&#8217; to the new table where the username starts with an &#8216;s&#8217;.</p>

<div class="wp_codebox"><table><tr id="p13689"><td class="code" id="p1368code9"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'s'</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Copies the following two rows from the &#8216;admin&#8217; table.</p>

<div class="wp_codebox"><table><tr id="p136810"><td class="code" id="p1368code10"><pre class="sql" style="font-family:monospace;">id      username    password
<span style="color: #808080; font-style: italic;">-----------------------------------</span>
<span style="color: #cc66cc;">2</span>       sameer      <span style="color: #66cc66;">*************</span>
<span style="color: #cc66cc;">3</span>       stewart     <span style="color: #66cc66;">*************</span></pre></td></tr></table></div>

<p>While copying you can also change the table structure of the new table by specifying the same in your query.</p>

<div class="wp_codebox"><table><tr id="p136811"><td class="code" id="p1368code11"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin
<span style="color: #66cc66;">&#40;</span>
    id INTEGER <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>
<span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This will copy the &#8216;admin&#8217; table and also assign the appropriate primary key to the &#8216;id&#8217; column in the new table. </p>
<h4>In conclusion</h4>
<p>So basically there are two methods of copying tables.<br />
a. By using the LIKE and AS keywords in single sql statement.</p>

<div class="wp_codebox"><table><tr id="p136812"><td class="code" id="p1368code12"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span>
    <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>b. By using two different statements.</p>

<div class="wp_codebox"><table><tr id="p136813"><td class="code" id="p1368code13"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> admin;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> newadmin <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin;</pre></td></tr></table></div>

<p>The first one offers flexibility while copying tables but does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions. </p>
<p>The second method truthfully copies the tables but does not offer the flexibility of the first method. By truthfully I mean only the DATA DIRECTORY or INDEX DIRECTORY table options are not copied.</p>
<p>Which method you use will depend on your specific requirement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/copying-tables-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

