Search code examples
mysqlsqlforeign-keystablecolumn

How to name SQL FOREIGN KEY Constraint on CREATE two tables columns into a relationship TABLE


i was just wondering if there is a way to name a foreign key on create table

FOREIGN KEY (uuid) REFERENCES employee(uuid));

i tried to do this and name my foreign key as fk1 but i've got syntax errors

FOREIGN KEY fk1 (uuid) REFERENCES employee(uuid));

error

org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is com.jda.cloudsql.common.MigrationException: org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException:

  FOREIGN KEY FK1[*] (UUID) REFERENCES EMPLOYEE(UUID)) "; expected "("; SQL statement:

Solution

  • You can write:

    ADD CONSTRAINT `customized_foreign_key_name` FOREIGN KEY (`uuid`) REFERENCES `employee` (`uuid`)