Not able to drop field in MariaDB table - mariadb

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.

Related

Set auto_increment for a primary key already created

I created two tables in a database on MariaDB: CasaProduzione and Produzione.
create table CasaProduzione(nome varchar(80) primary key);
alter table CasaProduzione add column id tinyint;
alter table CasaProduzione drop primary key;
alter table CasaProduzione modify nome varchar(80) not null;
alter table CasaProduzione modify id tinyint primary key auto_increment;
create table Produzione(
id_film smallint not null,
id_casaProduzione tinyint not null,
data date not null,
constraint `fk_produzione`
foreign key (id_film) references Film(id),
foreign key (id_casaProduzione) references CasaProduzione(id)
on update cascade
on delete restrict);
alter table Produzione modify data in smallint(4);
After i moved the column id in the table of CasaProduzione at first
alter table CasaProduzione modify column id tinyint(4) first;
Then i tried to set auto_increment in prevoius column
alter table Produzione modify column id tinyint(4) auto_increment;
ERROR 1833 (HY000): Cannot change column 'id': used in a foreign key constraint 'Produzione_ibfk_1' of table 'Film.Produzione'
So i tried to cancel the foreign key from Produzione
alter table Produzione drop foreign key fk_produzione;
but the result is the same.
What am I doing wrong?
After suggestion from the comment, I post here the result of this command:
SHOW CREATE TABLE Film.Produzione \G;
***************************
1. row
***************************
Table: Produzione
Create Table:
CREATE TABLE Produzione (
id_film SMALLINT(6) NOT NULL,
id_casaProduzione TINYINT(4) NOT NULL,
DATA SMALLINT(4) DEFAULT NULL,
KEY fk_produzione (id_film),
KEY id_casaProduzione (id_casaProduzione),
CONSTRAINT Produzione_ibfk_1 FOREIGN KEY (id_casaProduzione) REFERENCES CasaProduzione (id) ON UPDATE CASCADE )
ENGINE=INNODB DEFAULT CHARSET=utf8mb4
1 row in set (0.001 sec)
As told by #FanoFN, with the command
show CREATE TABLE Film.Produzione \G;
the system showed me the constraint on the table Produzione. The system created the constraint Produzione_ibfk_1
I deleted this constraint with the command
alter table Produzione drop foreign key Produzione_ibfk_1;
Query OK, 0 rows affected (0.130 sec)
Records: 0 Duplicates: 0 Warnings: 0
After I can applied the command
alter table Produzione modify column id tinyint(4) auto_increment;
Thanks a lot

Sqlite Auto Increment Error while creating table [duplicate]

This question already has answers here:
Error in near 'Autoincrement'
(4 answers)
Closed 12 months ago.
I am trying to create a table in sqlite3 using the following query
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTOINCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTOINCREMENT=2 DEFAULT CHARSET=utf8;
I am getting an error Error: near "AUTOINCREMENT": syntax error
How can we solve and create a table according to the above query ?
Thanks in advance
From sqlite AUTOINCREMENT doc section 3
Because AUTOINCREMENT keyword changes the behavior of the ROWID selection algorithm, AUTOINCREMENT is not allowed on WITHOUT ROWID tables or on any table column other than INTEGER PRIMARY KEY. Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in an error.

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.

Error Code 1005 in mysql while applying Foreign key to a table

I am having two tables tbluserlogindetail and tblRoles.
tbluserlogindetail is as follows
CREATE TABLE `tbluserlogindetail` (
`LoginID` varchar(45) NOT NULL,
`Name` varchar(45) DEFAULT NULL,
`Password` varchar(45) DEFAULT NULL,
PRIMARY KEY (`LoginID`),
UNIQUE KEY `LoginID_UNIQUE` (`LoginID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
EDIT doratesting.tbluserlogindetail;
and my second table tblRoles is as follows
CREATE TABLE `tblroles` (
`RoleID` int(11) NOT NULL,
`LoginID` varchar(45) NOT NULL,
PRIMARY KEY (`RoleID`,`LoginID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
I tried the the following to assign a primary key for the column LoginID in tblroles table but i don't know where i went wrong can any one help me.
I have gone through the documentation but unable to rectify the error so please help me
ALTER TABLE tblroles
ADD FOREIGN KEY (loginid)
REFERENCES tbluserlogindetail(loginid)
The referenced columns must be unique in the referenced table. Try one of these options:
The primary key on tbluserlogindetail is (ID, loginid) so you could use that as your foreign key instead of (loginid). This will require you to add a column tbluserlogindetail_ID to tblroles.
Try adding a unique index to the LoginID column of the tbluserlogindetail table. This is only possible if it is actually unique.
Also, why is your primary key on tbluserlogindetail defined as PRIMARY KEY (ID, LoginID)? The ID field is an auto-increment field and is already unique. So why do you also include the LoginID as part of the primary key? I think you need to go back to your table design and rethink which columns to choose as your primary keys.
this is just guess/ assumption ,i like to share here,
PRIMARY KEY (ID,LoginID)
in tbluserlogindetail are considered as surrogate key,
when we execute the child table,That is tblroles,
child table expected primary key in parent table,
but actually we created the surrogate key, due to this reason, i alter query failed,
ALTER TABLE tblroles ADD FOREIGN KEY (LoginID) REFERENCES tbluserlogindetail(LoginID)
This is my assumption, give feedback for my answer,
I did following changes to execute the Alter table command:
1.tblroles create with given Create query command, after created i manually deleted the
LoginID primary key in tblroles table,
Changed varchar to int in LoginID,
in tbluserlogindetail, deleted the ID AUTO_INCREMENT,Pk.
Check i updated ALTER query

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