Previous |
Next |
To add constraints to existing tables, use either SQL Developer tools or the DDL statement ALTER
TABLE
. This topic shows how to use both of these ways to add constraints to the tables created in "Creating Tables".
This tutorial has several procedures. The first procedure (immediately after this paragraph) uses the Edit Table tool to add a Not Null constraint to the NAMES
column of the PERFORMANCE_PARTS
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.
Note: After any step of the tutorial, you can view the constraints that a table has:
In the navigation frame, select the name of the table.
In the right frame, click the tab Constraints.
For more information about viewing table properties and data, see "Tutorial: Viewing EMPLOYEES Table Properties and Data".
To add a Not Null constraint using the Edit Table tool:
On the Connections tab, expand hr_conn.
Under the hr_conn
icon, a list of schema object types appears.
Expand Tables.
A list of tables appears.
Right-click PERFORMANCE_PARTS.
A list of choices appears.
Click Edit.
The Edit Table window opens. By default, Columns is selected, the columns of the PERFORMANCE_PARTS
table are listed, the column PERFORMANCE_ID
is selected, and its properties are listed.
Click the column NAME.
The properties of the column NAME
appear. The property "Cannot be NULL" is deselected.
Select Cannot be NULL.
Click OK.
The Not Null constraint is added to the NAME
column of the PERFORMANCE_PARTS
table.
The following procedure uses the ALTER
TABLE
statement to add a Not Null constraint to the WEIGHT
column of the PERFORMANCE_PARTS
table.
To add a Not Null constraint using the ALTER TABLE statement:
Click the icon SQL Worksheet.
The SQL Worksheet pane appears.
In the SQL Worksheet pane, type this statement:
ALTER TABLE PERFORMANCE_PARTS MODIFY WEIGHT NOT NULL;
Click the icon Execute Statement.
The statement runs, adding the Not Null constraint to the WEIGHT
column of the PERFORMANCE_PARTS
table.
The following procedure uses the Add Unique tool to add a Unique constraint to the SCORES
table.
To add a Unique constraint using the Add Unique tool:
On the Connections tab, expand hr_conn.
Under the hr_conn
icon, a list of schema object types appears.
Expand Tables.
A list of tables appears.
Right-click SCORES.
A list of choices appears.
Click Constraint.
A list of choices appears.
Click Add Unique.
The Add Unique window opens.
For Constraint Name, enter SCORES_EVAL_PERF_UNIQUE
.
For Column 1, select EVALUATION_ID
from the drop-down menu.
For Column 2, select PERFORMANCE_ID
from the drop-down menu.
Click Apply.
The Confirmation window opens.
Click OK.
A unique constraint named SCORES_EVAL_PERF_UNIQUE
is added to the SCORES
table.
The following procedure uses the Add Primary Key tool to add a Primary Key constraint to the PERFORMANCE_ID
column of the PERFORMANCE_PARTS
table.
To add a Primary Key constraint using the Add Primary Key tool:
On the Connections tab, expand hr_conn.
Under the hr_conn
icon, a list of schema object types appears.
Expand Tables.
A list of tables appears.
Right-click PERFORMANCE_PARTS.
A list of choices appears.
Click Constraint.
A list of choices appears.
Click Add Primary Key.
The Add Primary Key window opens.
For Primary Key Name, enter PERF_PERF_ID_PK
.
For Column 1, select PERFORMANCE_ID
from the drop-down menu.
Click Apply.
The Confirmation window opens.
Click OK.
A primary key constraint named PERF_PERF_ID_PK
is added to the PERFORMANCE_ID
column of the PERFORMANCE_PARTS
table.
The following procedure uses the ALTER
TABLE
statement to add a Primary Key constraint to the EVALUATION_ID
column of the EVALUATIONS
table.
To add a Primary Key constraint using the ALTER TABLE statement:
Click the icon SQL Worksheet.
The SQL Worksheet pane appears. Under "Enter SQL Statement:" is a field where you can enter a SQL statement.
In the SQL Worksheet pane, type this statement:
ALTER TABLE EVALUATIONS ADD CONSTRAINT EVAL_EVAL_ID_PK PRIMARY KEY (EVALUATION_ID);
Click the icon Execute Statement.
The statement runs, adding the Primary Key constraint to the EVALUATION_ID
column of the EVALUATIONS
table.
The following procedure uses the Add Foreign Key tool to add two Foreign Key constraints to the SCORES
table.
To add two Foreign Key constraints using the Add Foreign Key tool:
On the Connections tab, expand hr_conn.
Under the hr_conn
icon, a list of schema object types appears.
Expand Tables.
A list of tables appears.
Right-click SCORES.
A list of choices appears.
Click Constraint.
A list of choices appears.
Click Add Foreign Key.
The Add Foreign Key window opens.
For Foreign Key Name, enter SCORES_EVAL_FK
.
For Column Name, select EVALUATION_ID
from the drop-down menu.
For Reference Table Name, select EVALUATIONS
from the drop-down menu.
For Referencing Column, select EVALUATION_ID
from the drop-down menu.
Click Apply.
The Confirmation window opens.
Click OK.
A foreign key constraint named SCORES_EVAL_FK
is added to the EVALUTION_ID
column of the SCORES
table, referencing the EVALUTION_ID
column of the EVALUATIONS
table.
The following steps add another foreign key constraint to the SCORES
table.
Right-click SCORES.
A list of tables appears.
Click Constraint.
A list of choices appears.
Click Add Foreign Key.
The Add Foreign Key window opens.
For Foreign Key Name, enter SCORES_PERF_FK
.
For Column Name, select PERFORMANCE_ID
from the drop-down menu.
For Reference Table Name, select PERFORMANCE_PARTS
from the drop-down menu.
For Referencing Column, select PERFORMANCE_ID
from the drop-down menu.
Click Apply.
The Confirmation window opens.
Click OK.
A foreign key constraint named SCORES_PERF_FK
is added to the EVALUTION_ID
column of the SCORES
table, referencing the EVALUTION_ID
column of the EVALUATIONS
table.
The following procedure uses the ALTER
TABLE
statement to add a Foreign Key constraint to the EMPLOYEE_ID
column of the EVALUATIONS
table, referencing the EMPLOYEE_ID
column of the EMPLOYEES
table.
To add a Foreign Key constraint using the ALTER TABLE statement:
Click the icon SQL Worksheet.
The SQL Worksheet pane appears. Under "Enter SQL Statement:" is a field where you can enter a SQL statement.
In the SQL Worksheet pane, type this statement:
ALTER TABLE EVALUATIONS ADD CONSTRAINT EVAL_EMP_ID_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEES (EMPLOYEE_ID);
Click the icon Execute Statement.
The statement runs, adding the Foreign Key constraint to the EMPLOYEE_ID
column of the EVALUATIONS
table, referencing the EMPLOYEE_ID
column of the EMPLOYEES
table.
The following procedure uses the Add Check tool to add a Check constraint to the SCORES
table.
To add a Check constraint using the Add Check tool:
On the Connections tab, expand hr_conn.
Under the hr_conn
icon, a list of schema object types appears.
Expand Tables.
A list of tables appears.
Right-click SCORES.
A list of choices appears.
Click Constraint.
A list of choices appears.
Click Add Check.
The Add Check window opens.
For Constraint Name, enter SCORE_VALID
.
For Check Condition, enter score
>=
0
and
score
<=
9
.
For Status, accept the default, ENABLE
.
Click Apply.
The Confirmation window opens.
Click OK.
A Check constraint named SCORE_VALID
is added to the SCORES
table.
Oracle Database SQL Language Reference for more information about the ALTER
TABLE
statement
Oracle Database SQL Developer User's Guide
Oracle Database SQL Language Reference for more information about the CREATE
TABLE
statement
About Data Definition Language (DDL) Statements