Error creating a Oracle NoSQL table with ENUM fields - oracle-nosql

I have the following error when trying to create this table :
IllegalArgumentException: Error: at (1, 88) Missing closing ')', at line 1:88
Create table TEST (
id String,
fileType ENUM (DOCUMENT,VIDEO),
status ENUM (IN_PROGRESS,PARTIAL_SUCCESS,SUCCEEDED,FAILED,NULL),
primary key (id)
)

I've spent a little time to understand what I was doing wrong, the error message is not clear.
Literal "null" is a reserved word in the query language so it is not supported in that location.
status ENUM (IN_PROGRESS,PARTIAL_SUCCESS,SUCCEEDED,FAILED,NULL),
By the way, We can insert data with null values for the ENUM fields.So, I just create the same table but without including the value NULL
Create table TEST (
id String,
fileType ENUM (DOCUMENT,VIDEO),
status ENUM (IN_PROGRESS,PARTIAL_SUCCESS,SUCCEEDED,FAILED),
primary key (id)
)

Related

How can I decline an INSERT when column is set to NOT NULL

From the documentation, you have to put a NOT NULL modifier in the column definition to mark it as such, just like for other SQL databases.
Consider this table:
CREATE TABLE test (
name String NOT NULL,
isodate DateTime('Europe/Berlin') NOT NULL
) ENGINE = MergeTree()
ORDER BY (isodate)
If I would try to insert NULL for both columns (or at least one), the expected behaviour is that Clickhouse declines insertion since the columns are marked as NOT NULL. Instead, Clickhouse creates a new row, where isodate is 1970-01-01 01:00:00 and name is an empty string, which are the default values for those data types apparently.
What do I have to do so that Clickhouse declines such inserts?
My Clickhouse server version is 21.12.3.
In ClickHouse, NULL and NOT NULL do change the behavior of the data type, but not in the way other relational databases - it is syntactically compatible with other relational database but not semantically (a Int32 NULL is the same as a Nullable(Int32), as a Int32 NOT NULL is the same as a Int32). A column defined as NOT NULL does not mean it will refuse to insert fields whose values are NULL in the insert statement - it means ClickHouse will use the default expression for the column type (or if it is not specified in the column definition, the default value for the data type). This behavior is expected in ClickHouse when input_format_null_as_default is enabled (the default for Clickhouse 21.12.3).
To throw exceptions for such invalid values you need to change the system setting input_format_null_as_default to 0. If you use clickhouse-client, you can disable it while connecting to clickhouse:
clickhouse-client -h ... --input_format_null_as_default 0
or after:
clickhouse> SET input_format_null_as_default=0
This way, a statement like insert into test (name, isodate) values (NULL, NULL); will behave more likely most relational databases.
Clickhouse behaviour with Not Null constraints is not compatible with other databases.
You can overcome it using check constraints https://clickhouse.com/docs/en/sql-reference/statements/create/table/#constraints
CREATE TABLE test (
name String NOT NULL,
isodate DateTime('Europe/Berlin') NOT NULL,
CONSTRAINT isodate_not_null CHECK isodate <> toDateTime(0, 'Europe/Berlin')
) ENGINE = MergeTree()
ORDER BY (isodate)
insert into test(name) values ('x');
DB::Exception: Constraint `isodate_not_null` for table default.test (f589312a-1592-426a-b589-312a1592b26a) is violated at row 1. Expression: (isodate != toDateTime(0)). Column values: isodate = 0. (VIOLATED_CONSTRAINT)
insert into test values ('x', now());
OK.
The reason is performance, in OLAP databases need to ingest data as fast as possible.

Oracle NoSQL - what does mean : Table exists but definitions do not match

I run into this issue:
Caused by: java.lang.IllegalArgumentException:
Error: User error in query:
CREATE TABLE failed for table TEST:
Table exists but definitions do not match
I added 2 new columns in my existing database using console, and added them in table creation statement. It hits the error when I run creating table again.
I checked everything, they all match, (name, type…)
the order of columns matter when you create a table. Did you validate this ? The IF NOT EXISTS in the CREATE TABLE clause checks for the same structure of the table. Please refer the documentation below for more details.https://docs.oracle.com/en/database/other-databases/nosql-database/21.1/sqlreferencefornosql/create-table.html
This inform you that the table that you are trying to create exits but the structure is not the same. It could be useful.
A sample to show the error when order of columns is changed:
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, firstName STRING,
lastName STRING,
PRIMARY KEY (id));
Statement completed successfully
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, firstName STRING,
lastName STRING,
PRIMARY KEY (id));
Statement did not require execution
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, lastName STRING,
firstName STRING,
PRIMARY KEY (id));
Error handling command CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER,
lastName STRING,
firstName STRING,
PRIMARY KEY (id)): Error: User error in query: CREATE TABLE failed for table testing_tbl: Table exists but definitions do not match

Insert records into collection type Oracle

