Sqlite Auto Increment Error while creating table [duplicate] - sqlite

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.

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

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.

near ",": syntax error using foreign keys [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I keep getting the issue syntax error on the CREATE TABLE lines for all three tables that use the foreign key statements. I've been troubleshooting for a while and I'm stressing out as it's my first programming uni assignment.
PRAGMA foreign_keys = ON;
CREATE TABLE Customer_info (
customer_id VARCHAR(32) PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
login_name VARCHAR(255)
);
CREATE TABLE Payment_method (
payment_id VARCHAR(32) PRIMARY KEY,
billing_adress VARCHAR(255),
card_holder_name VARCHAR(255),
credit_card_nr INT,
cvs INT,
exp_date DATE
);
CREATE TABLE Product_list (
p_name VARCHAR(255) PRIMARY KEY,
p_price INT,
publisher VARCHAR(255),
developer_studio VARCHAR(255)
);
CREATE TABLE Games (
game_name VARCHAR(255) PRIMARY KEY,
game_desc VARCHAR(255),
FOREIGN KEY (game_name),
REFERENCES Product_list (p_name)
);
CREATE TABLE Software (
soft_name VARCHAR(255) PRODUCT KEY,
soft_desc VARCHAR(255),
FOREIGN KEY (soft_name),
REFERENCES Product_list (p_name)
);
CREATE TABLE Prof_info (
profile_id VARCHAR(32) PRIMARY KEY,
nickname VARCHAR(255),
display_name VARCHAR(255),
description VARCHAR(255),
FOREIGN KEY (profile_id),
REFERENCES Customer_info (customer_id)
);
CREATE TABLE Groups (
group_id VARCHAR(32) PRIMARY KEY,
g_name VARCHAR(255),
g_desc VARCHAR(255)
);
Also I apologise if these are easy questions but I am sleep deprived, stressed & running out of time.
edit: removed the second question I had at the end as it was pointed out to me this isn't supposed to be done.
foreign key ... references is a single expression.
FOREIGN KEY (profile_id) REFERENCES Customer_info(customer_id)
SQLite does not require a separate foreign key clause. You can do this inline.
-- A primary key that is also a foreign key is a bit odd.
game_name VARCHAR(255) PRIMARY KEY REFERENCES Customer_info(customer_id)
See table-constraint and foreign-key-clause in the SQLite create table grammar.
Also would it be possible to state 2 foreign keys referencing different tables within the same table? How would I go about doing this?
Yes, just declare two foreign keys. Perhaps you're confused because you're making your foreign keys also your primary keys.
Here's a sketch of a better design. All of the pieces get their own table. All tables get an auto-incremented integer primary key which is assured to be unique and unchanging. Column names do not use prefixes, fully qualify them in queries if necessary. Table names are normalized as plurals.
create table publishers (
id integer primary key,
name varchar(255) not null
);
create table developers (
id integer primary key,
name varhcar(255) not null
);
create table products (
id integer primary key,
name varchar(255) not null,
price numeric(6,2),
publisher_id integer references publishers(id),
developer_id integer references developers(id)
);
create table games (
id integer primary key,
name varchar(255) not null,
description varchar(255),
product_id integer references products(id)
);

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.

SQLite foreign key mismatch error

Why am I getting a SQLite "foreign key mismatch" error when executing script below?
DELETE
FROM rlsconfig
WHERE importer_config_id=2 and
program_mode_config_id=1
Here is main table definition:
CREATE TABLE [RLSConfig] (
"rlsconfig_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"importer_config_id" integer NOT NULL,
"program_mode_config_id" integer NOT NULL,
"l2_channel_config_id" integer NOT NULL,
"rls_fixed_width" integer NOT NULL
,
FOREIGN KEY ([importer_config_id])
REFERENCES [ImporterConfig]([importer_config_id]),
FOREIGN KEY ([program_mode_config_id])
REFERENCES [ImporterConfig]([importer_config_id]),
FOREIGN KEY ([importer_config_id])
REFERENCES [ImporterConfig]([program_mode_config_id]),
FOREIGN KEY ([program_mode_config_id])
REFERENCES [ImporterConfig]([program_mode_config_id])
)
and referenced table:
CREATE TABLE [ImporterConfig] (
"importer_config_id" integer NOT NULL,
"program_mode_config_id" integer NOT NULL,
"selected" integer NOT NULL DEFAULT 0,
"combined_config_id" integer NOT NULL,
"description" varchar(50) NOT NULL COLLATE NOCASE,
"date_created" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
PRIMARY KEY ([program_mode_config_id], [importer_config_id])
,
FOREIGN KEY ([program_mode_config_id])
REFERENCES [ProgramModeConfig]([program_mode_config_id])
)
When you use a foreign key over a table that has a composite primary key you must use a composite foreign key with all the fields that are in the primary key of the referenced table.
Example:
CREATE TABLE IF NOT EXISTS parents
(
key1 INTEGER NOT NULL,
key2 INTEGER NOT NULL,
not_key INTEGER DEFAULT 0,
PRIMARY KEY ( key1, key2 )
);
CREATE TABLE IF NOT EXISTS childs
(
child_key INTEGER NOT NULL,
parentKey1 INTEGER NOT NULL,
parentKey2 INTEGER NOT NULL,
some_data INTEGER,
PRIMARY KEY ( child_key ),
FOREIGN KEY ( parentKey1, parentKey2 ) REFERENCES parents( key1, key2 )
);
I am not sure about SQLite. But I found this link on google. http://www.sqlite.org/foreignkeys.html.
Some of the reasons can be
The parent table does not exist, or
The parent key columns named in the foreign key constraint do not exist, or
The parent key columns named in the foreign key constraint are not the primary key of the parent table and are not subject to a unique constraint using collating sequence specified in the CREATE TABLE, or
The child table references the primary key of the parent without specifying the primary key columns and the number of primary key columns in the parent do not match the number of child key columns.
Unfortunately, SQLite gives this error all the time without mentioning WHICH foreign key constraint failed. You are left to try to check them one by one, which often doesn't work, and then rebuild the table without the constraints and add them back one by one until you find the problem. SQLite is great in a lot of ways, but this isn't one of them.

Resources