How to fix syntax error at or near “CREATE” in psql - postgresql

please why is this code not working?
nodelogin-# CREATE TABLE user
nodelogin-# (id BIGSERIAL PRIMARY KEY NOT NULL,
nodelogin(# name VARCHAR(200) NOT NULL,
nodelogin(# email VARCHAR(200) NOT NULL,
nodelogin(# password VARCHAR(200) NOT NULL,
nodelogin(# UNIQUE (email));
ERROR: syntax error at or near "CREATE"
LINE 2: CREATE TABLE user
^

user is a reserved key word in PostgreSQL. Basically, PostgreSQL doesn't like the name. If you try a different name (user2, user_, comp_user) it should work

Related

Why is the format of the sql files affecting if they can run or not in PG?

I have placed a file in my docker-entrypoint-initdb.d/ directory. Here is what is in the file:
CREATE TABLE user_test (
user_id INTEGER,
name VARCHAR(100),
email VARCHAR(128),
active_flg BOOLEAN,
type VARCHAR(20),
CONSTRAINT pk_user PRIMARY KEY (user_id)
);
The error I am getting is psql:/docker-entrypoint-initdb.d/0001-initial-database-design.sql:8: ERROR: syntax error at or near "CREATE".
What am I missing in being able to run a file? How do I change this file to work?
USER is a reserved keyword in Postgres, see the documentation. In general, you should avoid naming your tables and columns using reserved SQL keywords. If you really wanted to proceed as is, then place user into double quotes:
CREATE TABLE "user" (
user_id INTEGER,
name VARCHAR(100),
email VARCHAR(128),
active_flg BOOLEAN,
type VARCHAR(20),
CONSTRAINT pk_user PRIMARY KEY (user_id)
);
But, keep in mind that if you choose to name your table user, then you will forever have to escape it with double quotes.

How to specify the size of a database parameters with schema on PostgreSQL? [duplicate]

This question already has answers here:
How can I set a size limit for an "int" datatype in PostgreSQL 9.5
(3 answers)
Closed 4 years ago.
I tried to initialize a database with a schema.sql file for PostreSQL in Heroku but I have a problem with the size I want to date the parameters of the table. In effect with the line id int (11) auto_increment PRIMARY KEY I get the error:
ERROR: syntax error at or near "("
LINE 2: id int(11) auto_increment PRIMARY KEY,
Here is the file schema.sql
drop table if exists users;
create table users (
id int(11) auto_increment PRIMARY KEY,
name varchar(100),
email varchar(100),
username varchar(30),
password varchar(100),
register_date timestamp not null
);
And here is the error:
mike#mike-thinks:~/Programing/Rasa/myflaskapp$ heroku pg:psql < schema.sql--> Connecting to postgresql-clean-86569
NOTICE: table "users" does not exist, skipping
DROP TABLE
ERROR: syntax error at or near "("
LINE 2: id int(11) auto_increment PRIMARY KEY,
^
NOTICE: table "articles" does not exist, skipping
DROP TABLE
ERROR: syntax error at or near "("
LINE 2: id int(11) PRIMARY KEY,
^
If I'm not wrong (and I'm pretty sure), postgresSQL doesn't accept int sizes but NUMERIC.
You can do columnName NUMERIC(11)
Or with a constraint, forcing to being between ranges

create table in Db2 for iSeries

I have a table structure like this in sql server:
CREATE TABLE [dbo].[taname](
[ID] [char](7) NOT NULL,
[SOURCE] [char](14) NOT NULL,
[TARGET] [char](14) NOT NULL,
[ID1] [char](100) NULL,
)
this similar table I'm trying to create in DB2:
CREATE TABLE schema.taname(
ID char(7) NOT NULL,
SOURCE char(14) NOT NULL,
TARGET char(14) NOT NULL,
ID1 char(100) NULL --error is here
);
However, I'm getting error in "ID":
Keyword NULL not expected. Valid tokens: AS NO FOR NOT FILE WITH CCSID CHECK LOGGED UNIQUE COMPACT. Cause . . . . . : The keyword NULL was not expected here. A syntax error was detected at keyword NULL. The partial list of valid tokens is AS NO FOR NOT FILE WITH CCSID CHECK LOGGED UNIQUE COMPACT. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
Processing ended because the highlighted statement did not complete successfully
I would like to create table similar to SQL Server and allow NULL in the ID field. How can I correct this?
NULL is the default... you can just leave it off...
CREATE TABLE schema.taname(
ID char(7) NOT NULL,
SOURCE char(14) NOT NULL,
TARGET char(14) NOT NULL,
ID1 char(100)
);
alternatively, specify the DEFAULT clause...
CREATE TABLE schema.taname(
ID char(7) NOT NULL,
SOURCE char(14) NOT NULL,
TARGET char(14) NOT NULL,
ID1 char(100) DEFAULT NULL
);

Why SERIAL is not working on this simple table in Postgres?

I'm using Postgres 9.1. and the auto_increment (serial) is not working. I've just found this about 'serial':
https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL
CREATE TYPE FAMILY AS(
id int,
name VARCHAR(35),
img_address VARCHAR(150));
CREATE TABLE FAMILIES of FAMILY(
id SERIAL primary key NOT NULL,
name NOT NULL
);
ERROR: syntax error at or near "SERIAL"
LINE 7: id SERIAL primary key NOT NULL,
^
********** Error **********
ERROR: syntax error at or near "SERIAL"
SQL state: 42601
When you create a table using the syntax:
CREATE TABLE xxx OF yyyy
you can add default values and constraints, but not alter or specify the type of the columns.
The type SERIAL is in effect a combination of a data type, NOT NULL constraint and default value specification. It is equivalent to:
integer NOT NULL DEFAULT nextval('tablename_colname_seq')
See: documentation for SERIAL
So, instead you would have to use:
CREATE SEQUENCE families_id_seq;
CREATE TABLE FAMILIES of FAMILY(
id WITH OPTIONS NOT NULL DEFAULT nextval('families_id_seq'),
name WITH OPTIONS NOT NULL
);
ALTER SEQUENCE families_id_seq OWNED BY FAMILIES.id;
You would have to create the sequence families_id_seq manually as well, as shown above.

Declaring a default constraint when creating a table

I am creating a new table in Microsoft SQL server 2000 by writing the code instead of using the GUI, I am trying to learn how to do it "the manual way".
This is the code I am actually using, and it works fine:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE())
)
I have specified the primary key, foreign key and check constraints on their own because in this way I can define a name for them, otherwise declaring them inline would make SQL Server generate a random name, and I do not "like" it.
The problem arose when I tried to declare the default value constraint: looking at the informations on the internet and how Microsoft SLQ Server Management Studio creates it, I understood that it can be created both inline and on its own:
"load_date" SMALLDATETIME NOT NULL DEFAULT GETDATE()
or
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
The inline method works fine, but it generates as usual a random name for the constaint, the stand alone method throws an error, saying Incorrect syntax near 'FOR'..
Also, if I create the table and then ALTER it, the command works:
ALTER TABLE "attachments"
ADD CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
As a reference, here is the full code I am trying to execute:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE()),
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
)
I'm totally at loss here, is what I am trying to do not possible, or I am doing something wrong?
Edit:
David M showed how to add a named default constraint using the inline syntax, I am still looking to understand if the stand alone syntax is completely wrong or it is my fault.
Do it inline with the column creation:
[load_date] SMALLDATETIME NOT NULL
CONSTRAINT [df_load_date] DEFAULT GETDATE()
I have used square brackets rather than quotes as many readers won't work with QUOTED_IDENTIFIERS on by default.