
- 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
Set NOT NULL attribute to an existing column in MySQL
To set NOT NULL attribute to an existing column, use ALTER TABLE command. Let us first create a table −
mysql> create table DemoTable1949 ( UserId int, UserName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Here is the query to set NOT NULL attribute to an existing column −
mysql> alter table DemoTable1949 modify UserName varchar(20) not null; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table −
mysql> desc DemoTable1949;
This will produce the following output −
+----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | UserId | int(11) | YES | | NULL | | | UserName | varchar(20) | NO | | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1949 values(101,NULL); ERROR 1048 (23000): Column 'UserName' cannot be null mysql> insert into DemoTable1949 values(101,'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1949 values(102,'Bob'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1949;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 101 | Chris | | 102 | Bob | +--------+----------+ 2 rows in set (0.00 sec)
- Related Articles
- How to add not null constraint to existing column in MySQL?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- Adding a new NOT NULL column to an existing table with records
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- Set existing column as Primary Key in MySQL?
- How to add NOT NULL constraint to an already created MySQL column?
- 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?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Set 1 for NOT NULL value in MySQL
- Check for NULL or NOT NULL values in a column in MySQL
- Change a MySQL column to have NOT NULL constraint
- Insert default into not null column if value is null in MySQL?
- SELECT not null column from two columns in MySQL?

Advertisements