Note
This variable is introduced since OceanBase Database V2.1.
Description
foreign_key_checks specifies whether to enable FOREIGN KEY constraint check for DML or DDL statements.
Applicability
This variable is valid only in the MySQL mode of OceanBase Database.
Privilege requirements
Query variables
Global level
You can execute the
SHOW VARIABLESstatement or query theinformation_schema.GLOBAL_VARIABLESview (MySQL mode) to view the value of a Global system variable in thesystenant or any user tenant.Session level
You can execute the
SHOW VARIABLESstatement or query theinformation_schema.SESSION_VARIABLESview (MySQL mode) to view the value of a Session system variable in thesystenant or any user tenant.
Modify variables
Global level
You can directly modify the value of a Global system variable in the
systenant.A MySQL user tenant must have the
SUPERorALTER SYSTEMprivilege to modify the value of a Global system variable.
Session level
You can directly modify the value of a Session system variable in the
systenant or any user tenant.
Attributes
| Attribute | Description |
|---|---|
| Type | Boolean |
| Default value | 1 |
| Value range | 0: disables FOREIGN KEY constraint check.1: enables FOREIGN KEY constraint check. |
| Applicable scope | |
| Modifiable | Yes. It can be modified using the SET statement. |
Considerations
The foreign key check is controlled by the foreign_key_checks variable, which is enabled by default. It is typically enabled during normal operations to enforce referential integrity.
Disabling foreign_key_checks can have positive impacts on the database in the following scenarios:
Deleting a table referenced by a foreign key constraint. The referenced table can only be deleted after disabling
foreign_key_checks. When a table is deleted, the constraints defined on the table are also removed.Reloading tables in a different order than required by the foreign key relationships.
Speeding up data imports by disabling foreign key checks during data import operations.
Performing
ALTER TABLEoperations on tables with foreign key relationships.
However, disabling foreign_key_checks can have some negative impact in the following scenarios:
Allowing the deletion of databases containing tables with foreign keys referenced by tables outside of the database.
Allowing the deletion of tables with foreign keys referenced by other tables.
Enabling foreign_key_checks does not trigger a scan of table data. This means that the rows added to the table while foreign_key_checks was disabled will not be checked for consistency when foreign_key_checks is enabled again.
Examples
Session level
obclient> SET foreign_key_checks=1;Global level
obclient> SET GLOBAL foreign_key_checks=1;