Class CreateForeignKey
java.lang.Object
com.northconcepts.datapipeline.jdbc.sql.SqlPart
com.northconcepts.datapipeline.sql.mysql.MySqlPart
com.northconcepts.datapipeline.sql.mysql.CreateForeignKey
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 Summary
ConstructorsConstructorDescriptionCreateForeignKey(String constraintName, String primaryTableName, String foreignTableName) Creates a foreign key namedconstraintNameonforeignTableNamethat referencesprimaryTableName. -
Method Summary
Modifier and TypeMethodDescriptionvoidcollectSqlFragment(CodeWriter writer) Writes this part's SQL to the writer; used bySqlPart.getSqlFragment()and by enclosing parts.Returns the name of the table that is altered to add the foreign key (the referencing table).Returns theON DELETEaction, or null if the clause is omitted (the default).Returns theON UPDATEaction, or null if the clause is omitted (the default).Returns the name of the table the foreign key references.setConstraintName(String constraintName) setForeignKeyColumnNames(String... foreignKeyColumnNames) setForeignKeyColumnNames(List<String> foreignKeyColumnNames) setForeignTableName(String foreignTableName) Sets the name of the table that is altered to add the foreign key (the referencing table).setOnDeleteAction(ForeignKeyAction onDeleteAction) Sets theON DELETEaction; null omits the clause (the default).setOnUpdateAction(ForeignKeyAction onUpdateAction) Sets theON UPDATEaction; null omits the clause (the default).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 CASCADEsetPrimaryKeyColumnNames(String... primaryKeyColumnNames) setPrimaryKeyColumnNames(List<String> primaryKeyColumnNames) setPrimaryTableName(String primaryTableName) Sets the name of the table the foreign key references.Methods inherited from class com.northconcepts.datapipeline.sql.mysql.MySqlPart
escapeKeyword, placeBackTicksMethods inherited from class com.northconcepts.datapipeline.jdbc.sql.SqlPart
collectParameterValues, getParameterValues, getSqlFragment, isPretty, printSqlParts, toString
-
Constructor Details
-
CreateForeignKey
public CreateForeignKey() -
CreateForeignKey
Creates a foreign key namedconstraintNameonforeignTableNamethat referencesprimaryTableName.
-
-
Method Details
-
setPretty
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 -
collectSqlFragment
Description copied from class:SqlPartWrites this part's SQL to the writer; used bySqlPart.getSqlFragment()and by enclosing parts.- Specified by:
collectSqlFragmentin classSqlPart
-
getForeignTableName
Returns the name of the table that is altered to add the foreign key (the referencing table). -
setForeignTableName
Sets the name of the table that is altered to add the foreign key (the referencing table). -
getPrimaryTableName
Returns the name of the table the foreign key references. -
setPrimaryTableName
Sets the name of the table the foreign key references. -
getConstraintName
-
setConstraintName
-
getForeignKeyColumnNames
-
setForeignKeyColumnNames
-
setForeignKeyColumnNames
-
getPrimaryKeyColumnNames
-
setPrimaryKeyColumnNames
-
setPrimaryKeyColumnNames
-
getOnUpdateAction
Returns theON UPDATEaction, or null if the clause is omitted (the default). -
setOnUpdateAction
Sets theON UPDATEaction; null omits the clause (the default). -
getOnDeleteAction
Returns theON DELETEaction, or null if the clause is omitted (the default). -
setOnDeleteAction
Sets theON DELETEaction; null omits the clause (the default).
-