MySQL and PostgreSQL – Creating Unique Constraints with Different Names

MySQLpostgresqlsql-standard

In MySQL 5.7

  1. alter table mydb.roles add constraint uk1 unique (role_name);

  2. alter table mydb.roles add constraint uk2 unique (role_name);

The 2nd alter command threw me a warning stating:

Duplicate index 'uk2' defined on the table 'world_x.roles'. This is
deprecated and will be disallowed in a future release.

When I tried the same in Postgres(10.x), it didn't warn me.

So, what would the SQL standard be in this scenario?

Best Answer

It can be result very confusing mixing names constraints and slow down your queries because they have the same name

maybe this results helpful

CREATE ONLY ONE CONSTRAINT INDEX AND INCLUDE THOSE NECESSARY COLUMNS

Example

ALTER TABLE users ADD CONSTRAINT findUser UNIQUE (nameUser,emailUser);

No double constraint and no warning message and you keep the SQL rules alive

Remember SQL is an standard but not implemented at the same time for everyone with the same rules

As you can see at the following link, in fact these scenario is a bug

https://bugs.mysql.com/bug.php?id=37520

and finally recognized in: https://bugs.mysql.com/bug.php?id=8565

and finally fixed in MySQL Server 5.6.7