Example: Adding a New Table Constraint

In this example, you use SQL Developer to add a table constraint to the purchase_orders table that you created in "Example: Creating a Table". To enforce the rule that the po_date_received value must be either the same day as, or later than, the value of po_date, you add a check constraint.

Note:

You can also add constraints during table creation, as shown in "Example: Creating a Table". In that example, you added a primary key constraint.

To add a table constraint to the PURCHASE_ORDERS table:

  1. Navigate to the PURCHASE_ORDERS table in the HR schema, following the instructions in "Viewing Tables".
  2. Right-click the PURCHASE_ORDERS table and select Edit.

    The Edit Table dialog box appears.

  3. Under the Search box, click Constraints.
  4. To the right of the Constraints section, click the green plus sign icon and choose New Check Constraint.

    In the Constraints section, a new row appears at the bottom of the list of constraints.

  5. In the Name field for the new constraint, enter PO_CHECK_RCVD_DATE.
  6. Leave the check mark in the Enabled column and leave Not Deferrable.
  7. In the Check Condition section, enter the following condition for this constraint:

    PO_DATE_RECEIVED >= PO_DATE

    This expression indicates that PO_DATE_RECEIVED must be greater than or equal to PO_DATE. For date columns, this is equivalent to stating that PO_DATE_RECEIVED must be on the same day as, or later than, PO_DATE.

  8. Click OK.
  9. On the PURCHASE_ORDERS tab in the object pane, click the Constraints subtab to view the current constraints in the table.