Object
org.apache.spark.sql.connector.catalog.constraints.ForeignKey
All Implemented Interfaces:
Constraint

@Evolving public class ForeignKey extends Object
A FOREIGN KEY constraint.

A FOREIGN KEY constraint specifies one or more columns (referencing columns) in a table that refer to corresponding columns (referenced columns) in another table. The referenced columns must form a UNIQUE or PRIMARY KEY constraint in the referenced table. For this constraint to be satisfied, each row in the table must contain values in the referencing columns that exactly match values of a row in the referenced table.

Spark doesn't enforce FOREIGN KEY constraints but leverages them for query optimization. Each constraint is either valid (the existing data is guaranteed to satisfy the constraint), invalid (some records violate the constraint), or unvalidated (the validity is unknown). If the validity is unknown, Spark will check Constraint.rely() to see whether the constraint is believed to be true and can be used for query optimization.

Since:
4.1.0
  • Method Details

    • columns

      public NamedReference[] columns()
      Returns the referencing columns.
    • referencedTable

      public Identifier referencedTable()
      Returns the referenced table.
    • referencedColumns

      public NamedReference[] referencedColumns()
      Returns the referenced columns in the referenced table.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • name

      public String name()
      Description copied from interface: Constraint
      Returns the name of this constraint.
      Specified by:
      name in interface Constraint
    • enforced

      public boolean enforced()
      Description copied from interface: Constraint
      Indicates whether this constraint is actively enforced. If enforced, data modifications that violate the constraint fail with a constraint violation error.
      Specified by:
      enforced in interface Constraint
    • validationStatus

      public Constraint.ValidationStatus validationStatus()
      Description copied from interface: Constraint
      Indicates whether the existing data in the table satisfies this constraint. The constraint can be valid (the data is guaranteed to satisfy the constraint), invalid (some records violate the constraint), or unvalidated (the validity is unknown). The validation status is usually managed by the system and can't be modified by the user.
      Specified by:
      validationStatus in interface Constraint
    • rely

      public boolean rely()
      Description copied from interface: Constraint
      Indicates whether this constraint is assumed to hold true if the validity is unknown. Unlike the validation status, this flag is usually provided by the user as a hint to the system.
      Specified by:
      rely in interface Constraint
    • toDDL

      public String toDDL()
      Description copied from interface: Constraint
      Returns the definition of this constraint in the DDL format.
      Specified by:
      toDDL in interface Constraint
    • toDescription

      public String toDescription()
      Description copied from interface: Constraint
      Returns the constraint description for DESCRIBE TABLE output, excluding the constraint name (shown separately).
      Specified by:
      toDescription in interface Constraint
    • toString

      public String toString()
      Overrides:
      toString in class Object