Ads Here

Thursday 15 February 2018

NOT NULL Constraint

As we discussed previously, there are five constraints. Now, we discussed the first one Constraint NOT NULL. Now, in a oracle database we have a table with some columns and datatypes and we can insert values into a specific column of a specific datatype we added. Now, there is a problem that a user can insert any amount of value to a specific field as they want. Now, if we insert any amount of value to a column then it may be not a proper value. So, for not getting any error in inserting values to a column of a table we can set a NOT NULL constraint.
Now, NOT NULL constraint means that the column cannot be null, you have to insert a value to that specific column of the table. So, using this constraint we can ensure that the column must be null for any specific record entered in the table. We can set NOT NULL constraint when we creating a table and we can also set constraint on a already created table.
For create a constraint we can create a object for that, but it is not necessary. The basic syntax for create a NOT NULL constraint is like this -
Syntax:

So, as you can see first we write the create Table statement, after that the table name added, now we add the column name & it's datatype. After this we use the Constraint keyword, then we give a name to the Constraint as we want. It is not necessary but, it will be helpful when we want to disable or stop the Constraint. After giving the name we use the main keyword Not Null for complete the statement. There is another syntax for use the Not Null Constraint -
Syntax:

So, as you can see on the above SQL statement we have directly added the Not Null constraint after the table creation and we have not used any Constraint keyword and not created any constraint object name. So, in this way you can use not Null constraint on your table.
Let's check this with an example. Now, we create a table named Student with ID, Roll and Name columns. Now, we want that the ID and the Name field will not be null for all the records.So, let's try this -
Syntax:

When we execute this SQL statement it will create a table which have three columns ID, Roll & Name.
Now, when you view the table it will show you that the ID and the Name column cannot be Null, which means the user have to to enter some value to this two columns for some specific record.
So, in this way you can use the Not Null constraint in your database tables. In next upcoming post we start the Unique constraint, so, don't forget to share, thank you. 

No comments:

Post a Comment