Oracle - Adding NOT NULL constraint to a currently nullable column - sql

I have a handful of numeric fields that are currently nullable. I want to add a NOT NULL constraint to these fields and have the default value now set to 0.
Data gets pushed in to this table on a schedule. There is nothing being pushed into these fields, and they're nullable, so these certain fields are all being set to null. This is causing some trouble when we're trying to do math with these fields.
If I first run updates on these fields to set all of the current nulls to zeros, should I have any issues adding a NOT NULL with default 0 constraint on these currently nullable fields? Is there anything I should look out for?
Thanks
edit:
SET SERVEROUTPUT ON
DECLARE
P_PROJECT_NUM VARCHAR2(200);
P_FISCAL_YEAR VARCHAR2(200);
P_RESP_CENTER VARCHAR2(200);
FIELD1 INTEGER;
FIELD2 INTEGER;
FIELD3 INTEGER;
BEGIN
P_PROJECT_NUM := '123456';
P_FISCAL_YEAR := '2019_2020';
P_RESP_CENTER := '123A';
FIELD1 := 0;
FIELD2 := 0;
FIELD3 := 0;
FMS_EXTRACT.LOAD_TEMPLATE(
P_PROJECT_NUM => P_PROJECT_NUM,
P_FISCAL_YEAR => P_FISCAL_YEAR,
P_RESP_CENTER => P_RESP_CENTER,
FIELD1 => FIELD1,
FIELD2 => FIELD2,
FIELD3 => FIELD3
);
COMMIT;

You can do it like this
Add the default clause to the columns
Update the null values to 0
Add the constraint NOT NULL
My example:
SQL> create table t ( c1 number , c2 number );
Table created.
SQL> insert into t values ( 1 , null ) ;
1 row created.
SQL> insert into t values ( null , 1 ) ;
1 row created.
SQL> select * from t ;
C1 C2
---------- ----------
1
1
SQL> alter table t modify c1 number default 0 ;
Table altered.
SQL> alter table t modify c2 number default 0 ;
Table altered.
SQL> update t set c1 = 0 where c1 is null ;
1 row updated.
SQL> update t set c2 = 0 where c2 is null ;
1 row updated.
SQL> commit ;
Commit complete.
SQL> alter table t modify c1 number not null ;
Table altered.
SQL> alter table t modify c2 number not null ;
Table altered.
SQL> insert into t ( c1 ) values ( 2 ) ;
1 row created.
SQL> select * from t ;
C1 C2
---------- ----------
1 0
0 1
2 0

This is too long for a comment. I don't think you want a NOT NULL constraint. That would result in your inserts failing if any of the NOT NULL columns are NULL.
The simplest solution is to not change the data at all. After all, NULL appears to be a valid value, if that is how the data is coming in. Just use COALESCE() in your logic:
select coalesce(x, 0) + coalesce(y, 0)
You can encompass this logic in a view or in generated columns:
alter table t add x_notnull generated always as (coalesce(x, 0));
If you want to prevent rows coming in with NULL values, then you can filter them out in your data important process. You don't provide details, so I can't make any suggestions.
You can add a default value. For instance:
alter table modify y default 0;
Then subsequent inserts where the value is missing will be replaced by the default value. However, explicit NULL values will not be affected.
Finally, you can add a NOT NULL constraint -- after removing all current NULL values.
Here is a db<>fiddle showing some examples of the above.

Related

SQL ORACLE using %VALUE%

Hi because of technical limitation of a Framework I need another way to do a smiliar query without using single quotes
--> current
Select Json_Value(json, '$.bankReference') From R_O where json like '%12345%';
--> Need a Valid query without single quotes which is doing exactly the same thing, maybe a function or something.
Select Json_Value(json, '$.bankReference') From R_O where json like %12345%;
A similar alternative, but a little bit more dynamic
Demo
SQL> create table t1 ( c1 varchar2(10) ) ;
Table created.
SQL> insert into t1 values ( 'A12345B' );
1 row created.
SQL> insert into t1 values ( 'A12345C' );
1 row created.
SQL> insert into t1 values ( 'A12345D' );
1 row created.
SQL> insert into t1 values ( 'A12399B' );
1 row created.
SQL> insert into t1 values ( 'A13299B' );
1 row created.
SQL> insert into t1 values ( 'A21399B' );
1 row created.
SQL> commit ;
Commit complete.
SQL> select * from t1 ;
C1
----------
A12345B
A12345C
A12345D
A12399B
A13299B
A21399B
6 rows selected.
Now let's create a function that admits two parameters:
The column we want to check
The value we want to apply the % ( I am guessing that is always a number ). If the value contains any string, it won't work.
Function
SQL> create or replace function p_chk_json(p_text varchar2, p_val number)
return integer is
begin
if p_text like '%'||p_val||'%' then
return 1;
else
return 0;
end if;
end;
/
Function created.
Then test it for 12345 or 99
SQL> select * from t1 where p_chk_json(c1 , 12345) = 1;
C1
----------
A12345B
A12345C
A12345D
SQL> select * from t1 where p_chk_json(c1 , 99 ) = 1 ;
C1
----------
A12399B
A13299B
A21399B
In Oracle you can write a function something like that:
create or replace function test1(p_text varchar2)
return integer is
begin
if p_text like '%12345%' then
return 1;
else
return 0;
end if;
end test1;
Your modified SQL statement will be:
Select Json_Value(json, '$.bankReference') From R_O where test1(json) = 1;

