# Defining a Referential Integrity Constraint by Creating an Entity Constraint

The Entity Constraint Wizard lets you add constraints, which are used when you generate tables with the Create Database Objects Tool (forward generation).

You can also create constraints while you define associations. In the Association Properties panel of the Association Wizard and Editor, select Use Database Key Constraints to automatically create a constraint based on the association.

An entity constraint is a business logic tier object that represents a key constraint in the database. An entity constraint describes, in terms of entity objects and attributes, the database-level relationships between tables and columns. You select the entity object's attributes and define the constraint in terms of database integrity constraints such as primary, foreign, check, or unique. When you create a database object based on an entity object, the definition of the entity constraint is used to detect associations between tables, create the specified key constraints in the database, and ensure that data in the database is valid and conforms to the key constraints.

#### To create an entity constraint with the Entity Constraint Wizard:

1. In the Workspace view of the Navigator, right-click an entity object and choose Create Entity Constraint.

2. If the Welcome page appears, click Next.

3. In the Name page, enter the Java name for your key constraint in the Name field. For example, `EmployeeSalaryKey`.

4. In the Constraint Name field enter the name of the constraint that the database will use. JDeveloper will provide a default name based on your choice of a constraint name. For example, EMPLOYEE_SALARY. Click Next.

5. In the Attributes page, select the attributes that will participate in the key constraint. Use the right arrow button to move the desired items from the Available to the Selected list. Click Next.

6. In the Properties page, enter specific details about the key constraint.

> > Key Constraint - Select the type of key constraint you want to create:

> > - Primary - The attributes or set of attributes you chose in the Attributes panel will be interpreted as columns participating in the table’s primary key constraint. The attributes will be used to represent unique values in the corresponding table columns. A primary key’s values uniquely identify the rows in a table. The primary key integrity constraint guarantees that:

> >   - No two rows of a table have duplicate values in the specified column or set of columns.

> >   - The primary key columns do not allow nulls (that is, a value must exist for the primary key columns in each row).

> >   Only one primary key can be defined in a table.

> > - Unique - The attributes or set of attributes you chose in the Attributes panel will be interpreted as columns participating in the table’s unique key constraint. The unique key guarantees that every value in a column or set of columns be unique; that is, no two rows of a table can have duplicate values in a specified column or set of columns. You can define one or more unique keys for each table.

> > - Check - The check integrity constraint does not use attributes. This constraint on a column or set of columns requires that a specified condition be true for every row of the table. If a DML statement results in the condition of the Check constraint evaluating to false, the statement is rolled back. If you select Check, you must also enter a Condition. An example of a condition would be a PL/SQL code fragment that checks that a column value is "not null".

> >   The Condition of a Check constraint has some limitations:

> >   - It must be a Boolean expression evaluated using the values in the row being inserted or updated.

> >   - It cannot contain subqueries, sequences, the SQL functions SYSDATE, UID, USER, USERENV, or the pseudocolumns LEVEL or ROWNUM.

> > - Foreign - The attributes or set of attributes you chose in the Attributes panel will be interpreted as columns participating in a referential integrity constraint. A referential integrity constraint requires that for each row of a table, the value in the foreign key matches a value in the parent key. If you select Foreign, you must also enter the database name of a primary or unique key that the foreign key references.

> >   - References - Enter the database name of the primary or unique key that the foreign key references.

> >   - Cascade Delete - If you select this option, when rows containing referenced key values are deleted, then all rows in child tables with dependent foreign key values are also deleted; that is, the delete "cascades". For example, if a row in a parent table is deleted, and this row's primary key value is referenced by one or more foreign key values in a child table, then the rows in the child table that reference the primary key value are also deleted from the child table.

> > Validation Options - Select from these key validation options:

> > - Mandatory - If you select Mandatory, the column or set of columns must be non-null.

> > - Deferrable validation - Indicates that constraint checking can be deferred until the end of the transaction.

> >   - Initially deferred validation - Implies that this constraint is DEFERRABLE and specifies that, by default, the constraint is checked only at the end of each transaction.

> > - Disable validation - Disables the constraint, drops the index on the constraint, and disallows any modification of the constrained columns. Any new or modified columns can violate the constraint.

> > - Enable validation, validate existing data - The constraint is checked and must be true for all new and per-existing column data.

> > - Enable validation - The constraint is checked and is guaranteed to hold for all new rows. Pre-existing column data is not checked.

7. When you are finished, click Finish to complete the wizard.