Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

column not null deferrable

In Oracle deferred constraints are checked only at the point of commit.

What is the meaning of DEFERRABLE clause in a case of NOT NULL constraint? For example

create table test(a number not null deferrable, b number);
insert into test(a,b) values (222, 111);
commit;

After these statements I thought the following code would work

update test set a = null where b = 111;
delete test where b = 111;
commit;

But it doesn't.

What is the difference between two definitions?

create table test1(a number not null deferrable, b number);
create table test2(a number not null, b number);
like image 554
Oleg Pavliv Avatar asked Jan 14 '11 10:01

Oleg Pavliv


People also ask

What does deferrable mean in SQL?

If a constraint is deferrable, this clause specifies the default time to check the constraint. If the constraint is INITIALLY IMMEDIATE, it is checked after each statement, which is the default. If the constraint is INITIALLY DEFERRED, it is checked only at the end of the transaction.

How do you make a column NOT NULL in Oracle?

Go to the Object Explorer, Tables View, Right click on the table you want to change, Select Alter Table option, Select Columns option in the left panel, Then check the not null checkboxes in the right, then click ok.

What is the function of the not NUL constraint?

The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

Why do you need deferrable option in case of SQL transactions?

To recap, declaring a constraint deferrable allows transactions to defer validation until commit time.


1 Answers

There are two options here. Either you need to set the constraint to be deferred within the transaction by using the command shown below

SET CONSTRAINTS ALL DEFERRED;

This should be run before doing the UPDATE statement that you have defined.

Alternatively you can set the constraint to be INITIALLY DEFERRED in the table definition

create table test(a number not null initially deferred deferrable, b number);

After doing either of these things, you should then be able to run the DML that you have in the question.

like image 185
Mike Meyers Avatar answered Sep 23 '22 02:09

Mike Meyers