Why do I get the error "unable to encode table key" when altering a table in CockroachDB? - cockroachdb

I'm using CockroachDB version 22.1 and have a table with many columns. When I try to run ALTER TABLE ... DROP COLUMN I get the error ERROR: unable to encode table key: <nil>. How can I fix this?

This is a bug that is fixed in patch 22.1.7, so the easiest way to fix this is to upgrade to that version or higher. It manifests when a table has a virtual column as part of its primary key, which most often happens when using a hash-sharded primary key index.

Related

Dropping table identity causes ORA-00600 error on Oracle 12c

My Oracle DB version is 12.1.0.2.0.
I'm having a hard time removing the column identity. Also tried to drop the column and dropping the table with purge command, but every time I'm getting the same Oracle error:
ORA-00600: internal error code, arguments: [12811], [96650], [], [], [], [], [], [], [], [], [], []
Just can't touch the identity column. I tried below commands but no luck:
ALTER TABLE DYS_CATEGORY MODIFY CATEGORY_ID DROP IDENTITY;
ALTER TABLE DYS_CATEGORY DROP COLUMN CATEGORY_ID;
DROP TABLE DYS_CATEGORY PURGE;
I can drop any other column from the table, but the problem is with identity column.
Identity columns are new to Oracle, just introduced in 12c.
This is a problem with Oracle 12.1.0.2.0. At least one other person has reported it (on Windows, which may be relevant).
The error you have is an ORA-00600, which is Oracle's default message for unhandled exceptions i.e. Oracle bugs. The correct answer is to raise a Service Request with Oracle Support; they will be able to provide you with a patch or a workaround if you have a corrupted table you need to fix. If you don't have a Support contract you may be out of luck.
For future reference dropping identity columns is a two-stage process:
alter table t42 modify id drop identity;
alter table t42 drop column id;
As it happens, this is not a problem on the very latest version of the product. In Oracle 18c we can just drop the column without modifying it first. LiveSQL demo.
As William said above it looks like there is/was a system generated the sequence for the identity column that was deleted but the record in idnseq$ remains intact.
I would not recommend this to anyone, but I created a new sequence called junk in the same schema. I then found the object_id for the table and the sequence I created and updated idnseq$ manually changing the seqobj# to the object_id of my new sequence for the object# of the table in question.
I was then able to drop the table and purge the recyclebin successfully.
Really don't recommend hacking oracle system tables, but this was a test system that didn't really matter and it worked.
after lots of search and hard work if the table stil showing error ORA-00600: internal error code, arguments:
Do the below step.
Take a backup of original tables
syntax: Create table original_table_back as select * from original_table;
Rename original table to some new table name
syntax: Rename original_table to original_table_1;
Rename backup to the original table
syntax: Rename Original_table_back to original_table.

Add enum type column to table

im trying to add a new column to my existing table 'Results', and it seems to be very easy but I cant see the mistake.
Here is my code:
SQL> Alter table results add column CAL ENUM('A','B');
ERROR at line 1: ORA-00904: : invalid identifier
What am I missing?
I've read this from w3 and this from java2s but cant see the difference to mine.
Thanks, and sorry for the dumb question.
OK, with an ORA- error I am assuming that this is an oracle database, and not mysql. you have both tags and you are linking to MySQL example, but the error is not a MySQL error.
Assuming that this IS an Oracle DB, then there is no native ENUM data type. You have to do this as follows: First - you add the column with a correctly defined data type, and second you create a constrained list of permitted values on that column as a check constraint.
Alter table results add (cal varchar2(1));
Alter table results add constraint chk_cal CHECK (cal in ('A','B')) ENABLE;
(EDITED to fix brackets in constraint creation line)

How can I insert to Cassandra with CQL, to table with only primary key, using UPDATE?

