SQL not NULL(no default value) but allowed to insert NULL value - sql

I have this table nonulls col1 varchar NULL col2 numeric (24,6) NOT NULL.
How can I insert value like this,
INSERT INTO nonulls (col1) SELECT 'ARF' as col1
Without having value for col2, some existing command in some of SSIS package here have that kind like of query but the col2 is not null but it still proceeds inserting value. Without using "coalesce" and without changing NOT NULL to "allow null".

If col2 has a default value, then you can insert a row with no value. It would be declared something like:
col2 <type> not null default <default value>
The default default value is NULL, which a not null declaration would preclude.
It is also possible that the table has an insert trigger defined on it. The trigger could assign a value to the column.

Related

Force COALESE(NULL,NULL) to return NULL

In SQL SERVER
Considering the fact that:
Col1 and Col2 contain numeric and NULL values
SELECT
COALESCE(Col1,Col2)
Return an error: "At least one of the arguments to COALESCE must be an expression that is not the NULL constant."
Considering that Col1 and Col2 are NULL,I want to force it to return NULL value in this case.
The workaround seems to be unelegant/inefficient for me:
SELECT
NULLIF(COALESCE(Col1 ,Col2 ,''),'')
Note that Col1 and Col2 are numeric fields and cannot take '' as a value.
Any other suggestion ?
Thank you for your help
This code works:
SELECT COALESCE(Col1, Col2)
FROM . . . -- references here that define COL1 and COL2
If both columns are NULL, it will return NULL (typed as an integer in your case).
The only time you get the error you mention is when the columns are explicitly NULL. Even calculations seem to get around this:
select coalesce(null + 3, null)
--> NULL rather than an error
The following even returns NULL rather than an error:
declare #col1 int;
declare #col2 int;
select coalesce(#col1, #col2);
Here is a db<>fiddle.

How to set default values properly in SQL?

I have a table which should not have any NULL values at all. When I set a NOT NULL constraint, it disallows the statement and it fails with the constraint error. Default constraint will take place only if the column is not referenced in the insert statement.
How can we get around this? If a insert statement has a NULL value for any of the columns, then the DEFAULT value must be taken instead of the NULL values.
create table temp1 (col0 int, col1 int default 0);
insert into temp1 (col0) values (1); --> col0 -> 1 and col1 ->0
insert into temp1 (col0,col1) values (1,NULL); --> col0 -> 1 and col1 -> NULL (I would expect a 0 here instead of NULL)
alter table temp1 (add column col2 int not null default 0); --> col2 -> 0 for all the existing rows
insert into temp1 (col0) values (2); --> col0 -> 2 and col1 ->0 and col2-> 0
select * from temp1;
COL0 |COL1 |COL2
1 |0 |0
1 |(null) |0
2 |0 |0
Converting NULL into a column's default value for an insert is not part of standard SQL.
As you observed, you can omit the column from the insert statement but that is not the same as inserting a NULL value. Rather, in effect, the default value for a column's DEFAULT is NULL (SQL92 13.8 General Rules 4b); this is why inserting default values gives NULLs if there is no explicit default defined.
You can alternatively include the column and use the keyword DEFAULT (SQL92 7.1 General Rules 2). WX2 doesn't currently support this syntax but Kognitio plans to add it in the upcoming version 8.2.
insert into temp1 (col0, col1) values (1, DEFAULT);
The standard only allows you to use DEFAULT as shown above and not in a compound expression or in an insert-select statement.
-- NOT VALID IN STANDARD SQL!
insert into temp1 (col0, col1) values (1, case when ... then 1 else DEFAULT end);
-- NOT VALID IN STANDARD SQL!
insert into temp1 (col0, col1) select C1, DEFAULT from ...;
You can solve this using a COALESCE() function.
insert into temp1 (col0, col1) select E1, COALESCE(E2, 0) from ...;
Other databases don't in general allow converting NULLs to default values either: see similar questions for SQL Server, MySQL, Postgres and
Firebird. Oracle does have a non-standard syntax to create table columns with DEFAULT ON NULL which would do what you want.
(Kognitio might add DEFAULT in compound expressions or DEFAULT ON NULL as extensions in a future release.)

Oracle: Insert into table(column_list) values(values_list) with some columns defined as default NULL

Suppose you have this Oracle 11g table:
table sample(
mykey int, --primary key
myval varchar2(10 byte) not null,
col1 varchar2(10 byte),
col2 varchar2(10 byte) default NULL)
If you execute this statement from sqldeveloper for example:
insert into sample(mykey, myval) values (1, 'test');
You get
mykey myval col1 col2
1 test null null
Note that it doesn't make a difference for the two nullable columns to specify the 'default null' or not, you'll still have 'null' entered as value for you.
Now, why does the exact same insert from JDBC driver fail with the 'Not enough values' error?
Furthermore, if the column col2 is defined without 'default null', but is still nullable, the JDBC insert works and sets 'null' for both col1 and col2.
Is it just superfluous or plain wrong to specify 'default null' for a column? If Oracle allows me to create the column with that default value and allows me to write SQL code that inserts as in the example correctly, I don't think it's wrong/illegal to do so.
Is this something related to the Oracle DB (or any DB for that matter) or could the application inserting via JDBC driver be issuing a wrong statement?
Thanks in advance
Is it just superfluous or plain wrong to specify 'default null' for a column?
Yes, it superfluous. DEFAULT NULL is implied if you don't specify a default value.
After all that's the only sensible behavior: if you don't provide a value, the only possible choice is to store "unknown" in that column. And that's precisely what NULL means: it is unknown whether there is a value or not.
Now, why does the exact same insert from JDBC driver fail with the 'Not enough values' error?
I don't believe that you are using the exact same statement in your Java program. The "not enough values" error only appears if you - well - don't supply enough values. E.g. by providing less values in the values clause than you have columns in the insert part. The following statement would cause that error:
insert into sample(mykey, myval, col1) values (1, 'test');
I haven't tried, but it could be that something like the following would also cause this error:
PreparedStatement pstmt = connection.prepareStatement("insert into sample(mykey, myval, col1, col2) values (?,?,?,?)");
pstmt.setInt(1, 1),
pstmt.setString(2, 'test');
Note that setXXX() was not called for the third column.
I believe that the JDBC would create an SQL similar to the following:
insert into sample(mykey, myval, col1, col2 ) values (?, ?, ?, ?);
Just to say, i am not into JAVA so the parameters may be written differently.
Having said that, your client expects 4 values and not just 2. It wouldn't know that you may want to only pass a certain set of columns.

How to input the value 'NULL' into nvarchar column in SQL Server?

I have a requirement to insert the literal value 'NULL' into a nvarchar column as a default value. But when I insert records the default is going as db NULL instead of the literal 'NULL'. Can someone tell me where I am going wrong?
So you want the actual string of "NULL" to be inserted? Try something like this:
create table NullTable
(
nvarchar(100) not null default 'NULL',
.... -- your other fields
)
EDIT: Here is a full solution
create table InsertNull
(
Nullfield nvarchar(100) not null default 'NULL',
someint int not null
)
go
insert into insertnull(someint)
select 20
select *
from InsertNull
Note the quotes:
INSERT INTO yourtable (varcharfield) VALUES ('NULL'); // string null
INSERT INTO yourtable (varcharfield) VALUES (NULL); // actual null
If set the default in code instead of the designer is should work.
ALTER TABLE [myTable] ADD DEFAULT ('NULL') FOR [TextColumn]

SQL Server 2000 - Default Value for varchar fields

I have a sql server nvarchar field with a "Default Value or Binding" of empty string. It also happens to be a not null field.
Does this mean that there is no default or that it is a default of a string with no characters in it.
If I don't insert a value, will it insert with an empty string or fail with a "not null" error?
The default is a blank (empty) string.
If you don't provide a value, the insert will be successful and the value will be blank, not null.
Its the same as (assuming data is the col in question):
create table #t (id int, data varchar(100) not null default(''))
So:
insert into #t (id) values (1)
insert into #t (id,data) values (2,default)
insert into #t (id,data) values (3, 'allowed')
select * from #t
will return
1
2
3 allowed
and ..
insert into #t (id,data) values (1, null)
-- will error
If you have a true empty string as a default, then it will autopopulate with a 0 length string.
You should be careful to ensure it is a 0 length string and not nothing though. If for instance you are looking in the table builder gui for SSMS and it shows a blank for "Default Value or Binding", that means that there is no default value and an insert will fail if it is not populated. If you want it to have a 0 length string, populate it with '' (two single-quotes together with nothing in between.)
Default value for a column is just that - sql server will put that value when you dont supply one for the column. The value in the column will be an empty string. Not null error will not happen