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.
Related
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.
I am using SQLite and DB Browser for SQLite. I know that if you want to insert a foreign key using the GUI, you just have to scroll to the right and double click to insert the foreign key to the corresponding field. The problem is when you want to use a multiple foreign key. How or where should i put the multiple foreign key constrain using the GUI ?
my actual sql is (returns an exception 'foreign key mismatch - "diseases" referencing "beehives"...'):
CREATE TABLE "diseases" (
"id" INTEGER NOT NULL,
"id_beehive" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL,
"disease" TEXT NOT NULL,
"treatment" TEXT NOT NULL,
"start_treat_date" DATE NOT NULL,
"end_treat_date" DATE,
PRIMARY KEY("id"),
FOREIGN KEY("id_beehive") REFERENCES "beehives"("number") ON UPDATE CASCADE,
FOREIGN KEY("id_apiary") REFERENCES "beehives"("id_apiary") ON UPDATE CASCADE
);
what i want to do using the gui is:
CREATE TABLE "diseases" (
"id" INTEGER NOT NULL,
"id_beehive" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL,
"disease" TEXT NOT NULL,
"treatment" TEXT NOT NULL,
"start_treat_date" DATE NOT NULL,
"end_treat_date" DATE,
PRIMARY KEY("id"),
FOREIGN KEY("id_beehive","id_apiary") REFERENCES "beehives"("number","id_apiary") ON UPDATE CASCADE
);
beehives sql:
CREATE TABLE "beehives" (
"number" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL DEFAULT -2,
"date" DATE,
"type" TEXT,
"favorite" BOOLEAN DEFAULT 'false',
PRIMARY KEY("number","id_apiary"),
FOREIGN KEY("id_apiary") REFERENCES "apiaries"("id") ON DELETE SET NULL
);
After researching, it is not possible for the moment.
Everytime you modify the structure of the table, what DB Browser for SQLite do on the background is renaming the table, creating a new table with the updated structure, move the data to this new table and delete the old table. So in the GUI it looks like its only updating.
Unfortunatly, to create a composite foreign key or modify a simple FK to a composite FK , you can only do it manualy from the SQL window.
example :
FOREIGN KEY("id_beehive","id_apiary") REFERENCES "beehives"("number","id_apiary") ON
UPDATE CASCADE
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.
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
I need to convert a table from MySQL to SQLite, but I can't figure out how to convert an enum field, because I can't find ENUM type in SQLite.
The aforementioned field is pType in the following table:
CREATE TABLE `prices` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`pName` VARCHAR(100) NOT NULL DEFAULT '',
`pType` ENUM('M','R','H') NOT NULL DEFAULT 'M',
`pField` VARCHAR(50) NULL DEFAULT NULL,
`pFieldExt` VARCHAR(50) NULL DEFAULT NULL,
`cmp_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
I need a field with only three values for the user to chose, and I would like to enforce that in the DB, not just in my application.
SQLite way is to use a CHECK constraint.
Some examples:
CREATE TABLE prices (
id INTEGER PRIMARY KEY,
pName TEXT CHECK( LENGTH(pName) <= 100 ) NOT NULL DEFAULT '',
pType TEXT CHECK( pType IN ('M','R','H') ) NOT NULL DEFAULT 'M',
pField TEXT CHECK( LENGTH(pField) <= 50 ) NULL DEFAULT NULL,
pFieldExt TEXT CHECK( LENGTH(pFieldExt) <= 50 ) NULL DEFAULT NULL,
cmp_id INTEGER NOT NULL DEFAULT '0'
)
This will limit the pType column to just the values M, R, and H, just
like enum("M", "R", "H") would do in some other SQL engines.
There is no enum type in SQLite, only the following:
NULL
INTEGER
REAL
TEXT
BLOB
Source: http://www.sqlite.org/datatype3.html
I'm afraid a small, custom enum table will be required in your case.
To expand on MPelletier’s answer, you can create the tables like so:
CREATE TABLE Price (
PriceId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(100) NOT NULL,
Type CHAR(1) NOT NULL DEFAULT ('M') REFERENCES PriceType(Type)
);
CREATE TABLE PriceType (
Type CHAR(1) PRIMARY KEY NOT NULL,
Seq INTEGER
);
INSERT INTO PriceType(Type, Seq) VALUES ('M',1);
INSERT INTO PriceType(Type, Seq) VALUES ('R',2);
INSERT INTO PriceType(Type, Seq) VALUES ('H',3);
Now the enumeration values are available directly in the Price table as they would be using an ENUM: you don’t need to join to the PriceType table to get the Type values, you only need to use it if you want to determine the sequence of the ENUMs.
Foreign key constraints were introduced in SQLite version 3.6.19.