What to assign to a MySQL column that must not be empty?

MySQLMySQLi Database

Define with NOT NULL, if a column must not be empty. Let us first create a table with one of the columns as NOT NULL −

mysql> create table DemoTable1895
   (
   Id int NOT NULL,
   FirstName varchar(20),
   LastName varchar(20) NOT NULL
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1895 values(100,'John','Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1895 values(NULL,'Chris','Brown');
ERROR 1048 (23000): Column 'Id' cannot be null
mysql> insert into DemoTable1895 values(102,'Carol',NULL);
ERROR 1048 (23000): Column 'LastName' cannot be null
mysql> insert into DemoTable1895 values(103,NULL,'Miller');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1895;

This will produce the following output −

+-----+-----------+----------+
| Id  | FirstName | LastName |
+-----+-----------+----------+
| 100 | John      |    Smith |
| 103 | NULL      |   Miller |
+-----+-----------+----------+
2 rows in set (0.00 sec)
raja
Published on 27-Dec-2019 06:58:48
Advertisements