Creating a trigger where if one field is null another automatically is

I am trying the create trigger where if one field in my database is null then automatically update another to be null. This is what I have tried:
CREATE TRIGGER t123
BEFORE INSERT ON Customer
REFERENCING NEW ROW AS New
FOR EACH ROW
BEGIN
IF new.f is null
THEN
new.course_year = null;
END IF;
END;
You do not need the REFERENCING, you can simply use :NEW.
Also, outside a WHEN clause, you have to use :NEW and not NEW:
SQL> create table customer ( f varchar2(100), course_year varchar2(100));
Table created.
SQL> CREATE TRIGGER t123
2 BEFORE INSERT OR UPDATE
3 ON Customer
4 FOR EACH ROW
5 BEGIN
6 IF :new.f is null
7 THEN
8 :new.course_year := null;
9 END IF;
10 END;
11 /
Trigger created.
SQL> insert into Customer(f, course_year) values (123, 123);
1 row created.
SQL> update customer set f = null, course_year = 'test';
1 row updated.
SQL> select * from customer;
F COURSE_YEA
---------- ----------
SQL>

Set a value if null inserted in Oracle SQL

I have created a trigger, that will automatically set the first column values as subsequent factorial numbers. However, additionally, I would like to set the second column's value as the value of first incremented by 5, in case a null value is inserted. Here's what I try right now:
create or replace trigger test_tr
before insert on myT
for each row
begin
IF :new.mNumb is null
THEN
UPDATE myT
SET mNumb = :new.tab_id + 5;
END IF;
SELECT fac(test_seq.NEXTVAL)
INTO :new.tab_id
FROM dual;
end;
But clearly I'm missing something, as nothing happens, the inserted null is still empty.
Do not re-update the table in your trigger, update the row you're given directly:
...
IF :new.mNumb is null
THEN
:new.mNumb = :new.tab_id + 5;
END IF;
...
It all works as expected, using Emmanuel's suggestion to remove the update stmt, as far as I can tell. Here's the test case I used:
drop table test;
create table test (col1 number, col2 number);
create trigger test_trg
before insert on test
for each row
begin
IF :new.col2 is null
THEN
:new.col2 := :new.col1 + 5;
END IF;
:new.col1 := dbms_random.value;
end;
/
insert into test values (1, 1);
insert into test values (1, null);
insert into test values (null, null);
commit;
select * from test;
which produces the following output:
COL1 COL2
---------- ----------
.617580128 1
.030570358 6
.555066268
Maybe if you set :new.col1 before dealing with the null col2 scenario, that would work better for you? Doing that produces:
COL1 COL2
---------- ----------
.302670917 1
.024927489 5.02492749
.667568400 5.66756840

Alter table to modify default value of column

