Hana DB is not allowing nulls in a nullable field - sql

My shema looks like this:
CREATE TABLE newsletter_status(
identificationnumber BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,
bpid varchar(10),
consumer varchar(4) NOT NULL,
source varchar(10),
vkorg varchar(4) NOT NULL,
cryptid varchar(255) NOT NULL,
status varchar(25),
regDat timestamp,
confirmDat timestamp,
updateDat timestamp
);
CREATE TABLE scpnewsletter (version varchar(255));
CREATE INDEX bpid_index ON newsletter_status (bpid);
CREATE INDEX cryptid_index ON newsletter_status (cryptid);
Running locally in a H2-Database this works fine with insertions. Inserting a object which fields consumer,source,vkorg,cryptid and status ARE set, others dont. The Database should generate an identificationnumber. And the H2 does
When run on the customer DEV environment with a HANA DB the insertion fails, saying that:
PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO newsletter_status (identificationnumber,bpid,consumer,source,vkorg,cryptid,status,regDat,confirmDat,updateDat) VALUES (?,?,?,?,?,?,?,?,?,?)]; SQL state [HY000]; error code [287]; SAP DBTech JDBC: [287]: cannot insert NULL or update to NULL: Not nullable "IDENTIFICATIONNUMBER" column; nested exception is com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: [287]: cannot insert NULL or update to NULL: Not nullable "IDENTIFICATIONNUMBER" column
It does not likes that the Identificationnumber is null.
Going further. If I add an Identificationnumber it says the same thing about bpid:
PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO newsletter_status (identificationnumber,bpid,consumer,source,vkorg,cryptid,status,regDat,confirmDat,updateDat) VALUES (?,?,?,?,?,?,?,?,?,?)]; SQL state [HY000]; error code [287]; SAP DBTech JDBC: [287]: cannot insert NULL or update to NULL: Not nullable "BPID" column; nested exception is com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: [287]: cannot insert NULL or update to NULL: Not nullable "BPID" column
bpid can clearly be null in the shema. This ambiguity in consuming field confuses.
If both bpid and Identificationnumber are set, then the is not Problem with the database.
I want to store an object that both of this ids can be null, but also want an unique id for Identificationnumber.
I cant debug on the DEV env from the customer. Any Idea what possibly could go wrong here?

Ok, so your IDENTIFICATIONNUMBER can never ever be NULL.
Based on the DDL provided, it has a NOT NULL constraint and is the single-column primary key, which implicitly makes it NOT NULL.
If H2 allows NULL inserts, that's an H2 bug/incompliant behavior.
Concerning the BPID column it looks like you're still trying to insert the value for IDENTIFICATIONNUMBER even though this value is defined as IDENTITY column. I assume, that by specifying NULL as a value for it, you want to make HANA use the DEFAULT value (the sequence).
If that's correct, then the answer is: it does not work this way.
Also: the error message wrongly named BPID as the problematic field.
The correct way to use DEFAULT values in INSERT statements in HANA is to leave the columns for which the DEFAULT values should be used, out from the column list.
SQL standard also has the option to use the DEFAULT keyword, but that's (currently, HANA 2 SPS 04) not supported.

Related

Cockroachdb varchar column length isn't flexible

I have a spring boot application that connects to Cockroachdb. I have the following script in my flyway using which the table gets created:
CREATE TABLE IF NOT EXISTS sample_table (
name varchar,
groups varchar,
PRIMARY KEY (name));
The application starts fine, but whenever there is a value for the 'groups' column that is greater than 255 length, I get an error :
Caused by: org.postgresql.util.PSQLException: ERROR: value too long for type VARCHAR(255)
In the sql script, I have mentioned the column 'groups' as 'varchar' which should not restrict the length so I am not sure why am I getting this error.
There isn't an implicit default limit on varchar in CockroachDB. This error indicates that the groups column was initialized with the type varchar(255) when the table was created. Running SHOW CREATE TABLE sample_table; should confirm this.
It's possible that something unexpected is going on in the flyway and the table is not being created how you want it to be created.

Postgres GENERATED AS IDENTITY column nullability

I want to create a table, which contains a nullable column having GENERATED BY DEFAULT AS IDENTITY option, therefore I run the following query:
CREATE TABLE my_table (
generated INTEGER NULL GENERATED BY DEFAULT AS IDENTITY,
data TEXT NOT NULL
);
But once I try to insert a row in the table, which generated field is null like this:
INSERT INTO my_table(generated, data) VALUES(NULL, "some data");
I get a null-constraint violation error.
However if I change the order of my_table.generated column properties:
CREATE TABLE my_table (
generated INTEGER GENERATED BY DEFAULT AS IDENTITY NULL,
data TEXT NOT NULL
);
It inserts rows, which generated field is NULL, without any issues.
Is this the expected behavior for the case?
Postgres developers told me this is a bug since identity columns weren't supposed to be nullable (see the patch file under the response).

