
- 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
How do I set the default value for a column in MySQL?
To set the default value, use the DEFAULT keyword.
Let us first create a table −
mysql> create table DemoTable758 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100) ); Query OK, 0 rows affected (0.66 sec)
Following is the query to set default value for a column −
mysql> alter table DemoTable758 add column Colors ENUM('RED','GREEN','BLUE','ORANGE','YELLOW') DEFAULT 'YELLOW'; Query OK, 0 rows affected (0.44 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table once again −
mysql> desc DemoTable758;
This will produce the following output -
+-----------+----------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+----------------------------------------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | FirstName | varchar(100) | YES | | NULL | | | Colors | enum('RED','GREEN','BLUE','ORANGE','YELLOW') | YES | | YELLOW | | +-----------+----------------------------------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
Insert some records in the table using insert command. Here, we haven’t inserted the value for 2nd column with FirstName “John”. The default value “YELLOW” will get placed there −
mysql> insert into DemoTable758(FirstName) values('John'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable758(FirstName,Colors) values('Carol','RED'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable758;
This will produce the following output -
+----+-----------+--------+ | Id | FirstName | Colors | +----+-----------+--------+ | 1 | John | YELLOW | | 2 | Carol | RED | +----+-----------+--------+ 2 rows in set (0.00 sec)
- Related Articles
- Set default value to a JSON type column in MySQL?
- How do I find out the default server character set in MySQL?
- How to set default value for empty row in MySQL?
- How can I set the default value for an HTML element?
- How to modify column default value in MySQL?
- How to set default Field Value in MySQL?
- How to set MySQL default value NONE?
- How to set NOW() as default value for datetime datatype in MySQL?
- How to set default value to NULL in MySQL?
- How do I view the auto_increment value for a table in MySQL?
- How to revert rows to default column value in MySQL?
- Set a specific value for the first three column values in MySQL?
- How do I see what character set a MySQL database / table / column is?
- How do I get the current AUTO_INCREMENT value for a table in MySQL?
- How to use a function for default value in MySQL?

Advertisements