Error in near 'Autoincrement' - sqlite

I get a syntax error near AUTOINCREMENT. What is the cause of this error?
CREATE TABLE person (
id INTEGER NOT NULL AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE department (
id INTEGER NOT NULL AUTOINCREMENT,
name TEXT NOT NULL,
FOREIGN KEY (leader) REFERENCES person(id)
);

According to SQLite FAQ you have to declare either a INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT column to achieve that.

In SQLite you need not to specify AUTOINCREMENT if you are specifying a column as Primary Key...

SQLite AUTOINCREMENT : You Should Avoid Using It
Unless you create a table specifying the WITHOUT ROWID option, you get an implicit auto increment column called rowid.
The rowid column store 64-bit signed integer that uniquely identifies a row within the table.

It's an easy solution. Just use AUTOINCREMENT instead of AUTO_INCREMENT

Related

How do I resolve a foreign key mismatch error?

I tried inserting values into my 'JobTypes' table but I keep getting a foreign key mismatch error. I think I've made an error with my foreign keys but I can't quite figure out where exactly as I'm quite new at this, can anyone help me out?
This is what I have so far:
CREATE TABLE Projects (
Proj_ID INTEGER,
Proj_name TEXT,
PRIMARY KEY (Proj_ID)
);
CREATE TABLE Employees (
Emp_ID INTEGER,
Proj_ID INTEGER,
Emp_fname TEXT,
PRIMARY KEY(Emp_ID, Proj_ID),
FOREIGN KEY(Proj_ID) REFERENCES Projects(Proj_ID)
);
CREATE TABLE HourRates (
Job_type TEXT,
Hour_rate TEXT,
PRIMARY KEY(Job_type)
);
CREATE TABLE JobTypes (
Emp_ID INTEGER,
Job_type TEXT,
PRIMARY KEY(Emp_ID)
FOREIGN KEY (Emp_ID) REFERENCES Employees(Emp_ID)
FOREIGN KEY (Job_type) REFERENCES HourRates(Job_type)
);
Every FKEY target must be either a primary key or have an explicit unique index defined. Employees(Emp_ID) does not have a unique constraint or index. If this column is unique, you need to add a unique constraint or define a simple PKEY. Otherwise, you will not be able insert any data into the JobTypes table. The error message generated is confusing, and SQLite should at least issue some kind of warning when you create the JobTypes table, but this is not how it works, perhaps, due to the need for backward compatibility. Anyhow, my guess is that your Employees table actually needs to be split into Employees & Employees_Projects (many-to-many) tables.

Is "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" guarantee to be the alias for "rowid"?

I have read https://sqlite.org/autoinc.html but still not fully confirm the following idea.
I was wondering, INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL guarantee to be the alias for rowid?
For instance, for the following table
CREATE TABLE `attachment` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, ... )
Will
select * from attachment order by id;
equivalent to
select * from attachment order by rowid;
Thanks.
The link you are refering to describe the various algorithm used to select a default rowid (with ou without AUTOINCREMENT).
The rules for a primary key to be an integer primary key (and so an alias for the rowid) are detailed in ROWIDs and the INTEGER PRIMARY KEY :
The type must be exactly INTEGER (case insensitive)
The declaration of the column must not include PRIMARY KEY DESC
SQLite goes to great lengths to keep the documented behaviours (and sometime undocumented, e.g. the second rule is there not to break the compatibility with an old bugged version)
So INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL is an alias for the rowid and select the algorithm that guaranties that generated rowids will never be reused, even if you delete rows.

What happens if you declare an SQLite column PRIMARY KEY + UNIQUE?

SQLite's documentation says:
A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints.
What I'm wondering is, if I declare something like:
CREATE TABLE Example (
id INTEGER PRIMARY KEY UNIQUE);
Does SQLite create two indexes or one? Is the behavior different if I'm not using the rowid (i.e. if the column was id TEXT PRIMARY KEY UNIQUE)?
I realize the simplest thing to do is just remove UNIQUE but I'm curious what effect this will have.
When you define a primary key it will be unique, no need to define another index for unique column.
The point of primary keys is so that they are unique for any given row.
Meaning you can choose one or more fields to be a primary key, but a single field doesn't have to be unique.
Unique on the other hand has to be unique for the specified fields.
So it's more like a constraint you are placing.
Given this example:
CREATE TABLE IF NOT EXISTS user2domain (
userID INTEGER NOT NULL,
domainID INTEGER NOT NULL,
PRIMARY KEY (userID, domainID),
UNIQUE(userID, domainID) ON CONFLICT IGNORE
)
You don't actually have to specify the unique constraint as the primary key would cover the same fields.
CREATE TABLE IF NOT EXISTS user2domain (
userID INTEGER NOT NULL,
domainID INTEGER NOT NULL,
PRIMARY KEY (userID, domainID)
)
-- add this way
INSERT OR IGNORE INTO user2domain(userID, domainID) VALUES(#userID, #domainID)
This would be enough.
Use the Unique constraint when you absolutely feel like any fields
must be unique but not necessarily a row identifier.
The expected behaviour is overwriting. Primary is of a higher order precedence.

Can someone give me a PK insert sample?

So I'm making things complicated ...I think. A primary key basically is to make the row unique. Is that correct? Anyone want to show me an insert statement with the values for PK?
The SQLite documentation says:
On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not
explicitly given a value, then it will be filled automatically with an
unused integer, usually one more than the largest ROWID currently in
use. This is true regardless of whether or not the AUTOINCREMENT
keyword is used.
So, on a table like
CREATE TABLE test(id INTEGER PRIMARY KEY, descr TEXT);
an insert with a valid id could be
INSERT INTO test(descr) VALUES('this is a test');
A primary key, also called a primary keyword, is a key in a relational database that is unique for each record. It is a unique identifier, such as a driver license number, telephone number (including area code), or vehicle identification number (VIN). A relational database must always have one and only one primary key.
if you are using CREATE TABLE, if you are creating the primary key on a single field, you can use:
CREATE TABLE mytable (
field1 TEXT,
field2 INTEGER PRIMARY KEY,
field3 BLOB,
);
Reference more at: https://www.sqlite.org/lang_createtable.html & http://sqlite.org/faq.html#q11

Why need I input value for AUTOINCREMENT field in SQLite?

I create a SQLite table, the field id is INTEGER PRIMARY KEY AUTOINCREMENT,
I remember the AUTOINCREMENT field will be passed a value by system when I insert a record in SQL Server database, so I try to use insert into mytable values ("AA") to add a record,
but I get an error, so I have to use insert into mytable values (2,"BB") to add new record, why?
-- Describe MYTABLE
CREATE TABLE "mytable" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"mycontent" TEXT
)
Auto increment field is also just like other fields. Since you are going to insert only one field among two created in the table, you have to explicitly specify them. for ex.
insert into mytable(my content) values('hi');

Resources