Class CreateForeignKey


public class CreateForeignKey extends MySqlPart

Generates an ALTER TABLE...ADD CONSTRAINT statement to add a foreign key constraint in MySQL.


Sample Generated SQL:

ALTER TABLE products
  ADD CONSTRAINT fk_products_product_groups FOREIGN KEY (product_group_id)
  REFERENCES product_groups
  ON DELETE CASCADE
  ON UPDATE CASCADE
  • Constructor Details

    • CreateForeignKey

      public CreateForeignKey()
    • CreateForeignKey

      public CreateForeignKey(String constraintName, String primaryTableName, String foreignTableName)
      Creates a foreign key named constraintName on foreignTableName that references primaryTableName.
  • Method Details

    • setPretty

      public CreateForeignKey setPretty(boolean pretty)
      Indicates if line breaks and indentations should be added to output DDL (default false).

      Example output (pretty = true):
      ALTER TABLE products
        ADD CONSTRAINT fk_products_product_groups FOREIGN KEY (product_group_id)
        REFERENCES product_groups
        ON DELETE CASCADE
        ON UPDATE CASCADE

      Example output (pretty = false):
      ALTER TABLE products ADD CONSTRAINT fk_products_product_groups FOREIGN KEY (product_group_id) REFERENCES product_groups ON DELETE CASCADE ON UPDATE CASCADE
      Overrides:
      setPretty in class SqlPart
    • collectSqlFragment

      public void collectSqlFragment(CodeWriter writer)
      Description copied from class: SqlPart
      Writes this part's SQL to the writer; used by SqlPart.getSqlFragment() and by enclosing parts.
      Specified by:
      collectSqlFragment in class SqlPart
    • getForeignTableName

      public String getForeignTableName()
      Returns the name of the table that is altered to add the foreign key (the referencing table).
    • setForeignTableName

      public CreateForeignKey setForeignTableName(String foreignTableName)
      Sets the name of the table that is altered to add the foreign key (the referencing table).
    • getPrimaryTableName

      public String getPrimaryTableName()
      Returns the name of the table the foreign key references.
    • setPrimaryTableName

      public CreateForeignKey setPrimaryTableName(String primaryTableName)
      Sets the name of the table the foreign key references.
    • getConstraintName

      public String getConstraintName()
    • setConstraintName

      public CreateForeignKey setConstraintName(String constraintName)
    • getForeignKeyColumnNames

      public List<String> getForeignKeyColumnNames()
    • setForeignKeyColumnNames

      public CreateForeignKey setForeignKeyColumnNames(List<String> foreignKeyColumnNames)
    • setForeignKeyColumnNames

      public CreateForeignKey setForeignKeyColumnNames(String... foreignKeyColumnNames)
    • getPrimaryKeyColumnNames

      public List<String> getPrimaryKeyColumnNames()
    • setPrimaryKeyColumnNames

      public CreateForeignKey setPrimaryKeyColumnNames(List<String> primaryKeyColumnNames)
    • setPrimaryKeyColumnNames

      public CreateForeignKey setPrimaryKeyColumnNames(String... primaryKeyColumnNames)
    • getOnUpdateAction

      public ForeignKeyAction getOnUpdateAction()
      Returns the ON UPDATE action, or null if the clause is omitted (the default).
    • setOnUpdateAction

      public CreateForeignKey setOnUpdateAction(ForeignKeyAction onUpdateAction)
      Sets the ON UPDATE action; null omits the clause (the default).
    • getOnDeleteAction

      public ForeignKeyAction getOnDeleteAction()
      Returns the ON DELETE action, or null if the clause is omitted (the default).
    • setOnDeleteAction

      public CreateForeignKey setOnDeleteAction(ForeignKeyAction onDeleteAction)
      Sets the ON DELETE action; null omits the clause (the default).