Basic syntax for CREATE TABLE in PostgreSQL - sql

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.

Related

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

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.

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.

PostgresQL Foreign Key Syntax Error

When attempting to create the second table in this respective database, I'm getting the following error message:
ERROR: syntax error at or near "REFERENCES"
LINE 3: master_directory REFERENCES auth_table (directory),
Here's the database structure that I attempted to create:
CREATE TABLE auth_table (
id SERIAL PRIMARY KEY,
directory VARCHAR,
image VARCHAR
)
CREATE TABLE master_table (
id SERIAL PRIMARY KEY,
master_directory references auth_table (directory),
master_image references auth_table (image)
)
Any reason why I'm receiving that error? Any help would be appreciated!
You've left the data type off, but that syntax error is the least of your problems.
Your foreign key references need to refer to unique column(s). So "auth_table" probably needs to be declared one of these ways. (And you probably want the second one, if your table has something to do with the paths to files.)
CREATE TABLE auth_table (
id SERIAL PRIMARY KEY,
directory VARCHAR not null unique,
image VARCHAR not null unique
);
CREATE TABLE auth_table (
id SERIAL PRIMARY KEY,
directory VARCHAR not null,
image VARCHAR not null,
unique (directory, image)
);
Those unique constraints mean quite different things, and each requires a different foreign key reference. Assuming that you want to declare "auth_table" the second way, "master_table" probably ought to be declared like one of these. (Deliberately ignoring cascading updates and deletes.)
CREATE TABLE master_table (
master_directory varchar not null,
master_image varchar not null,
primary key (master_directory, master_image),
foreign key (master_directory, master_image)
references auth_table (directory, image)
);
CREATE TABLE master_table (
id integer primary key,
foreign key (id) references auth_table (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.

SQL Server 2008 R2. Incorrect syntax near 'AUTO_INCREMENT'

Why do i get the following error
Incorrect syntax near 'AUTO_INCREMENT'.
while trying to execute
CREATE TABLE Person
(
P_Id int NOT NULL AUTO_INCREMENT,
Name varchar(255),
PRIMARY KEY (P_Id)
)
What is the correct syntax?
CREATE TABLE Person(
P_Id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name varchar(255))
You should explicitly state whether NAME is NULL or NOT NULL so you are not dependant upon the current connection settings that happen to be in effect.
create table Person
(
PersonId int identity(1,1)
constraint PK_Person primary key,
Name varchar(255) not null
)
Some comments:
It is not needed to specify not null for identity column as identity column cannot be nullable. ANSI_NULL_DFLT_ON option does not affect 'nullability' of identity column.
On other hand it is important to specify 'not null / null' for Name column, as it will be affected by ANSI_NULL_DFLT_ON value.
It is always a good idea to explicitly specify names for constraints. Because if you don't, name constraint name will be generated. If you need to delete the constraint later, you will have to find out the auto-generated name.