Mroonga on Mariadb Primary Key Insert error - mariadb

I keep on getting duplicate primary key error when trying to insert into my Mroonga table on MariaDB 10.0.19. Does anyone know what might be the cause?
SQL:
insert into tbl_mroonga select pk_id, keyword from tbl_inno;
Create table:
create table tbl_mroonga (
'PK_ID' int(11) not null default 0,
'Keyword' varchar(191) null default null,
primary key (`pk_id`),
fulltext index ('keyword')) Engine=MROONGA;
create table tbl_inno (
'PK_ID' int(11) not null default 0,
'Keyword' varchar(191) null default null,
primary key (`pk_id`),
fulltext index ('keyword')) Engine=INNODB;
I'm inserting about 3.5million rows from tbl_inno to tbl_mroonga and it fails at around 400K rows. I've tried it with "select distinct" and "group by pk_id" and still it fails.
Any help would be greatly appreciated!
Thank you.

Write a loop that copies over 10K rows at a time. Use something like (example of the 13th chunk):
INSERT INTO tbl_mroonga
SELECT pk_id, keyword
FROM tbl_inno
WHERE pk_id > 120000
AND pk_id <= 130000
and COMMIT after each chunk.

Related

Not able to drop field in MariaDB table

I have table my_transitions and I am trying to delete field called myStage.
I am using this command:
alter table `my_transitions` drop `myStage`
I am seeing this error:
Key column 'myStage' doesn't exist in table
But when I list all fields using this command:
describe my_transitions
I can see
id bigint(20) unsigned NO PRI NULL auto_increment
myStage varchar(255) NO MUL NULL
updated_at timestamp YES NULL
Anyone can see if I am doing something wrong?
EDIT:
If I run show create table my_transitions;, I am getting:
CREATE TABLE `my_transitions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`myStage` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`myStage1` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_stage_combination` (`myStage`,`myStage1`),
KEY `my_transitions_myStage` (`myStage`),
KEY `my_transitions_myStage1` (`myStage1`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
I have solved it by first deleting the unique key
ALTER TABLE my_transitions DROP INDEX unique_stage_combination;
It seems like it is not possible to delete a column if it is a part of index key in Maria DB 10.5.8.
This is a pecular bug in MariaDB. It affects MariaDB 10.5.
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=867204670347fa29e40bd5eb510c6956
The workaround is to drop the UNIQUE KEY that column mystage is part of first, then drop the column.
alter table my_transitions drop key unique_stage_combination, drop column mystage;
P.S.: I tested this on MySQL 8.0 and it does not require the workaround. It does drop the column, but it leaves the table with a UNIQUE KEY column on just one column mystage1, which might not be what you want.

CHECK Constraint based on a column value IN OTHER Table

SqlServer
Suppose I have 2 tables:
Table 1 - having column A
Table 2 - having column B [Bit] Not Null
Is it possible to have a Check Constraint, such that value of Column B can be "0", only when Column A is NOT NULL.
OR put it other way, value of Column B can be "1", only when Column A is NULL.
Thanks in advance.
Assuming that these tables are already related by a suitable foreign key, we can implement this check using a computed column and a new foreign key.
Make sure you read to the end
So if we have:
CREATE TABLE Table1 (
Table1ID char(5) not null,
ColumnA int null,
constraint PK_Table1 PRIMARY KEY (Table1ID)
)
CREATE TABLE Table2 (
Table2ID char(7) not null,
Table1ID char(5) not null,
ColumnB bit not null,
constraint PK_Table2 PRIMARY KEY (Table2ID),
constraint FK_Table2_Table1 FOREIGN KEY (Table1ID) references Table1 (Table1ID)
)
We can run this script:
alter table Table1 add
ColumnBPrime as CAST(CASE WHEN ColumnA is NULL THEN 1 ELSE 0 END as bit) PERSISTED
go
alter table Table1 add constraint UQ_Table1_WithColumnBPrime UNIQUE (Table1ID, ColumnBPrime)
go
alter table Table2 add constraint FK_Table2_Table1_CheckColumnB FOREIGN KEY (Table1ID, ColumnB) references Table1 (Table1ID,ColumnBPrime)
Hopefully you can see how this enforces the relationship between the two tables1.
However, there's an issue. In T-SQL, any DML statement may only make changes to one table. So there's no way to issue an update that both changes whether ColumnA is null or not and changes Column B to suit it.
This is another good reason not to have Column B in the database at all - it's derived information, and in our quest to ensure it always matches its definition, we'd have to always delete from Table 2, update Table 1 and re-insert in Table 2.
1It's now a matter of personal taste whether you remove the previous foreign key or leave it in place as the "real" one.

sqlite integer primary key not null constraint failed

According to the SQLite documentation / FAQ a column declared INTEGER PRIMARY KEY will automatically get a value of +1 the highest of the column if omitted.
Using SQLite version 3.22.0 2018-01-22 18:45:57
Creating a table as follows:
CREATE TABLE test (
demo_id INTEGER PRIMARY KEY NOT NULL,
ttt VARCHAR(40) NOT NULL,
basic VARCHAR(25) NOT NULL,
name VARCHAR(255) NOT NULL,
UNIQUE(ttt, basic) ON CONFLICT ROLLBACK
) WITHOUT ROWID;
Then inserting like this:
INSERT INTO test (ttt, basic, name) VALUES ('foo', 'bar', 'This is
a test');
gives:
Error: NOT NULL constraint failed: test.demo_id
sqlite>
When it is expected to create a record with a demo_id value of 1. Even if the table already contains values, it'll fail inserting the row without explicitly specifying the id with the same error.
What am I doing wrong?
The documentation says that you get autoincrementing values for the rowid. But you specified WITHOUT ROWID.

Foreign key in sqlite

I am trying to convert the table that was dumped from mysql. Following is the code I have from mysql for creating the table:
CREATE TABLE "tbl_profession_attributes" (
"id" bigint(20) NOT NULL,
"tbl_profession_attribute_id" bigint(20) DEFAULT '0',
"code" varchar(10) NOT NULL,
"name" varchar(100) NOT NULL,
"keyword" text NOT NULL,
"tbl_profession_list_id" bigint(20) NOT NULL,
PRIMARY KEY ("id"),
KEY "tbl_passion_attribute_id" ("tbl_profession_attribute_id"),
KEY "tbl_passion_list_id" ("tbl_profession_list_id")
);
When I run this query for sqlite, I get the following error:
Query Error: near "KEY": syntax error Unable to execute statement
Can someone please help me resolve this.
Any help is much appreciated.
First, MySQL keyword KEY is synonym for INDEX. So this is not about foreign key at all.
Second, SQLite does not support creating non-primary index in CREATE TABLE statement. You should specify separate statement for create index, something like:
CREATE INDEX tbl_passion_attribute_id_idx
ON tbl_profession_attributes(tbl_profession_attribute_id)

SQLite: Constraint failed on Insert

I have one Auto Increment Field, rest are Integer,Text and Datetime field. How do I fix it out?
The Table Structure is given below:
CREATE TABLE "q1" (
"sb_id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"sb_title" text(100,0) NOT NULL,
"sb_details" text(300,0) NOT NULL,
"sb_image" text(30,0) NOT NULL,
"sb_type" integer(4,0) NOT NULL DEFAULT '1',
"sb_date" datetime NOT NULL
)
It could be because in your insert command
connection.execute("INSERT INTO q1(sb_title,sb_details)VALUES(?,?)",a,b);
you didn't insert any values for sb_image or sb_date, both of which are NOT NULL and have no default defined. SQLite doesn't know what to put in there. You should either take away the NOT NULL constraint on those columns, define a default for the columns, or insert something explicitly.

Resources