FOREIGN KEY
Constraints with MariaDB Xpand
This page is part of MariaDB's Documentation.
The parent of this page is: Constraints for MariaDB Xpand
Topics on this page:
Overview
MariaDB Xpand supports FOREIGN KEY
constraints to define referential constraints between Xpand tables:
Referential constraints allow the database to automatically ensure that each row in the child table is associated with a valid row in the parent table
If the row in the parent table changes, Xpand can block the change to protect child rows, or Xpand can propagate the change to the child rows
For easy migrations from InnoDB, Xpand handles foreign keys similar to InnoDB
Compatibility
MariaDB Xpand 5.3
MariaDB Xpand 6.0
MariaDB Xpand 6.1
CREATE TABLE
To create tables with a foreign key constraint, use the CREATE TABLE statement with the CONSTRAINT ... FOREIGN KEY (...) REFERENCES ...
clause:
CREATE TABLE hq_sales.customers (
customer_id BIGINT AUTO_INCREMENT NOT NULL,
customer_name VARCHAR(500) NOT NULL,
customer_email VARCHAR(200),
PRIMARY KEY(customer_id)
);
CREATE TABLE hq_sales.invoices (
invoice_id BIGINT AUTO_INCREMENT NOT NULL,
branch_id INT NOT NULL,
customer_id BIGINT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'),
PRIMARY KEY(invoice_id),
CONSTRAINT fk_invoices_customers
FOREIGN KEY (customer_id)
REFERENCES hq_sales.customers (customer_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT
);
ALTER TABLE
ALTER TABLE .. ADD CONSTRAINT .. FOREIGN KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to add a foreign key using the ADD FOREIGN KEY
clause with an optional constraint name:
ALTER TABLE hq_sales.invoices
ADD CONSTRAINT fk_invoices_customers
FOREIGN KEY (customer_id)
REFERENCES hq_sales.customers (customer_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT;
ALTER TABLE .. DROP FOREIGN KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to drop a foreign key using the DROP FOREIGN KEY
clause:
ALTER TABLE hq_sales.invoices
DROP FOREIGN KEY fk_invoices_customers;
Query Constraint Name
MariaDB Xpand allows the name of a foreign key constraint to be queried using the information_schema.TABLE_CONSTRAINTS
table:
SELECT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = 'hq_sales'
AND TABLE_NAME = 'invoices'
AND CONSTRAINT_TYPE = 'FOREIGN KEY';
+-----------------------+
| CONSTRAINT_NAME |
+-----------------------+
| fk_invoices_customers |
+-----------------------+
Constraint Checks
When a table is a parent table or a child table in a foreign key relationship, Xpand performs a referential constraint check for write operations.
Constraint Checks on a Parent Table
When an Xpand table has a foreign key, it is known as a parent table. Xpand performs referential constraint checks for the following operations on parent tables:
TRUNCATE (if the table has foreign keys, Xpand executes it as a DELETE statement with no
WHERE
clause)
When Xpand performs a referential constraint check, the outcome depends on several factors:
Operation | Result of Constraint Check | Action | Consequence |
---|---|---|---|
Xpand finds corresponding rows in the child table |
|
| |
Xpand finds corresponding rows in the child table |
|
| |
Xpand finds corresponding rows in the child table |
|
| |
Xpand finds corresponding rows in the child table |
|
| |
Xpand does not find any corresponding rows in the child table | N/A |
| |
Xpand finds corresponding rows in the child table |
|
| |
Xpand finds corresponding rows in the child table |
|
| |
Xpand finds corresponding rows in the child table |
|
| |
Xpand finds rows in the child table for the row |
|
| |
Xpand does not find any rows in the child table for the row | N/A |
|
For example, when an operation fails with the ER_ROW_IS_REFERENCED_2
error code, the following error message can occur:
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`hq_sales`.`invoices`, CONSTRAINT `fk_invoices_customers`
FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`))
Constraint Checks on a Child Table
When an Xpand table is referenced by a foreign key, it is known as a child table. Xpand performs referential constraint checks for the following operations on child tables:
When Xpand performs a referential constraint check, the outcome depends on several factors:
Operation | Result of Constraint Check | Consequence |
---|---|---|
New foreign key value is present in parent table | Success | |
New foreign key value is not present in parent table | Fails with | |
New foreign key value is | Success | |
New foreign key value is present in parent table | Success | |
New foreign key value is not present in parent table | Fails with | |
New foreign key value is | Success | |
Table is referenced by a foreign key | Fails with |
For example, when an operation fails with the ER_NO_REFERENCED_ROW_2
error code, the following error message can occur:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`hq_sales`.`invoices`, CONSTRAINT `fk_invoices_customers`
FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`))
Temporarily Disable FOREIGN KEY
Constraint Checks
When performing bulk data loads, dropping a table, or rebuilding a table, it can improve performance to temporarily disable FOREIGN KEY
constraint checks:
If
FOREIGN KEY
constraint checks are temporarily disabled, then rows can be inserted that violate the constraint, so users must proceed with cautionThis feature is most useful when you are loading a data set that is known to be valid
FOREIGN KEY
constraint checks can be temporarily disabled by setting the session value for theforeign_key_checks
system variable toOFF
:SET SESSION foreign_key_checks=OFF;
Special Cases
Xpand 6 does not support foreign keys on columns that have Columnar indexes
Xpand does not support the referential action
SET DEFAULT
Xpand does not allow foreign keys with non-unique parents with referential actions
CASCADE
orSET NULL
:When a foreign key referencing a non-unique parent is created or altered and uses
CASCADE
orSET NULL
, Xpand raises an error message
Xpand does not validate that foreign keys reference columns with the same data type
If dangling foreign key references are created when
foreign_key_checks
is disabled, subsequent inserts will succeed even ifforeign_key_checks
is enabledXpand allows self-referencing foreign keys
Xpand does not require a key length to be specified when a foreign key references a
BLOB
orTEXT
columnWhen adding or dropping a foreign key, the reported number of rows affected does not reflect the actual number of rows