I need to insert new rows to Cassandra, to a table that has only primary key columns, e.g.:
CREATE TABLE users (
user_id bigint,
website_id bigint,
PRIMARY KEY (user_id, website_id)
)
The obvious way to do it would be by INSERT:
INSERT INTO users(user_id, website_id) VALUES(1,2);
But I want to do it with use of Hadoop CqlOutputFormat and CqlRecordWriter only supports UPDATE statements. That's usually not a problem as UPDATE is in theory semantically the same as INSERT. (It will create rows if given primary key does not exist).
But here... I don't know how to construct UPDATE statement - it seems that CQL just does not
support my case, where there are non-primary key columns. See what I tried:
> update users set where user_id=3 and website_id=2 ;
Bad Request: line 1:18 no viable alternative at input 'where'
> update users set website_id=2 where user_id=3;
Bad Request: PRIMARY KEY part website_id found in SET part
> update users set website_id=2 where user_id=3 and website_id=2;
Bad Request: PRIMARY KEY part website_id found in SET part
> update users set website_id=2,user_id=1;
Bad Request: line 1:40 mismatched input ';' expecting K_WHERE
Some ideas on how to resolve it?
Many thanks.
Not sure if you can do this with update like that. But why not just create a new dummy column that you never use for anything else? Then you could do
update users set dummy=1 where user_id=3 and website_id=2;
You can't update primary key values in Cassandra as you have explained. As a solution you could also delete the row and insert a new one with the correct value in it. It's just a bit cleaner than creating two rows with one incorrect.

Create constraint in alter table without checking existing data

I'm trying to create a constraint on the OE.PRODUCT_INFORMATION table which is delivered with Oracle 11g R2.
The constraint should make the PRODUCT_NAME unique.
I've tried it with the following statement:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME);
The problem is, that in the OE.PRODUCT_INFORMATION there are already product names which currently exist more than twice.
Executing the code above throws the following error:
an alter table validating constraint failed because the table has
duplicate key values.
Is there a possibility that a new created constraint won't be used on existing table data?
I've already tried the DISABLED keyword. But when I enable the constraint then I receive the same error message.
You can certainly create a constraint which will validate any newly inserted or updated records, but which will not be validated against old existing data, using the NOVALIDATE keyword, e.g.:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
NOVALIDATE;
If there is no index on the column, this command will create a non-unique index on the column.
If you are looking to enforce some sort of uniqueness for all future entries whilst keeping your current duplicates you cannot use a UNIQUE constraint.
You could use a trigger on the table to check the value to be inserted against the current table values and if it already exists, prevent the insert.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
or you could just remove the duplicate values and then enfoce your UNIQUE constraint.
EDIT: After Jonearles and Jeffrey Kemp's comments, I'll add that you can actually enable a unique constraint on a table with duplicate values present using the NOVALIDATE clause but you'd not be able to have a unique index on that constrained column.
See Tom Kyte's explanation here.
However, I would still worry about how obvious the intent was to future people who have to support the database. From a support perspective, it'd be more obvious to either remove the duplicates or use the trigger to make your intent clear.
YMMV
You can use deferrable .
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
deferrable initially deferred NOVALIDATE;

Cant drop partition from header table

We have a header table which has a primary key and a detail table which references the header table (Foreign key). Both the header and detail tables are monthly partitioned. These tables contain 5 years of data, so when a new month comes the data for the first partition is deleted and so on. So that always only 5 years of data is maintained.
The problem that we are facing is while dropping the partition from the header we are getting the following error:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
ORA-06512: at "SCH_TEST.DROP_PARTITION", line 51
ORA-06512: at line 16 (DBD ERROR: OCIStmtExecute)
We have deleted the referencing data from the detail table, still the above error is occuring.
One way for solving this error is to disable the constraint, drop the partition and then enable the constraint. Is there any other approach/solution for this issue.
Database is Oracle 11G.
Edit 1: I'm able to drop the partition from header table if i delete the data in the partition first and then drop it. Any idea how this works?
Oracle 11g introduced partition by reference for this. Are you using reference partitioning? If not you need to change your partitioning scheme.
I chose "disable the reference constraint and then drop the partition" approach, as i did not find any other solution for this issue.
Posting this information here and accepting it as answer so that it may help others who search for the same issue.

Resources