Description

A column for which the NOT NULL constraint has been defined must have a certain value that is not NULL. The NOT NULL constraint can be defined for all columns. An error occurs if you try to insert a NULL value into a column with the NOT NULL constraint by using the INSERT or UPDATE statement.

Description

The UNIQUE constraint enforces a column to have a unique value. An error occurs if a new record that has the same value as the existing one is added by this constraint.

You can place a UNIQUE constraint on either a column or a set of columns. If the UNIQUE constraint is defined for multiple columns, the uniqueness is ensured not for each column, but the combination of multiple columns.

Example

If a UNIQUE constraint is defined on a set of columns, this ensures the uniqueness of the values in all the columns. As shown below, the second INSERT statement succeeds because the value of column a is the same, but the value of column b is unique. The third INSERT statement causes an error because the values of column a and b are the same as those in the first INSERT statement.

--UNIQUE constraint is defined on a single column only

CREATE TABLE const_tbl5(id INT UNIQUE, phone VARCHAR);

INSERT INTO const_tbl5(id) VALUES (NULL), (NULL);

INSERT INTO const_tbl5 VALUES (1, '000-0000');

SELECT * FROM const_tbl5;

id phone

===================================

NULL NULL

NULL NULL

1 '000-0000'

INSERT INTO const_tbl5 VALUES (1, '111-1111');

ERROR: Operation would have caused one or more unique constraint violations.

Description

A key in a table is a set of column(s) that uniquely identifies each row. A candidate key is a set of columns that uniquely identifies each row of the table. You can define one of such candidate keys a primary key. That is, the column defined as a primary key is uniquely identified in each row.

By default, the index created by defining the primary key is created in ascending order, and you can define the order by specifying ASC or DESC keyword next to the column.

Description

A foreign key is a column or a set of columns that references the primary key in other tables in order to maintain reference relationship. The foreign key and the referenced primary key must have the same data type. Consistency between two tables is maintained by the foreign key referencing the primary key, which is called referential integrity.

foreign_key_name : Specifies a name of the FOREIGN KEY constraint. You can skip the name specification. However, if you specify this value, constraint_name will be ignored, and the specified value will be used.

column_name : Specifies the name of the column to be defined as a foreign key after the FOREIGN KEY keyword. There is no limit on the number of foreign keys to be defined (the number of columns), but it must be the same number as that of the referred primary keys.

referenced_table_name : Specifies the name of the table to be referenced.

column_name : Specifies the name of the referred primary key column after the FOREIGN KEY keyword.

referential_triggered_action : Specifies the trigger action that responds to a certain operation in order to maintain referential integrity. ON UPDATE, ON DELETE or ON CACHE OBJECT can be specified. Each action can be defined multiple times, and the definition order is not significant.

ON UPDATE : Defines the action to be performed when attempting to update the primary key referenced by the foreign key. You can use either NO ACTION, RESTRICT, or SET NULL option. The default is RESTRICT.

ON DELETE : Defines the action to be performed when attempting to delete the primary key referenced by the foreign key. You can use NO ACTION, RESTRICT, CASCADE, or SET NULL option. The default is RESTRICT.

ON CACHE OBJECT : You can search an object using a direct object reference in object-oriented model. ON CACHE OBJECT option supports this feature in association with referential integrity (foreign key). ON CACHE OBJECT option adds an OID reference to a foreign key configuration. The OID is used as a CACHE point for the foreign key to the primary key table. Such OID is managed by the system internally; it cannot be changed by users.
To define the ON CACHE OBJECT option, you must have defined a column whose domain is the table with a primary key and specified the column in the cache_object_column_name.
The attribute defined with ON CACHE OBJECT can use the OID the same way as the one of the existing object type.

referential_ action : You can define an option that determines whether to maintain the value of the foreign key when the primary key value is deleted or updated.

CASCADE : If the primary key is deleted, the foreign key is deleted as well. This option is supported only for the ON DELETE operation.

RESTRICT : Prevents the value of the primary key from being deleted or updated, and rolls back any transaction that has been attempted.

SET NULL : When a specific record is being deleted or updated, the column value of the foreign key is updated to NULL.

ERROR: Update/Delete operations are restricted by the foreign key 'fk_id'.

0 command(s) successfully processed.

Caution

In a referential constraint, the name of the primary key table to be referenced and the corresponding column names are defined. If the list of column names are is not specified, the primary key of the primary key table is specified in the defined order.

The number of primary keys in a referential constraint must be identical to that of foreign keys. The same column name cannot be used multiple times for the primary key in the referential constraint.

The actions cascaded by reference constraints do not activate the trigger action.

It is not recommended to use referential_triggered_action in the CUBRID HA environment. In the CUBRID HA environment, the trigger action is not supported. Therefore, if you use referential_triggered_action, the data between the master database and the slave database can be inconsistent. For more information, see CUBRID HA.