SQL - SPLessons

SQL Not NULL

Chapter 26

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Not NULL

SQL Not NULL

shape Description

SQL Databases are huge gathering of logical related information which can be created, accessed, managed, and updated. The data can be in any form like text and numbers. And by defining a column with SQL Not NULL constraint then that constraint column won’t acknowledge null values. The constraint NOT NULL implements a field to dependably contain a quality. This implies that another record cannot be embedded, or overhaul a record without increasing the value of this field.

shape Syntax

create table<table_name>(<column_name1>datatype(size)constraint constraint name constraint_type, <column_name2>datatype(size));
Table_name => Any accurate table.

Column_name =>The operation that can be perform on a column in the table.

constraint_type =>constraint type will specify the type of constraint, that is going to be executed in the database.

shape Examples

By viewing the below example, the concept of not null constraint can be easily understood.

case-1:

sql> create table student(stu_id number(4)constraint Stud12_stu_id_nn not null,stu_name varchar(255)not null,fee (7,2));
Query OK, 0 rows affected (0.41 sec)

sql>insert into student values(1001,'jack',12000);
Query OK, 1 row affected (0.10 sec)

sql> insert into student values(null,'jack',12000);
ERROR 1048 (23000): Column 'stu_id' cannot be null

case-2:

sql> create table student(stu_id number(4),stu_name varchar(255)constraint Studtent_stu_name_nn not null,fee (7,2));
Query OK, 0 rows affected (0.41 sec)

sql>insert into student values(10,'mike',15000);
Query OK, 1 row affected (0.10 sec)

sql> insert into student values(11,'null',16000);
ERROR 1048 (23000): Column 'stu_name' cannot be null

In the above two example if not null constraint is applied on a column_name stud_id and stu_name then that column will accept the values other than null values.

Summary

shape Key Points

  • SQL Not NULL Constraint – Will not accept Null values.