
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Adding new enum column to an existing MySQL table?
To add a new enum column to an existing MySQL table, you can use ALTER command. Following is the syntax:
ALTER TABLE yourTableName ADD yourColumnName ENUM('yourValue1','yourValue2’....N) NOT NULL;
Let us first create a table:
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(200), StudentAge int ); Query OK, 0 rows affected (0.62 sec)
Check the description of table using DESC command:
mysql> DESC DemoTable;
This will produce the following output:
+-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(200) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
Following is the query to add a new enum column to existing table. We have set it for Student Gender:
mysql> ALTER TABLE DemoTable ADD StudentGender ENUM('Male','Female') NOT NULL; Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table once again:
mysql> desc DemoTable;
This will produce the following output and display the enum values as well for GENDER:
+---------------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-----------------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(200) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentGender | enum('Male','Female') | NO | | NULL | | +---------------+-----------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
Look at the above sample output, the column StudentGender has data type ENUM.
- Related Articles
- Adding a new NOT NULL column to an existing table with records
- Adding new column to existing DataFrame in Pandas
- Adding a new column to an existing DataFrame in Python Pandas
- Inserting data into a new column of an already existing table in MySQL?
- Adding a new column to existing DataFrame in Pandas in Python
- Adding characters in values for an existing int column in MySQL?
- How to rename a column in an existing MySQL table?
- How to add a new column to an existing table using JDBC API?
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- How to add a new column to an existing table of Laravel in a migration?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How can I drop an existing column from a MySQL table?
- How to add column to an existing table in PostgreSQL?
- Add a new value to a column of data type enum in MySQL?
- How to add columns to an existing MySQL table?

Advertisements