<?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>資料操縱 DML &#8211; 小人物看世界</title>
	<atom:link href="https://blog.che-ya.com/category/sql-tutorial/dml/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.che-ya.com</link>
	<description>軟體工程師的技術筆記</description>
	<lastBuildDate>Fri, 03 Apr 2026 07:13:17 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.che-ya.com/wp-content/uploads/2021/08/cropped-APP_icon-32x32.png</url>
	<title>資料操縱 DML &#8211; 小人物看世界</title>
	<link>https://blog.che-ya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>SQL SELECT INTO</title>
		<link>https://blog.che-ya.com/sql-select-into/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 20 Sep 2024 01:25:52 +0000</pubDate>
				<category><![CDATA[資料操縱 DML]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=137</guid>

					<description><![CDATA[SELECT INTO 用來從某資料表查詢所得之資料集結果新增到另一個新建的資料表中。此一指令常用來複製備份資料表，或將資料表輸出至另一資料庫中。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="select-into-敘述句-sql-select-into-statement">SELECT INTO 敘述句 (SQL SELECT INTO Statement)</h1>



<p>SELECT INTO 用來從某資料表查詢所得之資料集結果新增到另一個新建的資料表中。此一指令常用來複製備份資料表，或將資料表輸出至另一資料庫中。</p>



<h2 class="wp-block-heading" id="select-into-語法-sql-select-into-syntax">SELECT INTO 語法 (SQL SELECT INTO Syntax)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>SELECT</strong> table_column1, table_column2, table_column3...
<strong>INTO</strong> new_table_name [<strong>IN</strong> another_database]
<strong>FROM</strong> table_name;</code></pre>



<p>其中 new_table_name 為欲新建的資料表名稱，該資料表會自動建立，且不可與已經存在的資料表名稱相同；而 another_database 為至外部資料庫的路徑。</p>



<h2 class="wp-block-heading" id="select-into-敘述句用法-example">SELECT INTO 敘述句用法 (Example)</h2>



<p>例如，我們現在想備份顧客資料表 customers：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>SELECT</strong> * <strong>INTO</strong> customers_backup <strong>FROM</strong> customers;</code></pre>



<p>或者，我們想把它備份到 backup 資料庫中：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>SELECT</strong> *
<strong>INTO</strong> customers_backup <strong>IN</strong> 'backup.mdb'
<strong>FROM</strong> customers;</code></pre>



<p>我們也可以只複製資料表結構：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>SELECT</strong> * <strong>INTO</strong> new_table_name <strong>FROM</strong> table_name <strong>WHERE</strong> 0=1;</code></pre>



<p>MySQL 資料庫不支援 SELECT INTO，但你可以用&nbsp;INSERT INTO&#8230;SELECT&nbsp;來達到同樣的功能。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-insert-into/">SQL INSERT INTO</a></li>



<li><a href="https://blog.che-ya.com/create-table/">CREATE TABLE</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-select-into%2F&amp;linkname=SQL%20SELECT%20INTO" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-select-into%2F&amp;linkname=SQL%20SELECT%20INTO" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-select-into%2F&amp;linkname=SQL%20SELECT%20INTO" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-select-into%2F&#038;title=SQL%20SELECT%20INTO" data-a2a-url="https://blog.che-ya.com/sql-select-into/" data-a2a-title="SQL SELECT INTO"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL DELETE FROM</title>
		<link>https://blog.che-ya.com/sql-delete-from/</link>
					<comments>https://blog.che-ya.com/sql-delete-from/#comments</comments>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 17 Sep 2024 01:24:51 +0000</pubDate>
				<category><![CDATA[資料操縱 DML]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=134</guid>

					<description><![CDATA[DELETE FROM 敘述句 (SQL DELETE FROM Statement) DELETE FROM ... <a title="SQL DELETE FROM" class="read-more" href="https://blog.che-ya.com/sql-delete-from/" aria-label="Read more about SQL DELETE FROM">閱讀全文</a>]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="delete-from-敘述句-sql-delete-from-statement">DELETE FROM 敘述句 (SQL DELETE FROM Statement)</h1>



<p>DELETE FROM 是用來刪除資料表中的資料。</p>



<h2 class="wp-block-heading" id="delete-from-語法-sql-delete-from-syntax">DELETE FROM 語法 (SQL DELETE FROM Syntax)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DELETE</strong> <strong>FROM</strong> table_name
<strong>WHERE</strong> column_name <strong>operator</strong> <strong>value</strong>;</code></pre>



<p>WHERE 條件式記得要加哦！不然 &#8220;全部的&#8221; 資料都會刪除了。</p>



<h2 class="wp-block-heading" id="delete-from-敘述句用法-example">DELETE FROM 敘述句用法 (Example)</h2>



<p>假設我們想從下面的 customers 資料表中刪除顧客王二的資料：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新竹縣</td><td>YY路200號</td><td>03-12345678</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td>ZZ路300號</td><td>07-12345678</td></tr></tbody></table></figure>



<p>我們可以使用以下的 DELETE FROM 敘述句：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DELETE</strong> <strong>FROM</strong> customers <strong>WHERE</strong> <strong>Name</strong>='王二';</code></pre>



<p>刪除後的資料表現在變成：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td>ZZ路300號</td><td>07-12345678</td></tr></tbody></table></figure>



<h2 class="wp-block-heading" id="一次刪除資料表中所有的資料">一次刪除資料表中所有的資料</h2>



<p>我們還可以這樣一次刪除某資料表中所有的資料：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DELETE</strong> <strong>FROM</strong> table_name;</code></pre>



<p>或者：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DELETE</strong> * <strong>FROM</strong> table_name;</code></pre>



<h2 class="wp-block-heading">TRUNCATE TABLE 與 DELETE FROM 的比較</h2>



<p>除了 DELETE FROM 之外，SQL 還提供了 TRUNCATE TABLE 語句來刪除資料表中的所有資料。雖然兩者都能達到清空資料的效果，但在運作方式和使用場景上有很大的不同。</p>



<h2 class="wp-block-heading">TRUNCATE TABLE 語法 (TRUNCATE TABLE Syntax)</h2>



<pre class="wp-block-code"><code class=""><strong>TRUNCATE</strong> <strong>TABLE</strong> table_name;</code></pre>



<p>TRUNCATE TABLE 會一次移除資料表中的所有資料列，並且無法使用 WHERE 條件來篩選要刪除的資料。</p>



<h2 class="wp-block-heading">TRUNCATE TABLE 用法 (Example)</h2>



<p>假設我們想要清空 customers 資料表中的所有資料：</p>



<pre class="wp-block-code"><code class=""><strong>TRUNCATE</strong> <strong>TABLE</strong> customers;</code></pre>



<p>執行後，customers 資料表的所有資料都會被移除，但資料表本身的結構（欄位定義、索引等）會被保留。</p>



<h2 class="wp-block-heading">DELETE FROM 與 TRUNCATE TABLE 的差異</h2>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>比較項目</th><th>DELETE FROM</th><th>TRUNCATE TABLE</th></tr></thead><tbody><tr><td>刪除範圍</td><td>可搭配 WHERE 條件刪除部分資料</td><td>只能刪除全部資料</td></tr><tr><td>執行速度</td><td>較慢（逐筆刪除）</td><td>較快（直接釋放資料頁）</td></tr><tr><td>交易記錄</td><td>每筆刪除都會寫入交易記錄</td><td>只記錄頁面釋放的操作</td></tr><tr><td>ROLLBACK</td><td>支援（可復原）</td><td>部分資料庫不支援</td></tr><tr><td>觸發器 (Trigger)</td><td>會觸發 DELETE 觸發器</td><td>不會觸發觸發器</td></tr><tr><td>自動編號重置</td><td>不會重置 AUTO_INCREMENT</td><td>會重置 AUTO_INCREMENT</td></tr></tbody></table></figure>



<p>簡單來說，如果你只需要刪除符合特定條件的資料，使用 DELETE FROM；如果你想要快速清空整個資料表的資料並重置自動編號，使用 TRUNCATE TABLE 會更有效率。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-update/">SQL UPDATE</a></li>



<li><a href="https://blog.che-ya.com/sql-insert-into/">SQL INSERT INTO</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-delete-from%2F&amp;linkname=SQL%20DELETE%20FROM" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-delete-from%2F&amp;linkname=SQL%20DELETE%20FROM" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-delete-from%2F&amp;linkname=SQL%20DELETE%20FROM" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-delete-from%2F&#038;title=SQL%20DELETE%20FROM" data-a2a-url="https://blog.che-ya.com/sql-delete-from/" data-a2a-title="SQL DELETE FROM"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.che-ya.com/sql-delete-from/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL UPDATE</title>
		<link>https://blog.che-ya.com/sql-update/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 13 Sep 2024 01:23:39 +0000</pubDate>
				<category><![CDATA[資料操縱 DML]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=131</guid>

					<description><![CDATA[如果我們要修改資料表中的資料我們就會需要用到 UPDATE。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="update-敘述句-sql-update-statement">UPDATE 敘述句 (SQL UPDATE Statement)</h1>



<p>如果我們要修改資料表中的資料我們就會需要用到 UPDATE。</p>



<h2 class="wp-block-heading" id="update-語法-sql-update-syntax">UPDATE 語法 (SQL UPDATE Syntax)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>UPDATE</strong> table_name
<strong>SET</strong> column1=value1, column2=value2, column3=value3···
<strong>WHERE</strong> some_column=some_value;</code></pre>



<p>WHERE 條件式記得要加哦！這樣才會只更新特定某(幾)筆資料，不然 &#8220;全部的&#8221; 資料都會更改。</p>



<h2 class="wp-block-heading" id="update-敘述句用法-example">UPDATE 敘述句用法 (Example)</h2>



<p>假設我們想從下面的 customers 資料表中更改顧客王二的連絡電話：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新竹縣</td><td>YY路200號</td><td>03-12345678</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td>ZZ路300號</td><td>07-12345678</td></tr></tbody></table></figure>



<p>我們可以使用以下的 UPDATE 敘述句：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>UPDATE</strong> customers <strong>SET</strong> Phone='03-87654321' <strong>WHERE</strong> <strong>Name</strong>='王二';</code></pre>



<p>更新後的結果如下：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新竹縣</td><td>YY路200號</td><td>03-87654321</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td>ZZ路300號</td><td>07-12345678</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-delete-from/">SQL DELETE FROM</a></li>



<li><a href="https://blog.che-ya.com/sql-insert-into/">SQL INSERT INTO</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-update%2F&amp;linkname=SQL%20UPDATE" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-update%2F&amp;linkname=SQL%20UPDATE" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-update%2F&amp;linkname=SQL%20UPDATE" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-update%2F&#038;title=SQL%20UPDATE" data-a2a-url="https://blog.che-ya.com/sql-update/" data-a2a-title="SQL UPDATE"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL INSERT INTO</title>
		<link>https://blog.che-ya.com/sql-insert-into/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 10 Sep 2024 01:21:21 +0000</pubDate>
				<category><![CDATA[資料操縱 DML]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=127</guid>

					<description><![CDATA[INSERT INTO 是用來新增資料至某資料表 (table)。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="insert-into-敘述句-sql-insert-into-statement">INSERT INTO 敘述句 (SQL INSERT INTO Statement)</h1>



<p>INSERT INTO 是用來新增資料至某資料表 (table)。</p>



<h2 class="wp-block-heading" id="insert-into-語法-sql-insert-into-syntax">INSERT INTO 語法 (SQL INSERT INTO Syntax)</h2>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> table_name (column1, column2, column3...)
<strong>VALUES</strong> (value1, value2, value3...);
</code></pre>



<p>或，可以簡寫成這樣：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> table_name
<strong>VALUES</strong> (value1, value2, value3...);
</code></pre>



<p>使用簡寫的語法每個欄位的值都必需要依序輸入。</p>



<h2 class="wp-block-heading" id="insert-into-用法-example">INSERT INTO 用法 (Example)</h2>



<p>假設我們想從下面的 customers 資料表中新增一顧客的資料：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新竹縣</td><td>YY路200號</td><td>03-12345678</td></tr></tbody></table></figure>



<p>我們可以使用以下的 INSERT INTO 敘述句：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>INSERT</strong> <strong>INTO</strong> customers (C_Id, <strong>Name</strong>, City, Address, Phone)
<strong>VALUES</strong> (3, '李三', '高雄縣', 'ZZ路300號', '07-12345678');</code></pre>



<p>或，簡寫：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>INSERT</strong> <strong>INTO</strong> customers
<strong>VALUES</strong> (3, '李三', '高雄縣', 'ZZ路300號', '07-12345678');</code></pre>



<p>查詢新增後的結果如下：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新竹縣</td><td>YY路200號</td><td>03-12345678</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td>ZZ路300號</td><td>07-12345678</td></tr></tbody></table></figure>



<h3 class="wp-block-heading" id="只輸入幾個特定的欄位值">只輸入幾個特定的欄位值</h3>



<p>你也可以只輸入幾個特定的欄位值：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>INSERT</strong> <strong>INTO</strong> customers (C_Id, <strong>Name</strong>, City)
<strong>VALUES</strong> (3, '李三', '高雄縣');</code></pre>



<p>查詢新增後的結果如下：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新竹縣</td><td>YY路200號</td><td>03-12345678</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td></td><td></td></tr></tbody></table></figure>



<h2 class="wp-block-heading" id="一次新增多筆資料">一次新增多筆資料</h2>



<p>在實務中，我們經常需要一次新增多筆資料，而不是逐筆執行 INSERT INTO。SQL 提供了在一個敘述句中同時插入多筆資料的語法：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> table_name (column1, column2, column3,...)
<strong>VALUES</strong>
  (value1_1, value1_2, value1_3,...),
  (value2_1, value2_2, value2_3,...),
  (value3_1, value3_2, value3_3,...);</code></pre>



<p>以 customers 資料表為例，一次新增兩位顧客：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> customers (C_Id, <strong>Name</strong>, City, Address, Phone)
<strong>VALUES</strong>
  (3, '李三', '高雄縣', 'ZZ路300號', '07-12345678'),
  (4, '趙四', '台中市', 'YY路400號', '04-87654321');</code></pre>



<p>查詢新增後的結果如下：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr></thead><tbody><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新北市</td><td>YY路200號</td><td>02-87654321</td></tr><tr><td>3</td><td>李三</td><td>高雄縣</td><td>ZZ路300號</td><td>07-12345678</td></tr><tr><td>4</td><td>趙四</td><td>台中市</td><td>YY路400號</td><td>04-87654321</td></tr></tbody></table></figure>



<h2 class="wp-block-heading" id="insert-into-select-用法">INSERT INTO SELECT 用法</h2>



<p>INSERT INTO SELECT 可以從一個資料表中查詢資料，並將結果直接插入到另一個資料表中。這在資料搬移、備份或合併時非常實用。</p>



<h3 class="wp-block-heading" id="insert-into-select-語法">INSERT INTO SELECT 語法</h3>



<p>將來源資料表的所有欄位插入目標資料表：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> target_table
<strong>SELECT</strong> * <strong>FROM</strong> source_table
<strong>WHERE</strong> condition;</code></pre>



<p>或指定特定欄位：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> target_table (column1, column2, column3,...)
<strong>SELECT</strong> column1, column2, column3,...
<strong>FROM</strong> source_table
<strong>WHERE</strong> condition;</code></pre>



<h3 class="wp-block-heading" id="insert-into-select-範例">INSERT INTO SELECT 範例</h3>



<p>假設我們有一個 new_customers 資料表，裡面存放了新客戶的資料：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>C_Id</th><th>Name</th><th>City</th></tr></thead><tbody><tr><td>5</td><td>周五</td><td>台南市</td></tr><tr><td>6</td><td>吳六</td><td>桃園市</td></tr><tr><td>7</td><td>鄭七</td><td>新竹市</td></tr></tbody></table></figure>



<p>我們想把 new_customers 中位於「台南市」的客戶資料插入到 customers 資料表：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> customers (C_Id, <strong>Name</strong>, City)
<strong>SELECT</strong> C_Id, <strong>Name</strong>, City
<strong>FROM</strong> new_customers
<strong>WHERE</strong> City = '台南市';</code></pre>



<p>執行後，customers 資料表將會多出一筆資料：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>C_Id</th><th>Name</th><th>City</th><th>Address</th><th>Phone</th></tr></thead><tbody><tr><td>1</td><td>張一</td><td>台北市</td><td>XX路100號</td><td>02-12345678</td></tr><tr><td>2</td><td>王二</td><td>新北市</td><td>YY路200號</td><td>02-87654321</td></tr><tr><td>5</td><td>周五</td><td>台南市</td><td>NULL</td><td>NULL</td></tr></tbody></table></figure>



<p>注意：由於 new_customers 資料表中沒有 Address 和 Phone 欄位，所以插入後這兩個欄位的值為 NULL。如果欄位設定了 <a href="https://blog.che-ya.com/sql-not-null/">NOT NULL</a> 約束條件，則需要確保所有欄位都有對應的值。</p>



<h3 class="wp-block-heading" id="搭配聚合函數使用-insert-into-select">搭配聚合函數使用 INSERT INTO SELECT</h3>



<p>INSERT INTO SELECT 也可以搭配<a href="https://blog.che-ya.com/sql-aggregate-functions/">聚合函數</a>和 <a href="https://blog.che-ya.com/sql-aggregate-functions/#搭配-group-by-進行分組統計">GROUP BY</a> 使用，將統計結果直接插入另一個資料表。例如，將各城市的客戶數量統計結果存入 city_stats 資料表：</p>



<pre class="wp-block-code"><code class=""><strong>INSERT</strong> <strong>INTO</strong> city_stats (City, customer_count)
<strong>SELECT</strong> City, <strong>COUNT</strong>(*) <strong>AS</strong> customer_count
<strong>FROM</strong> customers
<strong>GROUP BY</strong> City;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>City</th><th>customer_count</th></tr></thead><tbody><tr><td>台北市</td><td>1</td></tr><tr><td>新北市</td><td>1</td></tr><tr><td>台南市</td><td>1</td></tr></tbody></table></figure>



<p>使用 INSERT INTO SELECT 時，請確保目標資料表已經存在，且欄位的資料型別與來源查詢結果相符。如果需要同時建立新資料表並複製資料，可以參考 <a href="https://blog.che-ya.com/sql-select-into/">SELECT INTO</a> 語法。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學 — 完整 SQL 語法教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT — 查詢資料</a></li>



<li><a href="https://blog.che-ya.com/sql-update/">SQL UPDATE — 更新資料</a></li>



<li><a href="https://blog.che-ya.com/sql-delete-from/">SQL DELETE FROM — 刪除資料</a></li>



<li><a href="https://blog.che-ya.com/create-table/">CREATE TABLE — 建立資料表</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE — 條件篩選</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery — 子查詢</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-insert-into%2F&amp;linkname=SQL%20INSERT%20INTO" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-insert-into%2F&amp;linkname=SQL%20INSERT%20INTO" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-insert-into%2F&amp;linkname=SQL%20INSERT%20INTO" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-insert-into%2F&#038;title=SQL%20INSERT%20INTO" data-a2a-url="https://blog.che-ya.com/sql-insert-into/" data-a2a-title="SQL INSERT INTO"></a></p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