I want to inserted into my collection some SQL records, but I can not. I'm a beginner. Can you help me ?
This is my tables :
CREATE TYPE article_type AS OBJECT (idA CHAR(10), nomA CHAR(10), prixA CHAR(10) )
CREATE TYPE facture_type AS OBJECT (idF CHAR(10), dateFact DATE)
CREATE TYPE ens_collection_fact AS OBJECT (refFact facture_type, refArticle article_type)
CREATE TYPE collection_fact AS TABLE OF ens_collection_fact
CREATE TYPE client_type AS OBJECT (idC NUMBER, nomC CHAR(10),adresse CHAR(10), Compose collection_fact )
CREATE TABLE Article OF article_type
CREATE TABLE Facture OF facture_type
CREATE TABLE Client OF client_type (PRIMARY KEY(idC)) NESTED TABLE Compose STORE AS temp
This is my query that I want to insert, but I have an error from the Oracle : ORA-02315
INSERT INTO ECOLER.CLIENT VALUES
(100, 'Jules Verne', '1', Collection_fact(Ens_collection_fact(reffact('A','2002-12-10'), ens_collection_fact(refarticle('D','E','F'))) ))
Thank in advance
reffact and refarticle are identifiers for objects within other objects, not types; you need to refer to the actual types. You also need to supply both values for each Ens_collection_fact attribute for the default constructor; you can pass null if you only want one or the other:
INSERT INTO CLIENT VALUES
(100, 'Jules Verne', '1',
Collection_fact(
Ens_collection_fact(facture_type('A',date '2002-12-10'), null),
Ens_collection_fact(null, article_type('D','E','F'))
)
)
Also notice that I've added the date keyword so it's providing an actual date literal rather than a string, which would be converted - if you're lucky - with your session NLS settings.
This will still error because 'Jules Verne' is 11 characters and you've defined the name attribute as 10 characters/bytes, but it will work with a shorter string literal.
db<>fiddle

Oracle pushing me ORA-00902: invalid datatype

create table accountDetails(
accountNumber int unique,
customerId int unique,
balance int not null,
password varchar(255) not null,
type varchar(255) not null check(type in ('Savings','Current')),
primary key(accountNumber,customerId) )
create table statusDetails(
customerId int references accountDetails(customerId),
primarykey(customerId))
The last table resulted in an error
The last table resulted in an error
ORA-00902: invalid datatype error happens when we try to define a column using an invalid datatype. It's logical really.
Now, you think you haven't declared a column with an invalid datatype, because you think statusdetails table has only one column, customerid. But if you look at your actual statement you follow that column with this:
primarykey(customerId))
Because you mis-typed primary key Oracle treats that line as an attempt to create a second column. Hence the error, because (customerId) is an invalid datatype. So all you need do is pop in the missing space and Oracle will create the table for you.
create table statusDetails(
customerId int references accountDetails(customerId),
primary key(customerId))
You had a compilation error caused by a simple typo. A key skill for a developer is the ability to turn a cool eye on our own code. I urge you to work on acquiring that skill as soon as you can.
Your second table declaration is all wrong. Try this:
CREATE TABLE statusdetails (
customerid INT,
CONSTRAINT fk_cust FOREIGN KEY ( customerid )
REFERENCES accountdetails ( customerid )
)
Note: using "int" data type maps to a NUMBER(38), which may not be what you want. Use the proper oracle data type names.

error while trying to select from external table

I am getting the below error while trying to select * from ext_poc
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-00554: error encountered while parsing access parameters
KUP-01005: syntax error: found "identifier": expecting one of: "binary_double, binary_float, comma, char, date, defaultif, decimal, double, float, integer, (, no, nullif, oracle_date, oracle_number, position, raw, recnum, ), unsigned, varrawc, varchar, varraw, varcharc, zoned"
KUP-01008: the bad identifier was: varchar2
KUP-01007: at line 4 column 10
29913. 00000 - "error in executing %s callout"
*Cause: The execution of the specified callout caused an error.
*Action: Examine the error messages take appropriate action.
Below is the ddl for the table:
CREATE TABLE "JDASTG"."EXT_POC"
( "ID" varchar2(100),
"NAME" varchar2(100),
"DOB" varchar2(100)
)
ORGANIZATION EXTERNAL
( TYPE ORACLE_LOADER
DEFAULT DIRECTORY "SCPO_EXT_DATA"
ACCESS PARAMETERS
( RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(id varchar2(100),
name varchar2(100),
dob varchar2(100)
)
)
LOCATION
( 'xyz_aldrin.csv'
)
);
PS:
This error however doesn't occur if the varchar2(100) is changed to char(100):
MISSING FIELD VALUES ARE NULL
(id char(100),
name char(100),
dob char(100)
)
It is important to distinguish between oracle internal data types specified on the table, and the external data types specified in the access parameters for the external table (control file for sqlldr). Read the manual again with that in mind. It should become clear. CHAR is allowed in SQL loader whereas VARCHAR2 is not

Resources