
- 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 a column whose value is not null by default in MySQL?
For this, you need to remove the default keyword. The syntax is as follows:
ALTER TABLE yourTableName ADD COLUMN yourColumnName dataType NOT NULL AFTER yourColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table AddingColumnDefaultValueNOTNULL -> ( -> Id int NOT NULL AUTO_INCREMENT, -> FirstName varchar(20), -> LastName varchar(20), -> Age int, -> Address varchar(100), -> Salary int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.58 sec)
Now check the description of table. The query is as follows:
mysql> desc AddingColumnDefaultValueNOTNULL;
The following is the output:
+-----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | FirstName | varchar(20) | YES | | NULL | | | LastName | varchar(20) | YES | | NULL | | | Age | int(11) | YES | | NULL | | | Address | varchar(100) | YES | | NULL | | | Salary | int(11) | YES | | NULL | | +-----------+--------------+------+-----+---------+----------------+ 6 rows in set (0.11 sec)
Here is the query to add a column whose value is NOT NULL by default:
mysql> alter table AddingColumnDefaultValueNOTNULL add column City varchar(20) NOT NULL after Age; Query OK, 0 rows affected (2.17 sec) Records: 0 Duplicates: 0 Warnings: 0
Now you can check the description of the table once again. The table will have one new column which has the name ‘City’, whose default value NOT NULL by default.
The query to check the description of table is as follows:
mysql> desc AddingColumnDefaultValueNOTNULL;
The following is the output:
+-----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | FirstName | varchar(20) | YES | | NULL | | | LastName | varchar(20) | YES | | NULL | | | Age | int(11) | YES | | NULL | | | City | varchar(20) | NO | | NULL | | | Address | varchar(100) | YES | | NULL | | | Salary | int(11) | YES | | NULL | | +-----------+--------------+------+-----+---------+----------------+ 7 rows in set (0.02 sec)
Look at the City field. In NULL column the value is NO. This itself says that we cannot set NULL value to City field.
- Related Articles
- Insert default into not null column if value is null in MySQL?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- Adding a new column with the current time as a default value in MySQL?
- Create a column on my table that allows null but is set by default to empty (not null)?
- Adding new column after a specific column and defining a default in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Is there a default ORDER BY value in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- How to set default value to NULL in MySQL?
- Adding a new NOT NULL column to an existing table with records
- How to add a NOT NULL column in MySQL?
- Empty string in not-null column in MySQL?
- Removing NOT NULL restriction from column in MySQL?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- How to modify column default value in MySQL?

Advertisements