1

I'm just wondering because MySQL ENUMs are essentially tied to the table, how do they handle JOINs if two tables have the same ENUM? Are they join based on the ENUM value's or the string-key (input)?

For the follow-up see also, How does MariaDB handle ENUM types used in FOREIGN KEY constraints?

2

Two tables can't have the same ENUMs. MySQL assumes all ENUM keys are globally unique..

CREATE TABLE foo ( a ENUM ('A', 'B', 'C'), x int );
CREATE TABLE bar ( a ENUM ('A', 'C', 'D'), y int );
INSERT INTO foo ( a, x ) VALUES ('A',1), ('B',2), ('C',3);
INSERT INTO bar ( a, y ) VALUES ('A',4), ('C',5), ('D',6);

SELECT * FROM foo JOIN bar USING(a);
+------+------+------+
| a    | x    | y    |
+------+------+------+
| A    |    1 |    4 |
| C    |    3 |    5 |
+------+------+------+

So in this context, the value of the ENUM is the string value of the key. PostgreSQL will return the same result, but because it has actual ENUM types the comparisons in the join are interger and it doesn't have to resolve the ENUM to a string,

CREATE TYPE myType AS ENUM ('A', 'B', 'C', 'D');
CREATE TABLE foo ( a myType, x int );
CREATE TABLE bar ( a myType, y int );
INSERT INTO foo ( a, x ) VALUES ('A',1), ('B',2), ('C',3);
INSERT INTO bar ( a, y ) VALUES ('A',4), ('C',5), ('D',6);

SELECT * FROM foo JOIN bar USING (a);
 a | x | y 
---+---+---
 A | 1 | 4
 C | 3 | 5

If foo and bar were different ENUM types as MySQL requires in it's enum "type", you'd have the same problem but PostgreSQL wouldn't allow the join at all, because they're different types. PostgreSQL also won't allow one enum type to reference a different discrete enum type type without manually providing a method to go from one enum to another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.