uTools-Manuals/docs/sql/FOREIGN KEY 约束.html
2019-05-07 10:15:08 +08:00

27 lines
4.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="m-bg">
<h1>SQL FOREIGN KEY 约束</h1>
<h2>SQL FOREIGN KEY 约束</h2> <p>一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY。</p> <p>让我们通过一个实例来解释外键。请看下面两个表:</p> <p>"Persons" 表:</p> <table class="reference notranslate">
<tr>
<th align="left">P_Id</th> <th align="left">LastName</th> <th align="left">FirstName</th> <th align="left">Address</th> <th align="left">City</th> </tr>
<tr>
<td>1</td> <td>Hansen</td> <td>Ola</td> <td>Timoteivn 10</td> <td>Sandnes</td> </tr>
<tr>
<td>2</td> <td>Svendson</td> <td>Tove</td> <td>Borgvn 23</td> <td>Sandnes</td> </tr>
<tr>
<td>3</td> <td>Pettersen</td> <td>Kari</td> <td>Storgt 20</td> <td>Stavanger</td> </tr>
</table>
<p>"Orders" 表:</p> <table class="reference notranslate">
<tr>
<th align="left" width="15%">O_Id</th> <th align="left" width="20%">OrderNo</th> <th align="left" width="15%">P_Id</th> </tr>
<tr>
<td>1</td> <td>77895</td> <td>3</td> </tr>
<tr>
<td>2</td> <td>44678</td> <td>3</td> </tr>
<tr>
<td>3</td> <td>22456</td> <td>2</td> </tr>
<tr>
<td>4</td> <td>24562</td> <td>1</td> </tr>
</table>
<p>请注意,"Orders" 表中的 "P_Id" 列指向 "Persons" 表中的 "P_Id" 列。</p> <p>"Persons" 表中的 "P_Id" 列是 "Persons" 表中的 PRIMARY KEY。</p> <p>"Orders" 表中的 "P_Id" 列是 "Orders" 表中的 FOREIGN KEY。</p> <p>FOREIGN KEY 约束用于预防破坏表之间连接的行为。</p> <p>FOREIGN KEY 约束也能防止非法数据插入外键列,因为它必须是它指向的那个表中的值之一。</p> <h2>CREATE TABLE 时的 SQL FOREIGN KEY 约束</h2> <p>下面的 SQL 在 "Orders" 表创建时在 "P_Id" 列上创建 FOREIGN KEY 约束:</p> <p><b>MySQL</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> CREATE TABLE Orders<br/> (<br/> O_Id int NOT NULL,<br/> OrderNo int NOT NULL,<br/> P_Id int,<br/> PRIMARY KEY (O_Id),<br/> FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)<br/> )</div></code></pre></div> <p><b>SQL Server / Oracle / MS Access</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> CREATE TABLE Orders<br/> (<br/> O_Id int NOT NULL PRIMARY KEY,<br/> OrderNo int NOT NULL,<br/> P_Id int FOREIGN KEY REFERENCES Persons(P_Id)<br/> )</div></code></pre></div> <p>如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束,请使用下面的 SQL 语法:</p> <p><b>MySQL / SQL Server / Oracle / MS Access</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> CREATE TABLE Orders<br/> (<br/> O_Id int NOT NULL,<br/> OrderNo int NOT NULL,<br/> P_Id int,<br/> PRIMARY KEY (O_Id),<br/> CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)<br/> REFERENCES Persons(P_Id)<br/> )</div></code></pre></div> <h2>ALTER TABLE 时的 SQL FOREIGN KEY 约束</h2> <p>当 "Orders" 表已被创建时,如需在 "P_Id" 列创建 FOREIGN KEY 约束,请使用下面的 SQL</p> <p><b>MySQL / SQL Server / Oracle / MS Access</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> ALTER TABLE Orders<br/> ADD FOREIGN KEY (P_Id)<br/> REFERENCES Persons(P_Id)</div></code></pre></div> <p>如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束,请使用下面的 SQL 语法:</p> <p><b>MySQL / SQL Server / Oracle / MS Access</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> ALTER TABLE Orders<br/> ADD CONSTRAINT fk_PerOrders<br/> FOREIGN KEY (P_Id)<br/> REFERENCES Persons(P_Id)</div></code></pre></div> <h2>撤销 FOREIGN KEY 约束</h2> <p>如需撤销 FOREIGN KEY 约束,请使用下面的 SQL</p> <p><b>MySQL</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> ALTER TABLE Orders<br/> DROP FOREIGN KEY fk_PerOrders</div></code></pre></div> <p><b>SQL Server / Oracle / MS Access</b></p> <div class="code notranslate"><pre><code class="language-sql"><div> ALTER TABLE Orders<br/> DROP CONSTRAINT fk_PerOrders</div></code></pre></div> <br/><div class="text-center padding-10 margin-t-5">
</div>
</div>