Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!