Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
Is it required to specify a Constraint Name when adding a Foreign Key in MySQL?

So far I've created the tables manually like this and I didn't have any issues.

  CREATE TABLE users (
    id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    email VARCHAR(100) NOT NULL,
    cityId BIGINT(20) UNSIGNED NULL DEFAULT NULL,
  PRIMARY KEY (id),
    FOREIGN KEY (cityId)
    REFERENCES cities (id));

Now, I'm using MySQL Workbench that generates the SQL Script for me, and the SQL script looks a bit different.

  CREATE TABLE users (
    id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    email VARCHAR(100) NOT NULL,
    cityId BIGINT(20) UNSIGNED NULL DEFAULT NULL,
  PRIMARY KEY (id),
  CONSTRAINT cityId
     FOREIGN KEY (cityId)
     REFERENCES locations (id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

I'm wondering if there is any difference between these two queries.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You are not required to provide a name for a foreign key, and if you don't, an autogenerated name will be assigned to it. Having said that, it's a good practice and will make your maintenance work easier.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda