AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY - SQL [duplicate] - sql

This question already has answers here:
Is there AUTO INCREMENT in SQLite?
(9 answers)
Closed 11 months ago.
I'm trying to create a simple table using DBeaver and is giving me this error, any hints? Thanks.
SQL Error [1]: [SQLITE_ERROR] SQL error or missing database (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY)
CREATE TABLE users (
id INT (11) NOT NULL PRIMARY KEY AUTOINCREMENT,
usermane VARCHAR (255) NOT NULL,
password VARCHAR (255) NOT NULL
);

The error says "SQLITE_ERROR" which means it comes from SQLite, not MySQL.
Perhaps you are using a CREATE TABLE statement designed for MySQL, and executing it against SQLite?
Anyway, SQLite requires you define an autoincrement field as INTEGER, not INT.
And you don't need to use the AUTO_INCREMENT keyword at all. In SQLite, the auto-increment behavior is implied by using INTEGER. You may use the keyword AUTOINCREMENT (with no underscore character, unlike MySQL's keyword), but you should read the documentation about the change in behavior when you use this keyword, and make sure it's what you want: https://sqlite.org/autoinc.html
sqlite> CREATE TABLE users (
...> id INT (11) NOT NULL PRIMARY KEY AUTOINCREMENT,
...> usermane VARCHAR (255) NOT NULL,
...> password VARCHAR (255) NOT NULL
...> );
Error: in prepare, AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (1)
sqlite> CREATE TABLE users (
...> id INTEGER NOT NULL PRIMARY KEY,
...> username VARCHAR(255) NOT NULL,
...> password VARCHAR(255) NOT NULL
...> );
sqlite>
(Notice no error the second time, therefore the create succeeded.)
P.S. I also corrected "usermane" to "username" which I suppose is what you want.

Related

I tried to run this but I get an error "ORA-00904: : invalid identifier" [duplicate]

This question already has answers here:
"date" as a column name
(3 answers)
when I create table, I got this error ORA-00904: : invalid identifier
(1 answer)
Query only creating 7 out of 10 tables.. assistance please?
(2 answers)
Closed 1 year ago.
The error occurs here:
CREATE TABLE CERDIT_CARD_LOG
(
CERDIT_CARD_NUMBER CHAR(20) NOT NULL,
CERDIT_CARD_LOG_NUMBER CHAR(15) NOT NULL,
CERDIT_CARD_NUM CHAR(20) NOT NULL,
DATE DATE NOT NULL,
OPERATION_TYPE CHAR(20) NOT NULL,
CONSTRAINT PK_CERDIT_CARD_LOG
PRIMARY KEY (CERDIT_CARD_LOG_NUMBER, CERDIT_CARD_NUM)
);
ALTER TABLE CERDIT_CARD_LOG
ADD CONSTRAINT FK_CERDIT_C_RECORD_CERDIT_C
FOREIGN KEY (CERDIT_CARD_NUMBER)
REFERENCES CERDIT_CARD (CERDIT_CARD_NUMBER);
If you want to create a column with the name DATE in Oracle (reserved word) you need to treat it as a special identifier, and enclose it in double quotes.
You can do:
create table CERDIT_CARD_LOG
(
CERDIT_CARD_NUMBER CHAR(20) not null,
CERDIT_CARD_LOG_NUMBER CHAR(15) not null,
CERDIT_CARD_NUM CHAR(20) not null,
"DATE" DATE not null,
OPERATION_TYPE CHAR(20) not null,
constraint PK_CERDIT_CARD_LOG primary key (CERDIT_CARD_LOG_NUMBER,CERDIT_CARD_NUM )
);
See example at db<>fiddle.
Note: As you see it's possible to use reserved words. I personally avoid it since I would need to remember to enclose them in double quotes in every single query I write in the future. I typically prefer change its name to opened_on or similar.

SQL syntax error while creating table in MariaDB

While creating a table in Mariadb with this code
CREATE TABLE classes(
ClassID SMALLINT UNSIGNED PRIMARY,
Grade TINYINT UNSIGNED,
Subject VARCHAR(20),
YearTaught YEAR
);
I got this error.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
Grade TINYINT UNSIGNED,
Subject VARCHAR(20),
YearTaught YEAR
)' at line 2
And I don't know what's wrong with the syntax. Thank you.
You are missing the keyword KEY on your statement, update
ClassID SMALLINT UNSIGNED PRIMARY,
to
ClassID SMALLINT UNSIGNED PRIMARY KEY,
You could use KEY instead of PRIMARY KEY, but not just PRIMARY:
Use PRIMARY KEY (or just KEY) to make a column a primary key. A primary key is a special type of a unique key. There can be at most one primary key per table, and it is implicitly NOT NULL.
The documentation is here.

Basic syntax for CREATE TABLE in PostgreSQL

I am working with PostgreSQL for the first time. I have this statement:
CREATE TABLE IF NOT EXISTS sequences (
SEQU_NK SERIAL PRIMARY KEY NOT NULL UNIQUE,
NOM_SEQU varchar(30) NOT NULL,
PROCHAIN bigint NOT NULL,
PRIMARY KEY (SEQU_NK),
UNIQUE KEY IDX_SEQU_NOM (NOM_SEQU)
)
When I run it I get:
ERROR: syntax error at or near "KEY"
LINE 6: UNIQUE KEY IDX_SEQU_NOM (NOM_SEQU)
^
********** Error **********
ERROR: syntax error at or near "KEY"
Try like this:
CREATE TABLE IF NOT EXISTS sequences (
SEQU_NK SERIAL PRIMARY KEY NOT NULL UNIQUE,
NOM_SEQU varchar(30) NOT NULL,
PROCHAIN bigint NOT NULL,
CONSTRAINT "IDX_SEQU_NOM" UNIQUE (NOM_SEQU)
)
SQLFIDDLE DEMO
CREATE TABLE sequences (
sequ_nk serial PRIMARY KEY,
nom_sequ varchar(30) NOT NULL UNIQUE,
prochain bigint NOT NULL
);
It's UNIQUE , not UNIQUE KEY.
There can only be one PRIMARY KEY.
A PRIMARY KEY column is UNIQUE and NOT NULL automatically.
Read the manual.
Or, better yet, read the manual for your actual Postgres version 8.4 - which tells you that IF NOT EXISTS is not yet implemented in version 8.4 (wait for 9.1).
Or, better still, upgrade to a current version of Postgres. Version 8.4 has reached EOL last year.
Aside: Don't use quoted upper-case identifiers, or you have to keep quoting them, always.

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);

Error with auto_increment while conneted to Postgres via psql and puTTY

I'm getting this error in puTTY. Not sure why, looks right to me ...
psql:pierre.sql:10: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
^
psql:pierre.sql:18: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
--DROP TABLE customer, reservation;
CREATE TABLE customer(
c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_ref VARCHAR(30) NOT NULL,
f_name VARCHAR(30) NOT NULL,
l_name VARCHAR(30) NOT NULL,
address VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(11) NOT NULL
);
CREATE TABLE reservation(
r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_id VARCHAR(30) NOT NULL REFERENCES customer(c_id),
book_date DATE NOT NULL CHECK (book_date <= now()),
s_time DOUBLE NOT NULL,
e_time DOUBLE NOT NULL,
amount INTEGER NOT NULL
);
Any ideas why?
auto_increment looks like something you'd use with MySQL.
But, here, it seems you are using PostgreSQL.
According to the datatype serial section of the manual, postgresql's equivalent of auto_increment is serial or bigserial.
Quoting that page :
The data types serial and bigserial are not true types, but merely
a notational convenience for setting up unique identifier columns
(similar to the AUTO_INCREMENT property supported by some other databases).
In Postgres 10 or later consider an IDENTITY column:
CREATE TABLE customer(
c_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
...
(PRIMARY KEY also makes it NOT NULL automatically.)
Details:
Auto increment table column
In Postgres 9.6 or older consider a serial like Pascal already suggested.
Works in pg 10 or later, too, but IDENTITY is generally superior.