cannot change null to not null

The column name on table [dbo].[payment_info] must be changed from NULL to NOT NULL. If the table contains data, the ALTER script may not work.
To avoid this issue, you must add values to this column for all rows or mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
CREATE TABLE [dbo].[payment_info]
(
[name] VARCHAR (50) NOT NULL,
[card_no] VARCHAR (50) NULL,
[card_type] VARCHAR (50) NOT NULL,
[tel_no] VARCHAR (50) NULL,
[mob_no] VARCHAR (50) NULL,
[address] VARCHAR (MAX) NULL
);
I cannot change NULLto NOT NULL; when I update it's showing the above warning.
I am using visual studio 2013 asp.net and c#.
If table already exists and is fulfilled with data, you have to update all NULLs in column you want to change on some value which is not NULL. Then ALTER command should work wthout warnings and/or errors.
I am not really sure if I understood your problem correctly, but the warning says it all - you can't switch column to not nullable, if there are nulls already in the column.
You have to update and set some values to empty entries or set DEFAULT value
EDIT:
You should try first:
select *
from [dbo].[payment_info]
where name is null
and check if there are any problems
Right-click on your table in server explorer and click "new query". Type:
ALTER TABLE
table
ALTER COLUMN
column
int NOT NULL;
This error is produced by SSDT. This will happen if you have an existing table and you would like to add a new non-nullable column to it. In order to do so, you must have a default for this new column (the default can be some temporary value).
CREATE TABLE [dbo].[payment_info]
(
[WhateverColumn] VARCHAR (50) NOT NULL DEFAULT 'Foo',
-- and so on
);
If you want to change the default to a more meaningful value, you can write the script to update the table and set the column's value to a more meaningful value in a post deployment. In post deployment you can also now drop the default since it was temporary:
ALTER TABLE WhateverTable
ALTER COLUMN WhateverColumn DROP DEFAULT;
Now your deployment will succeed.
Note: If your column is a foreign-key column, the default has to exist in the parent table even if the value is temporary.

Can not add a column to existing table

I have a table viz. expenses with three columns as under
ExpenseId int NOT NULL,
ExpenseName varchar(50) NOT NULL,
Invalid bit NOT NULL
To add a new column (OldCode char(4) not null), I used design feature for tables in Microsoft SQL Server Management Studio. But I get following error
'Expenses' table
- Unable to modify table. Cannot insert the value NULL into column 'OldCode', table 'TransportSystemMaster.dbo.Tmp_Expenses'; column does not allow nulls. INSERT fails. The statement has been terminated.
Incidentally I have been able to add same column with same specifications to other tables of the same database.
Any help?
Your Table Consist of Existing Records
and you are pushing a new column of type NOT NULL.
so for older records the data have to be something.
try something like this
ALTER TABLE MY_TABLE ADD Column_name INT NULL
GO
UPDATE MY_TABLE <set valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN Column_name INT NOT NULL
GO
Since OldCode is NOT NULL, you should specify a default value for it.
when you have some rows on your table you can't add a column that is not nullable you should provide a default value for it
Alter Table table_name add OldCode int not null DEFAULT(0);
You have to specify values for all the 4 fields of the table, its purely because, while designing the table you set the definition of the columns to be not null. Again you are adding a new column called OldCode and setting to be not null, all ready existing records hasn't got a value. So that is the reason its complains

sql: making a table structure for injections

I want to take the values from this site for the country table in my database.
The problem is that they don't provide the table structure, so I have to create one, but I cannot get it right - my phpMyAdmin keeps displaying an error when I want to inject the data into the table I created below:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMERIC, alpha3, name, officialName) VALUES ('004','AFG','Afghanistan','Afghan' at line 1
--
-- Table structure for table `countrytable`
--
CREATE TABLE IF NOT EXISTS `countrytable` (
`NUMERIC` int(11) NOT NULL,
`alpha3` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`officialName` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I think my table structure is incorrect. How can I fix it? Thanks!
Try all varchar fields to get the data in since all fields are in quotes in the string you have.
NUMERIC is reserved word in mysql
add in back-tick or quote it -> http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
`alpha3` should be a varchar(3) (or larger), not an int(11).