I have a requirement where we need to modify a column's default value in database table. The table is already an existing table in database and currently the default value of the column is NULL.
Now if add a new default value to this column, If I am correct it updates all the existing NULLs of the column to new DEfault value. Is there a way to not to do this but still set a new default value on column.
I mean I do not want the existing NULLs to be updated and want them to remain as NULLs.
Any help on this is appreciated.
Thanks
Your belief about what will happen is not correct. Setting a default value for a column will not affect the existing data in the table.
I create a table with a column col2 that has no default value
SQL> create table foo(
2 col1 number primary key,
3 col2 varchar2(10)
4 );
Table created.
SQL> insert into foo( col1 ) values (1);
1 row created.
SQL> insert into foo( col1 ) values (2);
1 row created.
SQL> insert into foo( col1 ) values (3);
1 row created.
SQL> select * from foo;
COL1 COL2
---------- ----------
1
2
3
If I then alter the table to set a default value, nothing about the existing rows will change
SQL> alter table foo
2 modify( col2 varchar2(10) default 'foo' );
Table altered.
SQL> select * from foo;
COL1 COL2
---------- ----------
1
2
3
SQL> insert into foo( col1 ) values (4);
1 row created.
SQL> select * from foo;
COL1 COL2
---------- ----------
1
2
3
4 foo
Even if I subsequently change the default again, there will still be no change to the existing rows
SQL> alter table foo
2 modify( col2 varchar2(10) default 'bar' );
Table altered.
SQL> select * from foo;
COL1 COL2
---------- ----------
1
2
3
4 foo
SQL> insert into foo( col1 ) values (5);
1 row created.
SQL> select * from foo;
COL1 COL2
---------- ----------
1
2
3
4 foo
5 bar
ALTER TABLE *table_name*
MODIFY *column_name* DEFAULT *value*;
worked in Oracle
e.g:
ALTER TABLE MY_TABLE
MODIFY MY_COLUMN DEFAULT 1;
ALTER TABLE {TABLE NAME}
ALTER COLUMN {COLUMN NAME} SET DEFAULT '{DEFAULT VALUES}'
example :
ALTER TABLE RESULT
ALTER COLUMN STATUS SET DEFAULT 'FAIL'
Following Justin's example, the command below works in Postgres:
alter table foo alter column col2 set default 'bar';
ALTER TABLE <table_name> MODIFY <column_name> DEFAULT <defult_value>
EX: ALTER TABLE AAA MODIFY ID DEFAULT AAA_SEQUENCE.nextval
Tested on Oracle Database 12c Enterprise Edition Release 12.2.0.1.0
For Sql Azure the following query works :
ALTER TABLE [TableName] ADD DEFAULT 'DefaultValue' FOR ColumnName
GO

Update with after insert trigger on same table

I have a to write a insert trigger on a tableA. which will perform update with same table but different column. I am getting error while doing this. My trigger is
create or replace trigger trigger_A
after insert on table_A
begin
update table_A set col1=1 where col1 is null;
end;
I have an application will perform col2 alone will be inserted and col1 will be kept null. so my trigger will give value for col1 once the row is inserted. But i am getting error saying "Trigger is failed and invalid" when a row is inserted.
How to do this. TIA.
If you want to assign a simple default value, the easiest way is to declare it on the table, using the DEFAULT clause.
SQL> create table t42
2 ( col1 number default 1 not null
3 , col2 date)
4 /
Table created.
SQL> insert into t42 (col2) values (sysdate)
2 /
1 row created.
SQL> select * from t42
2 /
COL1 COL2
---------- ---------
1 03-AUG-11
SQL>
This works with literals or pseudocolumns such as SYSDATE or USER. If you want to derive a more complicated value with a user-defined function or a sequence, you will need to use
a trigger.
Here is a new version of the table...
SQL> create table t42
2 ( col1 number default 1 not null
3 , col2 date default sysdate
4 , col3 varchar2(30) default user
5 , col4 number )
6 /
Table created.
SQL>
... with a trigger:
SQL> create or replace trigger t42_trg
2 before insert or update
3 on t42
4 for each row
5 begin
6 if :new.col4 is null
7 then
8 :new.col4 := my_seq.nextval;
9 end if;
10 end;
11 /
Trigger created.
SQL> insert into t42 (col1, col2, col3)
2 values (99, sysdate, 'MR KNOX')
3 /
1 row created.
SQL> select * from t42
2 /
COL1 COL2 COL3 COL4
---------- --------- ------------------------------ ----------
99 03-AUG-11 MR KNOX 161
SQL>
Note that although every column on the table is defaultable, I have to populate at least one column to make the SQL valid:
SQL> insert into t42 values ()
2 /
insert into t42 values ()
*
ERROR at line 1:
ORA-00936: missing expression
SQL>
But I can pass in NULL to COL4 to get a completely defaulted record:
SQL> insert into t42 (col4) values (null)
2 /
1 row created.
SQL> select * from t42
2 /
COL1 COL2 COL3 COL4
---------- --------- ------------------------------ ----------
99 03-AUG-11 MR KNOX 161
1 03-AUG-11 APC 162
SQL>
Caveat lector: my trigger uses the new 11g syntax. In previous versions we have to assign the sequence value using a SELECT statement:
select my_seq.nextval
into :new.col4
from dual;
You cannot update a table where the trigger is invoked:
Within a stored function or trigger, it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
Doing so will generate Error 1442:
Error Code: 1442
Can't update table 'MyTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
In short, we are not allowed to update the table in use - but your case is simple, only want to update the field if it's NULL, for this choose BEFORE INSERT ON trigger, this way you can update all the fields of the new/current entry/row (as it has not been entered yet):
DELIMITER //
DROP TRIGGER IF EXISTS trigger_A//
CREATE TRIGGER trigger_A BEFORE INSERT ON table_A
FOR EACH ROW BEGIN
IF NEW.col1 IS NULL THEN
set NEW.col1 = <some-value>;
ENF IF;
END;
//
DELIMITER ;