Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Advantages of an ENUM vs. a one-to-many relationship?

I very rarely see ENUM datatypes used in the wild; a developer almost always just uses a secondary table that looks like this:

CREATE TABLE officer_ranks ( id int PRIMARY KEY ,title varchar NOT NULL UNIQUE); INSERT INTO officer_ranks VALUES (1,'2LT'),(2,'1LT'),(3,'CPT'),(4,'MAJ'),(5,'LTC'),(6,'COL'),(7,'BG'),(8,'MG'),(9,'LTG'),(10,'GEN');  CREATE TABLE officers ( solider_name varchar NOT NULL ,rank int NOT NULL REFERENCES officer_ranks(id) ON DELETE RESTRICT ,serial_num varchar PRIMARY KEY); 

But the same thing can also be shown using a user-defined type / ENUM:

CREATE TYPE officer_rank AS ENUM ('2LT', '1LT','CPT','MAJ','LTC','COL','BG','MG','LTG','GEN');      CREATE TABLE officers ( solider_name varchar NOT NULL ,rank officer_rank NOT NULL ,serial_num varchar PRIMARY KEY); 

(Example shown using PostgreSQL, but other RDBMS's have similar syntax)

The biggest disadvantage I see to using an ENUM is that it's more difficult to update from within an application. And it might also confuse an inexperienced developer who's used to using a SQL DB simply as a bit bucket.

Assuming that the information is mostly static (weekday names, month names, US Army ranks, etc) is there any advantage to using a ENUM?

like image 280
jamieb Avatar asked Nov 27 '10 19:11

jamieb


People also ask

Why enum is used in SQL?

The ENUM data type in MySQL is a string object. It allows us to limit the value chosen from a list of permitted values in the column specification at the time of table creation. It is short for enumeration, which means that each column may have one of the specified possible values.

What are the advantages of using an enum over an INT?

The advantages of using enums are that they are very easy to use and represented as strings but processed as integers. Enums are easy to maintain and improve code readability because they provide symbolic named constants, which means you need to remember the names, not the integer values.

Why are enums better?

Advantages of enum: enum improves type safety at compile-time checking to avoid errors at run-time. enum can be easily used in switch. enum can be traversed. enum can have fields, constructors and methods.

Should I use enum in database?

Enum types are more convenient for coding. For infrequent releases, or if you often have new/deleted/changed values, use a database table. For static sets of values, or if you release code all the time, use an enum.


2 Answers

Example shown using PostgreSQL, but other RDBMS's have similar syntax

That's incorrect. It is not an ISO/IEC/ANSI SQL requirement, so the commercial databases do not provide it (you are supposed to provide Lookup tables). The small end of town implement various "extras", but do not implement the stricter requirements, or the grunt, of the big end of town.

We do not have ENUMs as part of a DataType either, that is absurd.

The first disadvantage of ENUMs is that is it non-standard and therefore not portable.

The second big disadvantage of ENUMs is, that the database is Closed. The hundreds of Report Tools that can be used on a database (independent of the app), cannot find them, and therefore cannot project the names/meanings. If you had a normal Standard SQL Lookup table, that problem is eliminated.

The third is, when you change the values, you have to change DDL. In a Normal Standard SQL database, you simply Insert/Update/Delete a row in the Lookup table.

Last, you cannot easily get a list of the content of the ENUM; you can with a Lookup table. More important, you have a vector to perform any Dimension-Fact queries with, eliminating the need for selecting from the large Fact table and GROUP BY.

like image 168
PerformanceDBA Avatar answered Sep 16 '22 17:09

PerformanceDBA


I don't see any advantage in using ENUMS.

They are harder to maintain and don't offer anything that a regular lookup table with proper foreign keys wouldn't allow you to do.

like image 38
a_horse_with_no_name Avatar answered Sep 19 '22 17:09

a_horse_with_no_name