![]() ![]() In other words, MATCH FULL requires that if any column of a composite key is NULL, then all columns of the key must be NULL.įor examples showing how these key matching algorithms work, see Match composite foreign keys with MATCH SIMPLE and MATCH FULL.ĬockroachDB does not support MATCH PARTIAL. Unacceptable keys have any combination of both null and non-null values. Unacceptable keys do not exist from the point of view of MATCH SIMPLE all composite keys are acceptable. Invalid keys contain one or more null values. Valid keys may not contain any null values. Unacceptable: Keys that cannot be inserted at all (an error is signalled). Invalid: Keys that will not be used for matching (including for any cascading operations). Valid: Keys that can be used for matching foreign key relationships. How it worksįor matching purposes, composite foreign keys can be in one of three states: If you had a composite foreign key constraint and have just upgraded to version 19.1, then check that MATCH SIMPLE works for your schema and consider replacing that foreign key constraint with a MATCH FULL one. You can specify both MATCH FULL and MATCH SIMPLE.Īll composite key matches defined prior to version 19.1 use the MATCH SIMPLE comparison method. To avoid this, you can use a NOT NULL constraint on foreign keys when creating your tables.Ī NOT NULL constraint cannot be added to existing tables.īy default, composite foreign keys are matched using the MATCH SIMPLE algorithm (which is the same default as PostgreSQL). Note that allowing null values in either your foreign key or referenced columns can degrade their referential integrity, since any key with a null value is never checked against the referenced table. The write contains null values for at least one foreign key column (if MATCH SIMPLE is specified).įor more information about composite foreign keys, see the composite foreign key matching section.The write contains null values for all foreign key columns (if MATCH FULL is specified).Multiple-column (composite) foreign keys only accept null values in the following scenarios: Single-column foreign keys accept null values. ![]() ![]() To enable cross-database foreign key references, set the sql.cross_db_fks.enabled cluster setting to true. By default, referenced columns must be in the same database as the referencing foreign key column.In these cases, the FOREIGN KEY constraint and the referenced table's primary key must contain the same number of columns. In the REFERENCES clause, if you specify a table but no columns, CockroachDB references the table's primary key.The order of the columns in the foreign key definition does not need to match the order of the columns in the corresponding UNIQUE or PRIMARY KEY constraint. For example, the clause REFERENCES tbl (C, D) requires tbl to have either the constraint UNIQUE (C, D) or PRIMARY KEY (C, D). This means the REFERENCES clause must use exactly the same columns as a UNIQUE or PRIMARY KEY constraint on the referenced table. Referenced columns must contain only unique sets of values.This applies to whichever column is used as the partitioning column, in case a different name is used via REGIONAL BY ROW AS. This is possible because crdb_region is implicitly included in every index on REGIONAL BY ROW tables as the partitioning key. A foreign key column can reference the crdb_region column in REGIONAL BY ROW tables even if the crdb_region column is not explicitly part of a UNIQUE constraint.For an example, see Add multiple foreign key constraints to a single column. A single column can have multiple foreign key constraints.A foreign key column cannot be a virtual computed column, but it can be a stored computed column.Foreign key columns must use their referenced column's type.To read more about how foreign keys work, see our What is a Foreign Key? (With SQL Examples) blog post. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |