
- 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 to add not null constraint to existing column in MySQL?
To add not null constraint to an existing column in MySQL, we will use the ALTER command. This is a type of validation to restrict the user from entering null values.
Let us see an example. Firstly, we will create a table. The CREATE command is used to create a table.
mysql> create table AddNotNUlldemo - > ( - > name varchar(100) - > ); Query OK, 0 rows affected (0.44 sec)
To insert records.
mysql> insert into AddNotNUlldemo values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into AddNotNUlldemo values('Bob'); Query OK, 1 row affected (0.19 sec)
To display all the records.
mysql> select *from AddNotNUlldemo;
Here is the output.
+------+ | name | +------+ | John | | Bob | +------+ 2 rows in set (0.00 sec)
The following is the syntax to add a constraint to an existing column.
ALTER table yourTableName modify column_name data type constraint;
Let us now implement the above syntax to implement the below query. Here, we are including “not null” constraint.
mysql> ALTER table AddNotNUlldemo modify name varchar(100) not null; Query OK, 0 rows affected (1.14 sec) Records: 0 Duplicates: 0 Warnings: 0
Now, we cannot insert a null value into the table “AddNotNUlldemo” because we have set the constraint above as “not null”. If we try to add a null value, we will get an error. The following is an example.
mysql> INSERT into AddNotNUlldemo values(null); ERROR 1048 (23000): Column 'name' cannot be null
To display all the records.
mysql> SELECT *from AddNotNUlldemo;
Here is the output.
+------+ | name | +------+ | John | | Bob | +------+ 2 rows in set (0.00 sec)
- Related Articles
- How to add NOT NULL constraint to an already created MySQL column?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- Change a MySQL column to have NOT NULL constraint
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How to add a NOT NULL column in MySQL?
- Set NOT NULL attribute to an existing column in MySQL
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- Add a positive integer constraint to an integer column in MySQL?
- How to add subtotal to a table column displaying NULL in MySQL?
- Add to existing value in MySQL column using CONCAT function?
- Adding a new NOT NULL column to an existing table with records
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- What happens when we apply NOT NULL constraint, with ALTER TABLE statement, to a column contains NULL values?
- How to add column to an existing table in PostgreSQL?
