<?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>資料定義 DDL &#8211; 小人物看世界</title>
	<atom:link href="https://blog.che-ya.com/category/sql-tutorial/ddl/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.che-ya.com</link>
	<description>軟體工程師的技術筆記</description>
	<lastBuildDate>Fri, 03 Apr 2026 02:17:21 +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>資料定義 DDL &#8211; 小人物看世界</title>
	<link>https://blog.che-ya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>SQL VIEW</title>
		<link>https://blog.che-ya.com/sql-view/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 06 Sep 2024 05:35:25 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=123</guid>

					<description><![CDATA[View 是藉由 SQL SELECT 查詢動態組合生成的資料表 (亦即 View 是由查詢得到的結果集組合而成的資料表)。View 內的資料紀錄是由其它實際存在的資料表中產生的]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="view-檢視表視圖-sql-view">View 檢視表、視圖 (SQL View)</h1>



<p>View 是藉由 SQL SELECT 查詢動態組合生成的資料表 (亦即 View 是由查詢得到的結果集組合而成的資料表)。View 內的資料紀錄是由其它實際存在的資料表中產生的，它就像是一個虛擬資料表，實際上資料庫 (或說是硬碟) 裡面是不存在這一個資料表的 (只存在此 View 的相關定義)，但是我們使用上卻有如實際存在的資料表 &#8211; 所有的 SQL 查詢語法都可以操作在此 View 上。</p>



<p>資料表是一種實體結構 (physical structure)，而 View 是一種虛擬結構 (virtual structure)。</p>



<h2 class="wp-block-heading" id="view-有哪些特性">View 有哪些特性</h2>



<ol class="wp-block-list">
<li>加強資料庫的安全性，View 可以將實體資料表結構隱藏起來，同時限制使用者只可以檢視及使用哪些資料表欄位。</li>



<li>檢視表是唯讀的，亦即外部使用者無法直接透過 View 去修改內部資料。</li>



<li>將複雜的 SQL 查詢包裝在 View 中，可以簡化查詢的複雜度。</li>



<li>當資料表結構有變更時，只需要更改 View 的設定，不需更改程式。</li>
</ol>



<h2 class="wp-block-heading" id="建立-view-sql-create-view">建立 View (SQL CREATE VIEW)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>VIEW</strong> view_name [(column_list)] <strong>AS</strong>
<strong>SELECT</strong> column_name(s)
<strong>FROM</strong> table_name
<strong>WHERE</strong> condition;</code></pre>



<p>例如：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>VIEW</strong> v <strong>AS</strong> <strong>SELECT</strong> qty, price, qty*price <strong>AS</strong> <strong>value</strong> <strong>FROM</strong> t;</code></pre>



<p>一個 View 可以由查詢實體資料表而建立，亦可以查詢其它已存在的 View 而建立。</p>



<h2 class="wp-block-heading" id="更新-view-sql-create-or-replace-view">更新 View (SQL CREATE OR REPLACE VIEW)</h2>



<p>如果加上&nbsp;<code>OR REPLACE</code>&nbsp;子句的意思就是若同名的 View 已經存在就覆蓋取代它。如果 View 不存在，我們可以把 CREATE OR REPLACE VIEW 看做是如同 CREATE VIEW；而如果 View 已存在，我們可以把 CREATE OR REPLACE VIEW 看做是 ALTER VIEW。</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>OR</strong> <strong>REPLACE</strong> <strong>VIEW</strong> view_name [(column_list)] <strong>AS</strong>
<strong>SELECT</strong> column_name(s)
<strong>FROM</strong> table_name
<strong>WHERE</strong> condition;</code></pre>



<h2 class="wp-block-heading" id="刪除-view-sql-drop-view">刪除 View (SQL DROP VIEW)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DROP</strong> <strong>VIEW</strong> view_name;</code></pre>



<h2 class="wp-block-heading" id="view-使用實例-example">View 使用實例 (Example)</h2>



<p>假設這是一個產品訂單資料表 p_orders：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>Product</th><th>Price</th><th>Quantity</th></tr><tr><td>LCD</td><td>4000</td><td>100</td></tr><tr><td>CPU</td><td>5000</td><td>200</td></tr></tbody></table></figure>



<p>我們可以建立一個方便查詢各產品售出總額的 View：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>VIEW</strong> view_p_sum (Product, P_SUM) <strong>AS</strong>
<strong>SELECT</strong> Product, Price*Quantity <strong>FROM</strong> p_orders <strong>GROUP</strong> <strong>BY</strong> Product;</code></pre>



<p>接著，你就可以像操作一般資料表：</p>



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



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>Product</th><th>P_SUM</th></tr><tr><td>CPU</td><td>1000000</td></tr><tr><td>LCD</td><td>400000</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">物化視圖 (Materialized View)</h2>



<p>除了一般的 View 之外，某些資料庫還支援<strong>物化視圖 (Materialized View)</strong>。與一般 View 不同的是，Materialized View 會將查詢結果實際儲存在磁碟中，因此在讀取時不需要重新執行查詢，效能上會比一般 View 更好。</p>



<p>目前支援 Materialized View 的資料庫包括 PostgreSQL、Oracle、SQL Server（以 Indexed View 的形式支援）等。MySQL 原生不支援 Materialized View，但可以透過資料表搭配觸發器 (Trigger) 來模擬。</p>



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



<p>以 PostgreSQL 為例，建立 Materialized View 的語法如下：</p>



<pre class="wp-block-code"><code class=""><strong>CREATE</strong> <strong>MATERIALIZED</strong> <strong>VIEW</strong> view_name <strong>AS</strong>
<strong>SELECT</strong> column_name(s)
<strong>FROM</strong> table_name
<strong>WHERE</strong> condition;</code></pre>



<h2 class="wp-block-heading">View 與 Materialized View 的差異</h2>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>特性</th><th>View</th><th>Materialized View</th></tr></thead><tbody><tr>資料儲存不儲存，每次查詢即時運算儲存在磁碟中</tr><tr>查詢效能較慢（需即時執行查詢）較快（直接讀取已存資料）</tr><tr>資料即時性永遠是最新資料需手動或排程刷新</tr><tr>佔用空間不佔用額外空間佔用額外儲存空間</tr><tr>適用場景資料量小、需即時資料資料量大、查詢頻繁、可容許些許延遲</tr></tbody></table></figure>



<h2 class="wp-block-heading">刷新 Materialized View (Refresh)</h2>



<p>由於 Materialized View 的資料是靜態儲存的，當來源資料表的資料發生變動時，Materialized View 的內容不會自動更新。我們需要手動刷新它：</p>



<pre class="wp-block-code"><code class=""><strong>REFRESH</strong> <strong>MATERIALIZED</strong> <strong>VIEW</strong> view_name;</code></pre>



<p>如果不想在刷新時鎖定整個 Materialized View（即允許查詢同時進行），可以使用 <code>CONCURRENTLY</code> 選項（需要在 Materialized View 上建立唯一索引）：</p>



<pre class="wp-block-code"><code class=""><strong>REFRESH</strong> <strong>MATERIALIZED</strong> <strong>VIEW</strong> <strong>CONCURRENTLY</strong> view_name;</code></pre>



<h2 class="wp-block-heading">Materialized View 使用實例 (Example)</h2>



<p>延續上面的 p_orders 資料表範例，我們可以建立一個 Materialized View 來儲存各產品售出總額（以 PostgreSQL 為例）：</p>



<pre class="wp-block-code"><code class=""><strong>CREATE</strong> <strong>MATERIALIZED</strong> <strong>VIEW</strong> mv_p_sum <strong>AS</strong>
<strong>SELECT</strong> Product, <strong>SUM</strong>(Price*Quantity) <strong>AS</strong> Total
<strong>FROM</strong> p_orders
<strong>GROUP</strong> <strong>BY</strong> Product;</code></pre>



<p>建立完成後，可以像一般資料表一樣查詢：</p>



<pre class="wp-block-code"><code class=""><strong>SELECT</strong> * <strong>FROM</strong> mv_p_sum;</code></pre>



<p>當 p_orders 資料有新增或異動時，需要執行刷新指令來更新 Materialized View 中的資料：</p>



<pre class="wp-block-code"><code class=""><strong>REFRESH</strong> <strong>MATERIALIZED</strong> <strong>VIEW</strong> mv_p_sum;</code></pre>



<p>如果不再需要此 Materialized View，可以使用以下語法刪除：</p>



<pre class="wp-block-code"><code class=""><strong>DROP</strong> <strong>MATERIALIZED</strong> <strong>VIEW</strong> mv_p_sum;</code></pre>



<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-join/">SQL JOIN — 多表連結查詢</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery — 子查詢</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-aggregate-functions/">SQL 聚合函數</a></li>



<li><a href="https://blog.che-ya.com/sql-index/">SQL INDEX — 索引教學</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-view%2F&amp;linkname=SQL%20VIEW" 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-view%2F&amp;linkname=SQL%20VIEW" 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-view%2F&amp;linkname=SQL%20VIEW" 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-view%2F&#038;title=SQL%20VIEW" data-a2a-url="https://blog.che-ya.com/sql-view/" data-a2a-title="SQL VIEW"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL INDEX</title>
		<link>https://blog.che-ya.com/sql-index/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 03 Sep 2024 05:32:46 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=121</guid>

					<description><![CDATA[如同如果一本書有索引 (index) 您可以更快的找到需要的資料，資料庫也是同樣道理，如果一張資料表中沒有索引，在查詢資料時就必需先把整張資料表讀過一遍 (scan table) 再慢慢去找資料，非常的沒有效率，因此替資料表加上 "適當的" 索引會大大加快查詢速度哦。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="create-index-敘述句-sql-create-index-statement">CREATE INDEX 敘述句 (SQL CREATE INDEX Statement)</h1>



<p>如同如果一本書有索引 (index) 您可以更快的找到需要的資料，資料庫也是同樣道理，如果一張資料表中沒有索引，在查詢資料時就必需先把整張資料表讀過一遍 (scan table) 再慢慢去找資料，非常的沒有效率，因此替資料表加上 &#8220;適當的&#8221; 索引會大大加快查詢速度哦。</p>



<p>為什麼說適當的？因為一個設有索引的資料表當你在 INSERT、UPDATE 或 DELETE 時，索引也需要額外花資源去更新，所以最好只在常被查詢到的欄位作索引，不然得不償失啊！</p>



<h2 class="wp-block-heading" id="create-index-語法-sql-create-index-syntax">CREATE INDEX 語法 (SQL CREATE INDEX Syntax)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>INDEX</strong> index_name <strong>ON</strong> table_name (column_name);</code></pre>



<h2 class="wp-block-heading" id="建立多欄位索引-multiple-column-index">建立多欄位索引 (Multiple-Column Index)</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>INDEX</strong> index_name <strong>ON</strong> table_name (column_name1, column_name2...);</code></pre>



<p>什麼時候建立多欄位索引比較合適？如果您常對一張資料表查詢&nbsp;<code>WHERE column_name1='xxx' AND column_name2='yyy'</code>，這時你就可以對 column_name1 及 column_name2 這兩個欄位建立一個共同索引。</p>



<h1 class="wp-block-heading" id="drop-index-敘述句-sql-drop-index-statement">DROP INDEX 敘述句 (SQL DROP INDEX Statement)</h1>



<p>我們要怎麼將索引移除掉？就是使用 DROP INDEX。</p>



<p>MySQL</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> table_name <strong>DROP</strong> <strong>INDEX</strong> index_name;</code></pre>



<p>SQL Server</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DROP</strong> <strong>INDEX</strong> table_name.index_name;</code></pre>



<p>Oracle</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DROP</strong> <strong>INDEX</strong> index_name;</code></pre>



<p>MS Access</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>DROP</strong> <strong>INDEX</strong> index_name <strong>ON</strong> table_name;</code></pre>



<h2 class="wp-block-heading">使用 EXPLAIN 查看執行計畫 (Execution Plan)</h2>



<p>建立索引後，要怎麼確認索引是否真的有被使用到呢？這時候就可以使用 <code>EXPLAIN</code> 來查看 SQL 查詢的執行計畫。執行計畫會告訴你資料庫是如何執行你的查詢，包括是否使用了索引、掃描了多少列資料等資訊，是效能優化時非常重要的工具。</p>



<h3 class="wp-block-heading">EXPLAIN 語法</h3>



<p>在 MySQL 中，只需在 SELECT 語句前面加上 <code>EXPLAIN</code> 即可：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql"><strong>EXPLAIN</strong> <strong>SELECT</strong> * <strong>FROM</strong> table_name
<strong>WHERE</strong> column_name = value;</code></pre>



<h3 class="wp-block-heading">EXPLAIN 範例</h3>



<p>假設我們有一張 <code>employees</code> 資料表，我們想查詢特定部門的員工。先來看看在沒有索引的情況下，執行計畫長什麼樣子：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql"><strong>EXPLAIN</strong> <strong>SELECT</strong> * <strong>FROM</strong> employees
<strong>WHERE</strong> Department = '資訊部';</code></pre>



<p>如果 <code>Department</code> 欄位沒有索引，執行計畫中的 <code>type</code> 會顯示為 <code>ALL</code>，代表進行了全表掃描（Full Table Scan），效率較差。</p>



<figure class="wp-block-table"><table><thead><tr><th>id</th><th>select_type</th><th>table</th><th>type</th><th>possible_keys</th><th>key</th><th>rows</th></tr></thead><tbody><tr><td>1</td><td>SIMPLE</td><td>employees</td><td>ALL</td><td>NULL</td><td>NULL</td><td>5</td></tr></tbody></table></figure>



<p>接著我們為 <code>Department</code> 欄位建立索引：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql"><strong>CREATE</strong> <strong>INDEX</strong> idx_department <strong>ON</strong> employees (Department);</code></pre>



<p>再次執行 EXPLAIN 查看：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql"><strong>EXPLAIN</strong> <strong>SELECT</strong> * <strong>FROM</strong> employees
<strong>WHERE</strong> Department = '資訊部';</code></pre>



<p>建立索引後，<code>type</code> 變成了 <code>ref</code>，<code>key</code> 欄位也顯示使用了 <code>idx_department</code> 索引，掃描的列數也大幅減少，查詢效率明顯提升。</p>



<figure class="wp-block-table"><table><thead><tr><th>id</th><th>select_type</th><th>table</th><th>type</th><th>possible_keys</th><th>key</th><th>rows</th></tr></thead><tbody><tr><td>1</td><td>SIMPLE</td><td>employees</td><td>ref</td><td>idx_department</td><td>idx_department</td><td>2</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">EXPLAIN 常見欄位說明</h3>



<figure class="wp-block-table"><table><thead><tr><th>欄位</th><th>說明</th></tr></thead><tbody><tr><td>id</td><td>查詢的序號</td></tr><tr><td>select_type</td><td>查詢類型，如 SIMPLE（簡單查詢）、SUBQUERY（子查詢）等</td></tr><tr><td>table</td><td>查詢的資料表</td></tr><tr><td>type</td><td>存取類型，效能由好到差為：system &gt; const &gt; eq_ref &gt; ref &gt; range &gt; index &gt; ALL</td></tr><tr><td>possible_keys</td><td>可能使用的索引</td></tr><tr><td>key</td><td>實際使用的索引</td></tr><tr><td>rows</td><td>預估需要掃描的列數，數字越小越好</td></tr></tbody></table></figure>



<p>透過 <code>EXPLAIN</code> 可以幫助我們判斷查詢是否有效地利用了索引，當 <code>type</code> 顯示為 <code>ALL</code> 時，通常代表需要考慮加上適當的索引來優化查詢效能。</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/create-table/">CREATE TABLE — 建立資料表</a></li>



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵</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-view/">SQL VIEW — 檢視表教學</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-index%2F&amp;linkname=SQL%20INDEX" 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-index%2F&amp;linkname=SQL%20INDEX" 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-index%2F&amp;linkname=SQL%20INDEX" 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-index%2F&#038;title=SQL%20INDEX" data-a2a-url="https://blog.che-ya.com/sql-index/" data-a2a-title="SQL INDEX"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL AUTO INCREMENT</title>
		<link>https://blog.che-ya.com/sql-auto-increment/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 30 Aug 2024 05:26:40 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=119</guid>

					<description><![CDATA[AUTO INCREMENT 欄位會自動遞增資料行的值，因為每次新增資料時欄位值都會自動遞增也就是說 AUTO INCREMENT 欄位值會是唯一的，該欄位用途就像是一個識別碼或流水號，而 AUTO INCREMENT 常與 Primary Key 一起搭配使用。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="auto-increment-欄位-sql-auto-increment-column">AUTO INCREMENT 欄位 (SQL AUTO INCREMENT column)</h1>



<p>AUTO INCREMENT 欄位會自動遞增資料行的值，因為每次新增資料時欄位值都會自動遞增也就是說 AUTO INCREMENT 欄位值會是唯一的，該欄位用途就像是一個識別碼或流水號，而 AUTO INCREMENT 常與 Primary Key 一起搭配使用。</p>



<h2 class="wp-block-heading" id="設定-auto-increment-欄位">設定 AUTO INCREMENT 欄位</h2>



<h3 class="wp-block-heading" id="mysql">MySQL</h3>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customers (
  C_Id INT AUTO_INCREMENT,
  <strong>Name</strong> varchar(50),
  Address varchar(255),
  Phone varchar(20),
  PRIMARY <strong>KEY</strong> (C_Id)
);</code></pre>



<p>MySQL 語法使用 AUTO_INCREMENT 這關鍵字。注意要將 AUTO_INCREMENT 欄位指定為 PRIMARY KEY，否則會有錯誤！</p>



<p>新增一筆資料：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>INSERT</strong> <strong>INTO</strong> customers (<strong>Name</strong>, Address, Phone)
<strong>VALUES</strong> ('姓名XXX', '地址XXX', '電話XXX');</code></pre>



<p>C_Id 欄位不需要指定值，MySQL 預設會由1開始逐列自動遞增 (2, 3, 4&#8230;)。不過你也可以替 AUTO_INCREMENT 欄位指定一個起始值，語法如下：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> table_name AUTO_INCREMENT=起始數字;</code></pre>



<h3 class="wp-block-heading" id="sql-server">SQL Server</h3>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customers (
  C_Id INT <strong>IDENTITY</strong> PRIMARY <strong>KEY</strong>,
  <strong>Name</strong> varchar(50),
  Address varchar(255),
  Phone varchar(20)
);</code></pre>



<p>SQL Server 語法使用 IDENTITY 這關鍵字。</p>



<p>新增一筆資料：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>INSERT</strong> <strong>INTO</strong> customers (<strong>Name</strong>, Address, Phone)
<strong>VALUES</strong> ('姓名XXX', '地址XXX', '電話XXX');</code></pre>



<p>SQL Server 預設會由1開始逐列自動遞增 (2, 3, 4&#8230;)。不過你也可以替 IDENTITY 欄位指定一個起始值與遞增量，語法如下：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers">將 IDENTITY 改成 IDENTITY(起始值, 遞增量)</code></pre>



<h3 class="wp-block-heading" id="oracle">Oracle</h3>



<p>Oracle 比較不一樣，我們需要先建立一個 Sequence，然後遞增值則從這個 Sequence 抓。</p>



<p>Sequence 建立語法：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>SEQUENCE</strong> sequence_name
<strong>START</strong> <strong>WITH</strong> 1     <em>--設定起始值</em>
<strong>INCREMENT</strong> <strong>BY</strong> 1;  <em>--設定遞增量</em></code></pre>



<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>, Address, Phone)
<strong>VALUES</strong> (sequence_name.NEXTVAL ,'姓名XXX', '地址XXX', '電話XXX');</code></pre>



<p>NEXTVAL 用來取得下一個值。</p>



<h3 class="wp-block-heading" id="ms-access">MS Access</h3>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customers (
  C_Id INT PRIMARY <strong>KEY</strong> AUTOINCREMENT,
  <strong>Name</strong> varchar(50),
  Address varchar(255),
  Phone varchar(20)
);</code></pre>



<p>Access 語法使用 AUTOINCREMENT 這關鍵字。</p>



<p>新增一筆資料：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>INSERT</strong> <strong>INTO</strong> customers (<strong>Name</strong>, Address, Phone)
<strong>VALUES</strong> ('姓名XXX', '地址XXX', '電話XXX');</code></pre>



<p>Access 預設會由 1 開始逐列自動遞增 (2, 3, 4&#8230;)。不過你也可以替 AUTOINCREMENT 欄位指定一個起始值與遞增量，語法如下：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers">將 AUTOINCREMENT 改成 AUTOINCREMENT(起始值, 遞增量)</code></pre>



<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/create-table/">CREATE TABLE — 建立資料表</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵</a></li>



<li><a href="https://blog.che-ya.com/sql-foreign-key/">SQL FOREIGN KEY — 外鍵</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/alter-table/">ALTER TABLE — 修改資料表結構</a></li>



<li><a href="https://blog.che-ya.com/sql-index/">SQL INDEX — 索引教學</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-auto-increment%2F&amp;linkname=SQL%20AUTO%20INCREMENT" 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-auto-increment%2F&amp;linkname=SQL%20AUTO%20INCREMENT" 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-auto-increment%2F&amp;linkname=SQL%20AUTO%20INCREMENT" 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-auto-increment%2F&#038;title=SQL%20AUTO%20INCREMENT" data-a2a-url="https://blog.che-ya.com/sql-auto-increment/" data-a2a-title="SQL AUTO INCREMENT"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL DEFAULT</title>
		<link>https://blog.che-ya.com/sql-default/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 27 Aug 2024 05:25:36 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=117</guid>

					<description><![CDATA[DEFAULT 限制用來設定欄位的預設值。當你在 INSERT 資料時若該欄位沒指定值則會採用預設值。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="default-預設值限制-sql-default-constraint">DEFAULT 預設值限制 (SQL DEFAULT Constraint)</h1>



<p>DEFAULT 限制用來設定欄位的預設值。當你在 INSERT 資料時若該欄位沒指定值則會採用預設值。</p>



<p>假設我們要設定 customer 資料表中的 Address 欄位預設值為 &#8220;未知&#8221;：</p>



<h2 class="wp-block-heading" id="在建立資料表同時-create-table">在建立資料表同時 CREATE TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255) <strong>DEFAULT</strong> '未知',
  Phone VARCHAR(20)
);</code></pre>



<h2 class="wp-block-heading" id="更改資料表限制-alter-table">更改資料表限制 ALTER TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>ALTER</strong> <strong>COLUMN</strong> Address <strong>SET</strong> <strong>DEFAULT</strong> '未知';</code></pre>



<p>SQL Server</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>ADD</strong> <strong>DEFAULT</strong> '未知' <strong>FOR</strong> Address;</code></pre>



<h2 class="wp-block-heading" id="移除資料表限制-alter-table">移除資料表限制 ALTER TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>ALTER</strong> <strong>COLUMN</strong> Address <strong>DROP</strong> <strong>DEFAULT</strong>;</code></pre>



<p>SQL Server</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> table_name <strong>DROP</strong> constrain_name;</code></pre>



<p>Oracle</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> table_name <strong>MODIFY</strong> column_name <strong>DEFAULT</strong> NULL;</code></pre>



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



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-constraints/">SQL Constraints — SQL 約束條件總覽</a></li>



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



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</a></li>



<li><a href="https://blog.che-ya.com/sql-not-null/">SQL NOT NULL — 非空值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-check/">SQL CHECK — 檢查約束</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵約束</a></li>



<li><a href="https://blog.che-ya.com/sql-foreign-key/">SQL FOREIGN KEY — 外鍵約束</a></li>



<li><a href="https://blog.che-ya.com/sql-insert-into/">SQL INSERT INTO — 新增資料</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-default%2F&amp;linkname=SQL%20DEFAULT" 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-default%2F&amp;linkname=SQL%20DEFAULT" 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-default%2F&amp;linkname=SQL%20DEFAULT" 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-default%2F&#038;title=SQL%20DEFAULT" data-a2a-url="https://blog.che-ya.com/sql-default/" data-a2a-title="SQL DEFAULT"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL CHECK</title>
		<link>https://blog.che-ya.com/sql-check/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 23 Aug 2024 05:23:48 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=115</guid>

					<description><![CDATA[CHECK 限制用來約束欄位中的可用值，以保證該欄位中的資料值都會符合您設定的條件。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="check-檢查限制-sql-check-constraint">CHECK 檢查限制 (SQL CHECK Constraint)</h1>



<p>CHECK 限制用來約束欄位中的可用值，以保證該欄位中的資料值都會符合您設定的條件。</p>



<p>假設我們要限制 customer 資料表中的 C_Id 欄位值都必需要大於 0：</p>



<h2 class="wp-block-heading" id="在建立資料表同時-create-table">在建立資料表同時 CREATE TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL <strong>CHECK</strong> (C_Id&gt;0),
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);</code></pre>



<p>或這樣寫：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  <strong>CHECK</strong> (C_Id&gt;0)
);</code></pre>



<p>在 MySQL 增加 CHECK 限制不會有錯誤，但是沒有用，CHECK 並不會被執行喔！</p>



<p>替檢查限制命名與多欄位的檢查：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  <strong>CONSTRAINT</strong> chk_Customer <strong>CHECK</strong> (C_Id&gt;0 <strong>AND</strong> <strong>Name</strong>!='XXX')
);</code></pre>



<h2 class="wp-block-heading" id="更改資料表限制-alter-table">更改資料表限制 ALTER TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>ADD</strong> <strong>CHECK</strong> (C_Id&gt;0);</code></pre>



<p>替主鍵命名與多欄位的組合鍵：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer
<strong>ADD</strong> <strong>CONSTRAINT</strong> chk_Customer <strong>CHECK</strong> (C_Id&gt;0 <strong>AND</strong> <strong>Name</strong>!='XXX');</code></pre>



<h2 class="wp-block-heading" id="移除資料表限制-alter-table">移除資料表限制 ALTER TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>DROP</strong> <strong>CONSTRAINT</strong> chk_Customer;</code></pre>



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



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-constraints/">SQL Constraints — SQL 約束條件總覽</a></li>



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



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</a></li>



<li><a href="https://blog.che-ya.com/sql-not-null/">SQL NOT NULL — 非空值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-default/">SQL DEFAULT — 預設值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵約束</a></li>



<li><a href="https://blog.che-ya.com/sql-foreign-key/">SQL FOREIGN KEY — 外鍵約束</a></li>



<li><a href="https://blog.che-ya.com/sql-insert-into/">SQL INSERT INTO — 新增資料</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-check%2F&amp;linkname=SQL%20CHECK" 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-check%2F&amp;linkname=SQL%20CHECK" 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-check%2F&amp;linkname=SQL%20CHECK" 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-check%2F&#038;title=SQL%20CHECK" data-a2a-url="https://blog.che-ya.com/sql-check/" data-a2a-title="SQL CHECK"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL FOREIGN KEY</title>
		<link>https://blog.che-ya.com/sql-foreign-key/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 20 Aug 2024 05:21:12 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=113</guid>

					<description><![CDATA[外鍵是一個 (或多個) 指向其它資料表中主鍵的欄位，它限制欄位值只能來自另一個資料表的主鍵欄位，用來確定資料的參考完整性 (Referential Integrity)。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="foreign-key-外鍵限制-sql-foreign-key-constraint">FOREIGN KEY 外鍵限制 (SQL FOREIGN KEY Constraint)</h1>



<p>外鍵是一個 (或多個) 指向其它資料表中主鍵的欄位，它限制欄位值只能來自另一個資料表的主鍵欄位，用來確定資料的參考完整性 (Referential Integrity)。</p>



<p>如果想在 MySQL 資料庫中使用外鍵限制，必需讓資料表使用 InnoDB 儲存引擎。</p>



<p>讓我們簡單了解一下什麼是外鍵：</p>



<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>而這是客戶訂單的資料表 orders</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><th>O_Id</th><th>Order_No</th><th>C_Id</th></tr><tr><td>1</td><td>2572</td><td>3</td></tr><tr><td>2</td><td>7375</td><td>3</td></tr><tr><td>3</td><td>7520</td><td>1</td></tr><tr><td>4</td><td>1054</td><td>2</td></tr></tbody></table></figure>



<p>在這裡我們會想有一個限制，就是在客戶訂單資料表中的客戶，都一定要在 customers 資料表中存在。所以我們需要在 orders 資料表中設定一個外鍵，再將此外鍵指向 customers 資料表中的主鍵，以確定所有在 orders 資料表中的客戶都存在於 customers 資料表中，才不會有任何幽靈訂單的出現！</p>



<h2 class="wp-block-heading" id="foreign-key-constraint">FOREIGN KEY Constraint</h2>



<p>假設我們要將 orders 資料表中的 C_Id 欄位設為外鍵，並將它關聯到 customers 資料表中的 C_Id：</p>



<h3 class="wp-block-heading" id="在建立資料表時-create-table">在建立資料表時 CREATE TABLE&#8230;</h3>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> orders (
  O_Id INT <strong>NOT</strong> NULL,
  Order_No INT <strong>NOT</strong> NULL,
  C_Id INT,
  PRIMARY <strong>KEY</strong> (O_Id),
  FOREIGN <strong>KEY</strong> (C_Id) <strong>REFERENCES</strong> customers(C_Id) 
);</code></pre>



<p>替外鍵命名與多欄位的外鍵：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> orders (
  O_Id INT <strong>NOT</strong> NULL PRIMARY <strong>KEY</strong>,
  Order_No INT <strong>NOT</strong> NULL,
  C_Id INT,
  <strong>CONSTRAINT</strong> fk_Cusomer_Id FOREIGN <strong>KEY</strong> (C_Id) <strong>REFERENCES</strong> customers(C_Id) 
);</code></pre>



<p>我們限制 C_Id 為外鍵，CONSTRAINT 後面接著的即是此外鍵的名稱，另一個重點是記得 customers 資料表中需將 C_Id 設為主鍵。</p>



<h3 class="wp-block-heading" id="更改資料表限制-alter-table">更改資料表限制 ALTER TABLE&#8230;</h3>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> orders
<strong>ADD</strong> FOREIGN <strong>KEY</strong> (C_Id) <strong>REFERENCES</strong> customers(C_Id);</code></pre>



<p>替外鍵命名與多欄位的外鍵：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> orders
<strong>ADD</strong> <strong>CONSTRAINT</strong> fk_Cusomer_Id FOREIGN <strong>KEY</strong> (C_Id) <strong>REFERENCES</strong> customers(C_Id);</code></pre>



<h3 class="wp-block-heading" id="移除資料表限制-alter-table">移除資料表限制 ALTER TABLE&#8230;</h3>



<p>MySQL</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> orders <strong>DROP</strong> FOREIGN <strong>KEY</strong> fk_Cusomer_Id;</code></pre>



<p>SQL Server / Oracle / MS Access</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> orders <strong>DROP</strong> <strong>CONSTRAINT</strong> fk_Cusomer_Id;</code></pre>



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



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-constraints/">SQL Constraints — SQL 約束條件總覽</a></li>



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



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵約束</a></li>



<li><a href="https://blog.che-ya.com/sql-not-null/">SQL NOT NULL — 非空值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-check/">SQL CHECK — 檢查約束</a></li>



<li><a href="https://blog.che-ya.com/sql-default/">SQL DEFAULT — 預設值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-index/">SQL INDEX — 索引</a></li>



<li><a href="https://blog.che-ya.com/sql-insert-into/">SQL INSERT INTO — 新增資料</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-foreign-key%2F&amp;linkname=SQL%20FOREIGN%20KEY" 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-foreign-key%2F&amp;linkname=SQL%20FOREIGN%20KEY" 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-foreign-key%2F&amp;linkname=SQL%20FOREIGN%20KEY" 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-foreign-key%2F&#038;title=SQL%20FOREIGN%20KEY" data-a2a-url="https://blog.che-ya.com/sql-foreign-key/" data-a2a-title="SQL FOREIGN KEY"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL PRIMARY KEY</title>
		<link>https://blog.che-ya.com/sql-primary-key/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 16 Aug 2024 03:16:16 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=108</guid>

					<description><![CDATA[PRIMARY KEY 用來保證欄位在資料表中的唯一性，主鍵欄位中的每一筆資料在資料表中都必需是獨一無二的。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">NOT NULL 約束 (SQL NOT NULL Constraint)</h2>



<p>NOT NULL 約束用來確保欄位中的值不能為空值 (NULL)。預設情況下，資料表的欄位可以接受 NULL 值。</p>



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



<pre class="wp-block-code"><code class="">CREATE TABLE table_name (
    column_name datatype NOT NULL
);</code></pre>



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



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-constraints/">SQL Constraints — SQL 約束條件總覽</a></li>



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



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</a></li>



<li><a href="https://blog.che-ya.com/sql-unique/">SQL UNIQUE — 唯一值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-not-null/">SQL NOT NULL — 非空值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-foreign-key/">SQL FOREIGN KEY — 外鍵約束</a></li>



<li><a href="https://blog.che-ya.com/sql-check/">SQL CHECK — 檢查約束</a></li>



<li><a href="https://blog.che-ya.com/sql-default/">SQL DEFAULT — 預設值約束</a></li>



<li><a href="https://blog.che-ya.com/sql-auto-increment/">SQL AUTO INCREMENT — 自動遞增</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-primary-key%2F&amp;linkname=SQL%20PRIMARY%20KEY" 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-primary-key%2F&amp;linkname=SQL%20PRIMARY%20KEY" 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-primary-key%2F&amp;linkname=SQL%20PRIMARY%20KEY" 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-primary-key%2F&#038;title=SQL%20PRIMARY%20KEY" data-a2a-url="https://blog.che-ya.com/sql-primary-key/" data-a2a-title="SQL PRIMARY KEY"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL UNIQUE</title>
		<link>https://blog.che-ya.com/sql-unique/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 13 Aug 2024 03:04:50 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=105</guid>

					<description><![CDATA[UNIQUE 用來保證欄位在資料表中的唯一性，約束資料表中的欄位不能有重複的資料。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="unique-唯一限制-sql-unique-constraint">UNIQUE 唯一限制 (SQL UNIQUE Constraint)</h1>



<p>UNIQUE 用來保證欄位在資料表中的唯一性，約束資料表中的欄位不能有重複的資料。</p>



<p>一個資料表可有多個 UNIQUE 欄位，此外 UNIQUE 欄位中可以接受 NULL 值。</p>



<p>假設我們要對「customer」資料表限制「C_Id」欄位不能有重複值存在：</p>



<h2 class="wp-block-heading" id="在建立資料表時-create-table">在建立資料表時 CREATE TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL <strong>UNIQUE</strong>,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);</code></pre>



<p>或這樣寫：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  <strong>UNIQUE</strong> (C_Id)
);</code></pre>



<h3 class="wp-block-heading" id="替唯一鍵命名與多欄位的唯一限制">替唯一鍵命名與多欄位的唯一限制</h3>



<p>我們限制 C_Id 及 Name 這兩個欄位共同唯一，CONSTRAINT 後面接著的即是此唯一鍵的名稱。</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  <strong>CONSTRAINT</strong> u_Customer_Id <strong>UNIQUE</strong> (C_Id, <strong>Name</strong>)
);</code></pre>



<h2 class="wp-block-heading" id="更改資料表限制-alter-table">更改資料表限制 ALTER TABLE&#8230;</h2>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>ADD</strong> <strong>UNIQUE</strong> (C_Id);</code></pre>



<p>替唯一鍵命名與多欄位的唯一限制：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer
<strong>ADD</strong> <strong>CONSTRAINT</strong> u_Customer_Id <strong>UNIQUE</strong> (C_Id, <strong>Name</strong>);</code></pre>



<h2 class="wp-block-heading" id="移除資料表限制-alter-table">移除資料表限制 ALTER TABLE&#8230;</h2>



<p>MySQL</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>DROP</strong> <strong>INDEX</strong> u_Customer_Id;</code></pre>



<p>SQL Server / Oracle / MS Access</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>ALTER</strong> <strong>TABLE</strong> customer <strong>DROP</strong> <strong>CONSTRAINT</strong> u_Customer_Id;</code></pre>



<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-constraints/">SQL Constraints — SQL 約束條件完整教學</a></li>



<li><a href="https://blog.che-ya.com/sql-not-null/">SQL NOT NULL — 非空約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵約束教學</a></li>



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



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</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-unique%2F&amp;linkname=SQL%20UNIQUE" 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-unique%2F&amp;linkname=SQL%20UNIQUE" 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-unique%2F&amp;linkname=SQL%20UNIQUE" 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-unique%2F&#038;title=SQL%20UNIQUE" data-a2a-url="https://blog.che-ya.com/sql-unique/" data-a2a-title="SQL UNIQUE"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL NOT NULL</title>
		<link>https://blog.che-ya.com/sql-not-null/</link>
					<comments>https://blog.che-ya.com/sql-not-null/#comments</comments>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 09 Aug 2024 02:56:58 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=102</guid>

					<description><![CDATA[NOT NULL 用來限制該欄位不能接受空值，而在預設的情況下，一個欄位是允許接受空值的。]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading" id="not-null-限制-sql-not-null-constraint">NOT NULL 限制 (SQL NOT NULL Constraint)</h1>



<p>NOT NULL 用來限制該欄位不能接受空值，而在預設的情況下，一個欄位是允許接受空值的。</p>



<p>當某欄位限制為 NOT NULL 時，則在新增資料時該欄位一定要有值。</p>



<p>例如，我們建立一張 customer 資料表，並限制其 C_Id 及 Name 欄位值不能是空值：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql line-numbers"><strong>CREATE</strong> <strong>TABLE</strong> customer (
  C_Id INT <strong>NOT</strong> NULL,
  <strong>Name</strong> VARCHAR(50) <strong>NOT</strong> NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);</code></pre>



<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-constraints/">SQL Constraints — SQL 約束條件完整教學</a></li>



<li><a href="https://blog.che-ya.com/sql-unique/">SQL UNIQUE — 唯一約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-default/">SQL DEFAULT — 預設值約束教學</a></li>



<li><a href="https://blog.che-ya.com/create-table/">CREATE TABLE — 建立資料表</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-not-null%2F&amp;linkname=SQL%20NOT%20NULL" 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-not-null%2F&amp;linkname=SQL%20NOT%20NULL" 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-not-null%2F&amp;linkname=SQL%20NOT%20NULL" 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-not-null%2F&#038;title=SQL%20NOT%20NULL" data-a2a-url="https://blog.che-ya.com/sql-not-null/" data-a2a-title="SQL NOT NULL"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.che-ya.com/sql-not-null/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Constraints</title>
		<link>https://blog.che-ya.com/sql-constraints/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 06 Aug 2024 02:52:46 +0000</pubDate>
				<category><![CDATA[資料定義 DDL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=100</guid>

					<description><![CDATA[onstraint 用來有條件地限制哪些資料才可以被存入資料表中，也就是對欄位作約束。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">刪除資料表 (SQL DROP TABLE)</h2>



<p>DROP TABLE 語句用來刪除現有的資料表，包含資料表中的所有資料。使用時請特別小心，因為刪除後就無法復原。</p>



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



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



<p>如果只想刪除資料表中的資料，但保留資料表本身，可以使用 TRUNCATE TABLE：</p>



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



<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-not-null/">SQL NOT NULL — 非空約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-unique/">SQL UNIQUE — 唯一約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-primary-key/">SQL PRIMARY KEY — 主鍵約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-foreign-key/">SQL FOREIGN KEY — 外鍵約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-check/">SQL CHECK — 檢查約束教學</a></li>



<li><a href="https://blog.che-ya.com/sql-default/">SQL DEFAULT — 預設值約束教學</a></li>



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



<li><a href="https://blog.che-ya.com/alter-table/">ALTER TABLE — 修改資料表結構</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-constraints%2F&amp;linkname=SQL%20Constraints" 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-constraints%2F&amp;linkname=SQL%20Constraints" 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-constraints%2F&amp;linkname=SQL%20Constraints" 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-constraints%2F&#038;title=SQL%20Constraints" data-a2a-url="https://blog.che-ya.com/sql-constraints/" data-a2a-title="SQL Constraints"></a></p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
