Edit D:\app\Administrator\product\11.2.0\dbhome_1\oc4j\j2ee\oc4j_applications\applications\em\em\online_help\tdddg\tdddg_objects010.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <script src="./callback.js" type="text/javascript"></script> <noscript>Your browser does not support JavaScript. This help page requires JavaScript to render correctly.</noscript> </head> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 5.1.1 Build 005" /> <meta name="date" content="2009-04-21T9:46:24Z" /> <meta name="robots" content="noarchive" /> <meta name="doctitle" content="Tutorial: Adding Constraints to Existing Tables" /> <meta name="relnum" content="11g Release 2 (11.2)" /> <meta name="partnum" content="E10766-01" /> <link rel="copyright" href="./dcommon/html/cpyr.htm" title="Copyright" type="text/html" /> <link rel="stylesheet" href="./dcommon/css/blafdoc.css" title="Oracle BLAFDoc" type="text/css" /> <link rel="contents" href="toc.htm" title="Contents" type="text/html" /> <link rel="prev" href="tdddg_objects009.htm" title="Previous" type="text/html" /> <link rel="next" href="tdddg_objects011.htm" title="Next" type="text/html" /> <title>Tutorial: Adding Constraints to Existing Tables</title> </head> <body> <div class="zz-skip-header"><a href="#BEGIN">Skip Headers</a></div> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr valign="bottom"> <td align="left"></td> <td align="center"><a href="tdddg_objects009.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_objects011.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> <p><a id="CIHHFGCB" name="CIHHFGCB"></a><a id="TDDDG32220" name="TDDDG32220"></a></p> <div class="sect3"> <h1>Tutorial: Adding Constraints to Existing Tables</h1> <a name="BEGIN" id="BEGIN"></a> <p>To add constraints to existing tables, use either SQL Developer tools or the DDL statement <code>ALTER</code> <code>TABLE</code>. This topic shows how to use both of these ways to add constraints to the tables created in <a href="tdddg_objects005.htm#BABFEIDE">"Creating Tables"</a>.</p> <p>This tutorial has several procedures. <a id="sthref212" name="sthref212"></a><a id="sthref213" name="sthref213"></a>The first procedure (immediately after this paragraph) uses the <a id="sthref214" name="sthref214"></a>Edit Table tool to add a <a id="sthref215" name="sthref215"></a>Not Null constraint to the <code>NAMES</code> column of the <a id="sthref216" name="sthref216"></a><code>PERFORMANCE_PARTS</code> table. The remaining procedures show how to use other tools to add constraints; however, you could add the same constraints using the Edit Table tool.</p> <div class="helpinfonote"> <p><span class="bold">Note: </span>After any step of the tutorial, you can view the constraints that a table has: <ol> <li> <p>In the navigation frame, select the name of the table.</p> </li> <li> <p>In the right frame, click the tab <span class="bold">Constraints</span>.</p> </li> </ol> <p>For more information about viewing table properties and data, see <a href="tdddg_exploring002.htm#BABGFBBA">"Tutorial: Viewing EMPLOYEES Table Properties and Data"</a>.</p> </p> </div> <a id="TDDDG190" name="TDDDG190"></a> <p class="subhead2">To add a Not Null constraint using the Edit Table tool:</p> <ol> <li> <p>On the Connections tab, expand <span class="bold">hr_conn</span>.</p> <p>Under the <code>hr_conn</code> icon, a list of schema object types appears.</p> </li> <li> <p>Expand <span class="bold">Tables</span>.</p> <p>A list of tables appears.</p> </li> <li> <p>Right-click <span class="bold">PERFORMANCE_PARTS</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Edit</span>.</p> <p>The Edit Table window opens. By default, Columns is selected, the columns of the <code>PERFORMANCE_PARTS</code> table are listed, the column <code>PERFORMANCE_ID</code> is selected, and its properties are listed.</p> </li> <li> <p>Click the column <span class="bold">NAME</span>.</p> <p>The properties of the column <code>NAME</code> appear. The property "Cannot be NULL" is deselected.</p> </li> <li> <p>Select <span class="bold">Cannot be NULL</span>.</p> </li> <li> <p>Click <span class="bold">OK</span>.</p> <p>The Not Null constraint is added to the <code>NAME</code> column of the <code>PERFORMANCE_PARTS</code> table.</p> </li> </ol> <p><a id="sthref217" name="sthref217"></a><a id="sthref218" name="sthref218"></a>The following procedure uses the <a id="sthref219" name="sthref219"></a><code>ALTER</code> <code>TABLE</code> statement to add a <a id="sthref220" name="sthref220"></a>Not Null constraint to the <code>WEIGHT</code> column of the <a id="sthref221" name="sthref221"></a><code>PERFORMANCE_PARTS</code> table.</p> <a id="TDDDG191" name="TDDDG191"></a> <p class="subhead2">To add a Not Null constraint using the ALTER TABLE statement:</p> <ol> <li> <p>Click the icon <span class="bold">SQL Worksheet</span>.</p> <p>The SQL Worksheet pane appears.</p> </li> <li> <p>In the SQL Worksheet pane, type this statement:</p> <pre xml:space="preserve" class="oac_no_warn"> ALTER TABLE PERFORMANCE_PARTS MODIFY WEIGHT NOT NULL; </pre></li> <li> <p>Click the icon <span class="bold">Execute Statement</span>.</p> <p>The statement runs, adding the Not Null constraint to the <code>WEIGHT</code> column of the <code>PERFORMANCE_PARTS</code> table.</p> </li> </ol> <p><a id="sthref222" name="sthref222"></a>The following procedure uses the <a id="sthref223" name="sthref223"></a>Add Unique tool to add a Unique constraint to the <a id="sthref224" name="sthref224"></a><code>SCORES</code> table.</p> <a id="TDDDG192" name="TDDDG192"></a> <p class="subhead2">To add a Unique constraint using the Add Unique tool:</p> <ol> <li> <p>On the Connections tab, expand <span class="bold">hr_conn</span>.</p> <p>Under the <code>hr_conn</code> icon, a list of schema object types appears.</p> </li> <li> <p>Expand <span class="bold">Tables</span>.</p> <p>A list of tables appears.</p> </li> <li> <p>Right-click <span class="bold">SCORES</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Constraint</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Add Unique</span>.</p> <p>The Add Unique window opens.</p> </li> <li> <p>For Constraint Name, enter <code>SCORES_EVAL_PERF_UNIQUE</code>.</p> </li> <li> <p>For Column 1, select <code>EVALUATION_ID</code> from the drop-down menu.</p> </li> <li> <p>For Column 2, select <code>PERFORMANCE_ID</code> from the drop-down menu.</p> </li> <li> <p>Click <span class="bold">Apply</span>.</p> <p>The Confirmation window opens.</p> </li> <li> <p>Click <span class="bold">OK</span>.</p> <p>A unique constraint named <code>SCORES_EVAL_PERF_UNIQUE</code> is added to the <code>SCORES</code> table.</p> </li> </ol> <p><a id="sthref225" name="sthref225"></a>The following procedure uses the <a id="sthref226" name="sthref226"></a>Add Primary Key tool to add a Primary Key constraint to the <code>PERFORMANCE_ID</code> column of the <a id="sthref227" name="sthref227"></a><code>PERFORMANCE_PARTS</code> table.</p> <a id="TDDDG193" name="TDDDG193"></a> <p class="subhead2">To add a Primary Key constraint using the Add Primary Key tool:</p> <ol> <li> <p>On the Connections tab, expand <span class="bold">hr_conn</span>.</p> <p>Under the <code>hr_conn</code> icon, a list of schema object types appears.</p> </li> <li> <p>Expand <span class="bold">Tables</span>.</p> <p>A list of tables appears.</p> </li> <li> <p>Right-click <span class="bold">PERFORMANCE_PARTS</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Constraint</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Add Primary Key</span>.</p> <p>The Add Primary Key window opens.</p> </li> <li> <p>For Primary Key Name, enter <code>PERF_PERF_ID_PK</code>.</p> </li> <li> <p>For Column 1, select <code>PERFORMANCE_ID</code> from the drop-down menu.</p> </li> <li> <p>Click <span class="bold">Apply</span>.</p> <p>The Confirmation window opens.</p> </li> <li> <p>Click <span class="bold">OK</span>.</p> <p>A primary key constraint named <code>PERF_PERF_ID_PK</code> is added to the <code>PERFORMANCE_ID</code> column of the <code>PERFORMANCE_PARTS</code> table.</p> </li> </ol> <p><a id="sthref228" name="sthref228"></a>The following procedure uses the <a id="sthref229" name="sthref229"></a><code>ALTER</code> <code>TABLE</code> statement to add a Primary Key constraint to the <code>EVALUATION_ID</code> column of the <a id="sthref230" name="sthref230"></a><code>EVALUATIONS</code> table.</p> <a id="TDDDG194" name="TDDDG194"></a> <p class="subhead2">To add a Primary Key constraint using the ALTER TABLE statement:</p> <ol> <li> <p>Click the icon <span class="bold">SQL Worksheet</span>.</p> <p>The SQL Worksheet pane appears. Under "Enter SQL Statement:" is a field where you can enter a SQL statement.</p> </li> <li> <p>In the SQL Worksheet pane, type this statement:</p> <pre xml:space="preserve" class="oac_no_warn"> ALTER TABLE EVALUATIONS ADD CONSTRAINT EVAL_EVAL_ID_PK PRIMARY KEY (EVALUATION_ID); </pre></li> <li> <p>Click the icon <span class="bold">Execute Statement</span>.</p> <p>The statement runs, adding the Primary Key constraint to the <code>EVALUATION_ID</code> column of the <code>EVALUATIONS</code> table.</p> </li> </ol> <p><a id="sthref231" name="sthref231"></a>The following procedure uses the <a id="sthref232" name="sthref232"></a>Add Foreign Key tool to add two Foreign Key constraints to the <a id="sthref233" name="sthref233"></a><code>SCORES</code> table.</p> <a id="TDDDG195" name="TDDDG195"></a> <p class="subhead2">To add two Foreign Key constraints using the Add Foreign Key tool:</p> <ol> <li> <p>On the Connections tab, expand <span class="bold">hr_conn</span>.</p> <p>Under the <code>hr_conn</code> icon, a list of schema object types appears.</p> </li> <li> <p>Expand <span class="bold">Tables</span>.</p> <p>A list of tables appears.</p> </li> <li> <p>Right-click <span class="bold">SCORES</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Constraint</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Add Foreign Key</span>.</p> <p>The Add Foreign Key window opens.</p> </li> <li> <p>For Foreign Key Name, enter <code>SCORES_EVAL_FK</code>.</p> </li> <li> <p>For Column Name, select <code>EVALUATION_ID</code> from the drop-down menu.</p> </li> <li> <p>For Reference Table Name, select <code>EVALUATIONS</code> from the drop-down menu.</p> </li> <li> <p>For Referencing Column, select <code>EVALUATION_ID</code> from the drop-down menu.</p> </li> <li> <p>Click <span class="bold">Apply</span>.</p> <p>The Confirmation window opens.</p> </li> <li> <p>Click <span class="bold">OK</span>.</p> <p>A foreign key constraint named <code>SCORES_EVAL_FK</code> is added to the <code>EVALUTION_ID</code> column of the <code>SCORES</code> table, referencing the <code>EVALUTION_ID</code> column of the <code>EVALUATIONS</code> table.</p> <p>The following steps add another foreign key constraint to the <code>SCORES</code> table.</p> </li> <li> <p>Right-click <span class="bold">SCORES</span>.</p> <p>A list of tables appears.</p> </li> <li> <p>Click <span class="bold">Constraint</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Add Foreign Key</span>.</p> <p>The Add Foreign Key window opens.</p> </li> <li> <p>For Foreign Key Name, enter <code>SCORES_PERF_FK</code>.</p> </li> <li> <p>For Column Name, select <code>PERFORMANCE_ID</code> from the drop-down menu.</p> </li> <li> <p>For Reference Table Name, select <code>PERFORMANCE_PARTS</code> from the drop-down menu.</p> </li> <li> <p>For Referencing Column, select <code>PERFORMANCE_ID</code> from the drop-down menu.</p> </li> <li> <p>Click <span class="bold">Apply</span>.</p> <p>The Confirmation window opens.</p> </li> <li> <p>Click <span class="bold">OK</span>.</p> <p>A foreign key constraint named <code>SCORES_PERF_FK</code> is added to the <code>EVALUTION_ID</code> column of the <code>SCORES</code> table, referencing the <code>EVALUTION_ID</code> column of the <code>EVALUATIONS</code> table.</p> </li> </ol> <p><a id="sthref234" name="sthref234"></a>The following procedure uses the <a id="sthref235" name="sthref235"></a><code>ALTER</code> <code>TABLE</code> statement to add a Foreign Key constraint to the <code>EMPLOYEE_ID</code> column of the <a id="sthref236" name="sthref236"></a><code>EVALUATIONS</code> table, referencing the <code>EMPLOYEE_ID</code> column of the <code>EMPLOYEES</code> table.</p> <a id="TDDDG196" name="TDDDG196"></a> <p class="subhead2">To add a Foreign Key constraint using the ALTER TABLE statement:</p> <ol> <li> <p>Click the icon <span class="bold">SQL Worksheet</span>.</p> <p>The SQL Worksheet pane appears. Under "Enter SQL Statement:" is a field where you can enter a SQL statement.</p> </li> <li> <p>In the SQL Worksheet pane, type this statement:</p> <pre xml:space="preserve" class="oac_no_warn"> ALTER TABLE EVALUATIONS ADD CONSTRAINT EVAL_EMP_ID_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEES (EMPLOYEE_ID); </pre></li> <li> <p>Click the icon <span class="bold">Execute Statement</span>.</p> <p>The statement runs, adding the Foreign Key constraint to the <code>EMPLOYEE_ID</code> column of the <code>EVALUATIONS</code> table, referencing the <code>EMPLOYEE_ID</code> column of the <code>EMPLOYEES</code> table.</p> </li> </ol> <p><a id="sthref237" name="sthref237"></a>The following procedure uses the <a id="sthref238" name="sthref238"></a>Add Check tool to add a Check constraint to the <a id="sthref239" name="sthref239"></a><code>SCORES</code> table.</p> <a id="TDDDG197" name="TDDDG197"></a> <p class="subhead2">To add a Check constraint using the Add Check tool:</p> <ol> <li> <p>On the Connections tab, expand <span class="bold">hr_conn</span>.</p> <p>Under the <code>hr_conn</code> icon, a list of schema object types appears.</p> </li> <li> <p>Expand <span class="bold">Tables</span>.</p> <p>A list of tables appears.</p> </li> <li> <p>Right-click <span class="bold">SCORES</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Constraint</span>.</p> <p>A list of choices appears.</p> </li> <li> <p>Click <span class="bold">Add Check</span>.</p> <p>The Add Check window opens.</p> </li> <li> <p>For Constraint Name, enter <code>SCORE_VALID</code>.</p> </li> <li> <p>For Check Condition, enter <code>score</code> <code>>=</code> <code>0</code> <code>and</code> <code>score</code> <code><=</code> <code>9</code>.</p> </li> <li> <p>For Status, accept the default, <code>ENABLE</code>.</p> </li> <li> <p>Click <span class="bold">Apply</span>.</p> <p>The Confirmation window opens.</p> </li> <li> <p>Click <span class="bold">OK</span>.</p> <p>A Check constraint named <code>SCORE_VALID</code> is added to the <code>SCORES</code> table.</p> </li> </ol> <div class="helpinfonotealso"> <h2>Related Topics</h2> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF01001','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about the <code>ALTER</code> <code>TABLE</code> statement</p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=RPTUG20100','newWindow').focus()"><span class="italic">Oracle Database SQL Developer User's Guide</span></a></p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF01402','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a> for more information about the <code>CREATE</code> <code>TABLE</code> statement</p> <p><a href="tdddg_objects001.htm#CIHGAJDJ">About Data Definition Language (DDL) Statements</a></p> <p><a href="tdddg_intro007.htm#CEGHIBFI">About SQL Developer</a></p> <p><a href="tdddg_objects008.htm#BABJFCCE">Ensuring Data Integrity in Tables</a></p> <p><a href="tdddg_objects005.htm#BABFEIDE">Creating Tables</a></p> </div> </div> <!-- class="sect3" --> <!-- class="sect2" --> <!-- Start Footer --> <div class="footer"> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr> <td align="left"><span class="copyrightlogo">Copyright © 1996, 2009, Oracle and/or its affiliates. All rights reserved.</span><br /> <a href="./dcommon/html/cpyr.htm"><span class="copyrightlogo">Legal Notices</span></a></td> <td align="center"><a href="tdddg_objects009.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_objects011.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> </div> <!-- class="footer" --> </body> </html>
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de