149

I need to convert a table from MySQL to SQLite, but I can't figure out how to convert an enum field, because I can't find ENUM type in SQLite.

The aforementioned field is pType in the following table:

CREATE TABLE `prices` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `pName` VARCHAR(100) NOT NULL DEFAULT '',
    `pType` ENUM('M','R','H') NOT NULL DEFAULT 'M',
    `pField` VARCHAR(50) NULL DEFAULT NULL,
    `pFieldExt` VARCHAR(50) NULL DEFAULT NULL,
    `cmp_id` INT(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT

I need a field with only three values for the user to chose, and I would like to enforce that in the DB, not just in my application.

3 Answers 3

184

SQLite way is to use a CHECK constraint.

Some examples:

CREATE TABLE prices (
 id         INTEGER                                PRIMARY KEY,
 pName      TEXT CHECK( LENGTH(pName) <= 100 )     NOT NULL DEFAULT '',
 pType      TEXT CHECK( pType IN ('M','R','H') )   NOT NULL DEFAULT 'M',
 pField     TEXT CHECK( LENGTH(pField) <= 50 )     NULL DEFAULT NULL,
 pFieldExt  TEXT CHECK( LENGTH(pFieldExt) <= 50 )  NULL DEFAULT NULL,
 cmp_id     INTEGER                                NOT NULL DEFAULT '0'
)

This will limit the pType column to just the values M, R, and H, just like enum("M", "R", "H") would do in some other SQL engines.

2
  • 8
    Welp, actually this implementation does not fully emulate enum because it makes it impossible to sort by the integer index of the values (which is possible with an actual enum field). Just, everyone, keep that in mind. Dec 12, 2019 at 15:07
  • 2
    The storage overhead should be considered a caveat as well. While constraints can be used to insure data integrity, and while an index will make queries fast enough, you are still storing strings - if used extensively, this requires several times more storage than proper enums, depending on string lengths. You might need to consider using additional lookup tables and foreign keys to work around this issue. May 21, 2022 at 8:12
111

There is no enum type in SQLite, only the following:

  • NULL
  • INTEGER
  • REAL
  • TEXT
  • BLOB

Source: http://www.sqlite.org/datatype3.html

I'm afraid a small, custom enum table will be required in your case.

8
  • 37
    Actually "a custom enum table" is a much cleaner design that using real enums
    – user330315
    Mar 14, 2011 at 15:55
  • 28
    Why not use CHECK() constraint to allow only three possible strings?
    – mateusza
    Jun 19, 2013 at 23:31
  • 4
    @Wideshanks I don't think CHECK() existed at the time I wrote that answer. Unless the string is really short, I'm totally against it. 1 or 2 characters maximum.
    – MPelletier
    May 9, 2014 at 21:34
  • I can use VARCHAR in my SQLite version, is that a new type addition? Jun 20, 2015 at 21:13
  • 4
    @HammanSamuel It's not new, it's resolved as meaning TEXT. On the source page I gave, see point 2.1 on Determination of Column Affinity.
    – MPelletier
    Jun 21, 2015 at 5:59
71

To expand on MPelletier’s answer, you can create the tables like so:

CREATE TABLE Price (
  PriceId INTEGER       PRIMARY KEY AUTOINCREMENT NOT NULL,
  Name    VARCHAR(100)  NOT NULL,
  Type    CHAR(1)       NOT NULL DEFAULT ('M') REFERENCES PriceType(Type)
);

CREATE TABLE PriceType (
  Type    CHAR(1)       PRIMARY KEY NOT NULL,
  Seq     INTEGER
);
INSERT INTO PriceType(Type, Seq) VALUES ('M',1);
INSERT INTO PriceType(Type, Seq) VALUES ('R',2);
INSERT INTO PriceType(Type, Seq) VALUES ('H',3);

Now the enumeration values are available directly in the Price table as they would be using an ENUM: you don’t need to join to the PriceType table to get the Type values, you only need to use it if you want to determine the sequence of the ENUMs.

Foreign key constraints were introduced in SQLite version 3.6.19.

7
  • 3
    INSERT INTO PriceType(Type, Seq) VALUES ('M',1), ('R',2), ('H',3); Should get you a syntax error. "The first form (with the "VALUES" keyword) creates a single new row in an existing table.": sqlite.org/lang_insert.html. Break it up avoid that: INSERT INTO PriceType(Type, Seq) VALUES ('M',1); INSERT INTO PriceType(Type, Seq) VALUES ('R',2); INSERT INTO PriceType(Type, Seq) VALUES ('H',3);
    – ahcox
    Jun 5, 2012 at 20:55
  • 13
    Don't forget to PRAGMA foreign_keys = ON; for every session - because fkeys are disabled by default in sqlite3
    – smathy
    Apr 3, 2013 at 4:38
  • 4
    If you wnat to avoid forgetting PRAGMA foreign_keys = ON; then you can set that up in your .sqliterc file in your home directory. May 5, 2013 at 20:46
  • 4
    Why would you create a separate 'Seq' column instead of just using the default rowid column? Mar 15, 2015 at 23:03
  • 4
    The main purpose of the Seq column will be for sorting, I suppose. Therefore I think it will be better to use 10, 20, 30... instead of 1, 2, 3... to be able to insert a new enum later which should appear e.g. between 20 and 30 in sort order.
    – gunnar247
    Sep 8, 2021 at 22:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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