SQL Server Equivalent to MySQL ENUM Data Type



No, SQL Server doesn't offer a direct equivalent to MySQL's ENUM data type, but you can replicate similar functionality with a combination of VARCHAR, and a CHECK constraint. This allows you to restrict the possible values for a column, just like ENUM does in MySQL.

This article will look at how SQL Server can handle ENUM-like behavior and examples of how it is being utilized in your database design.

Enum in MySQL

In MySQL, the ENUM is a data type that is used to create a column with a fixed set of predefined values which allows data integrity. It is helpful for the fields that have limited alternatives, such as categories, statuses, and color codes. Additionally, you can also set a default value for the column, which will apply when the record is being created and no value is specified.

Syntax

Following is the syntax to create an EMUM column in MYSQL.

CREATE TABLE yourTableName (
   volumn_name enum(?Value1',Value2',Value3',......N)
   default Value1' or Value2 or Value3,..N
);

Example

In the following query, you can see how to define an ENUM for a column that stores the type of operation (such as CREATE, READ, UPDATE, DELETE).

CREATE TABLE EnumInMySQL (
   WebCRUD enum('CREATE','READ','UPDATE','DELETE')
   default 'CREATE'
);

Now, let's insert some sample data into the 'EnumInMySQL' table. We'll specifically insert pre-defined values for the WebCRUD column from the ENUM list. The query will be as follows -

INSERT INTO EnumInMySQL (WebCRUD)
VALUES ('CREATE'), ('READ'), ('UPDATE'), ('DELETE');

Inserting a record without specifying 'WebCRUD' values will give us a default value in our case it is 'CREATE'.

INSERT INTO EnumInMySQL (WebCRUD) VALUES (DEFAULT);

After inserting data into the table, you can query it to see the results using SELECT command.

SELECT * FROM EnumInMySQL;

From the below output, we can confirm that the first four rows contain the explicitly inserted values. The last row has the default value CREATE.

WebCRUD
CREATE
READ
UPDATE
DELETE
CREATE

ENUM Like Behaviour in SQL Server

Even though SQL Server does not directly support the ENUM in MySQL, you can still use a CHECK constraint on a VARCHAR or CHAR column to achieve something like ENUM in MySQL. This is very useful in limiting the values that are entered into a column, just like using ENUM in MySQL.

Syntax

Below is the syntax to create a table with ENUM-like behavior in an SQL server using the CHECK constraint.

CREATE TABLE yourTableName (
    Column1 DataType,
    Column2 DataType,
    Column3 DataType,
    CONSTRAINT ConstraintName CHECK (Column3 IN ('Value1', 'Value2', 'Value3'))
);

Example

Here is how to create a table to restrict a column to specific values like Active, Inactive, Suspended:

CREATE TABLE Employees (
    ID INT PRIMARY KEY,
    Name VARCHAR(100),
    Status VARCHAR(10),
    CONSTRAINT chk_status CHECK (Status IN ('Active', 'Inactive', 'Suspended'))
);
Inserting data

Now let's populate the table with sample data which is explicitly defined while creating the table.

INSERT INTO Employees (ID, Name, Status)
VALUES (1, 'John Doe', 'Active'),
       (2, 'Jane Smith', 'Inactive'),
	   (3, 'Alice Johnson', 'Suspended') ;

After inserting data into the table, you can query it to see the results.

SELECT * FROM Employees;

Following is the output of the above code ?

ID Name Status
1 John Doe Active
2 Jane Smith Inactive
3 Alice Johnson Suspended
Inserting invalid data

Let us check what happens if we attempt to insert an invalid value, which is other than the values explicitly defined.

INSERT INTO Employees (ID, Name, Status)
VALUES (4, 'Bob Brown', 'Retired');

Following is the output of the above code ?

ERROR 3819 (HY000): Check constraint 'chk_status' is violated.

From the output, you can confirm that only valid values defined in the check constraint are allowed.

Conclusion

In summary, MySQL has a built-in ENUM data type that limits columns to a set list of values. SQL Server doesn't have a direct match for this. But SQL Server lets you copy this behavior by using CHECK constraints on VARCHAR or CHAR columns. This method helps keep data clean by controlling what values can go in a column. It makes sure valid entries get into the database.

Updated on: 2025-01-22T18:08:33+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements