So I'm new to Postgres and am trying to create a table that uses an INHERITS clause.
Can't figure out why I'm getting a syntax error (postgres 9.1):
CREATE TABLE images (
id SERIAL,
description VARCHAR NOT NULL,
filename VARCHAR NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted TIMESTAMP DEFAULT NULL,
PRIMARY KEY(id)
);
CREATE TABLE posts (
id SERIAL,
title VARCHAR(50),
body_text VARCHAR,
image_id INTEGER,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted TIMESTAMP DEFAULT NULL,
PRIMARY KEY(id),
FOREIGN KEY(image_id) REFERENCES images(id)
);
CREATE TABLE events (
date_time DATETIME NOT NULL,
venue VARCHAR(50) NOT NULL,
booking_details VARCHAR NOT NULL,
) INHERITS (posts);
Error
psql:2.sql:30: ERROR: syntax error at or near ")"
LINE 6: ) INHERITS (posts);
^
booking_details VARCHAR NOT NULL,
Remove the comma
Related
I am trying to run a migration script against a sqlite database. Below is the error I am getting when attempting the migration queries:
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
[Error: SQLITE_ERROR: near "categoryId": syntax error] {
errno: 1,
code: 'SQLITE_ERROR'
}
sql query I am running to create tables
CREATE TABLE IF NOT EXISTS "Category" (
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL );
CREATE TABLE IF NOT EXISTS "Location"(
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL );
CREATE TABLE IF NOT EXISTS "Job" (
id INTEGER PRIMARY KEY NOT NULL,
createdAt datetime NOT NULL default current_time,
title VARCHAR NOT NULL,
description VARCHAR NOT NULL,
locationId INTEGER NOT NULL,
FOREIGN KEY(locationId) REFERENCES Location(id),
categoryId INTEGER NOT NULL,
FOREIGN KEY(categoryId) REFERENCES Category(id) );
You need to define all columns before defining the constraints, so swap the 2nd and 3rd last lines in your script:
CREATE TABLE IF NOT EXISTS "Job" (
id INTEGER PRIMARY KEY NOT NULL,
createdAt datetime NOT NULL default current_time,
title VARCHAR NOT NULL,
description VARCHAR NOT NULL,
locationId INTEGER NOT NULL,
categoryId INTEGER NOT NULL,
FOREIGN KEY(locationId) REFERENCES Location(id),
FOREIGN KEY(categoryId) REFERENCES Category(id) );
I have this table definition in pgAdmin4:
CREATE TABLE IF NOT EXISTS cdr_event
(
id bigint primary key generated always as identity,
start_time timestamptz NOT NULL DEFAULT now(),
end_time timestamptz NULL,
group_id VARCHAR(10) NOT NULL,
environment VARCHAR(10) NOT NULL,
level VARCHAR(10) NOT NULL,
schema VARCHAR(30) NOT NULL,
instance INTEGER NOT NULL,
hive_instance_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema||'_'||instance) STORED,
hive_static_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema) STORED,
);
this fails with
ERROR: generation expression is not immutable
SQL state: 42P17
Why does postgres consider the concat mutable when the dependent columns are all NOT NULL? This thread suggests it should work
Is there anyway to create a concat-ed generated column without creating a custom concat function?
Thanks
Try keeping the involved columns of the same type, e.g. casting instance to text should do the trick:
CREATE TABLE IF NOT EXISTS cdr_event
(
id bigint primary key generated always as identity,
start_time timestamptz NOT NULL DEFAULT now(),
end_time timestamptz NULL,
group_id VARCHAR(10) NOT NULL,
environment VARCHAR(10) NOT NULL,
level VARCHAR(10) NOT NULL,
schema VARCHAR(30) NOT NULL,
instance INTEGER NOT NULL,
hive_instance_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema||'_'||instance::text) STORED,
hive_static_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema) STORED
);
Consider using text instead of varchar.
Demo: db<>fiddle
I Got a table BILL which is associated to another table BANK_CARD as follows:
create table BILL (
id_bill BIGSERIAL not null,
id_bank_card BIGSERIAL null,
id_registred_user BIGSERIAL not null,
reference_number INT4 null,
purchase_date DATE null,
bill_status VARCHAR(50) null,
payment_method VARCHAR(50) null,
constraint PK_BILL primary key (id_bill)
);
create table BANK_CARD (
id_bank_card BIGSERIAL not null,
id_registred_user BIGSERIAL not null,
card_type VARCHAR(50) null,
card_number INT4 null,
expiring_date DATE null,
cipher INT4 null,
constraint PK_BANK_CARD primary key (id_bank_card)
);
The table BILL has a 0..1 association with the table BANK_CARD, and BANK_CARD has a 1..n association with table BILL.
But when i execute my sql script i get the following error:
conflicting NULL/NOT NULL declarations for column "id_bank_card" of table "bill"
Because the relationship BILL and BANK_CARD is 0..1 the foreign key id_bank_card can be null in the table bill, so i don't understand why i get this error.
Any help please?
Thanks.
You are confusing data type definitions for primary and foreign keys. A bigserial is a generator of values of the bigint type. The foreign keys should use that data type. See table definitions below. Also, use of the NULL clause is redundant as that is the default behaviour. Primary keys can not be NULL so NOT NULL there is also redundant.
create table bank_card (
id_bank_card bigserial,
id_registred_user bigint references <user table>,
card_type VARCHAR(50),
card_number INT4,
expiring_date DATE,
cipher INT4,
constraint PK_BANK_CARD primary key (id_bank_card)
);
create table bill (
id_bill BIGSERIAL,
id_bank_card bigint references bank_card,
id_registred_user bigint references <user table>,
reference_number INT4,
purchase_date DATE,
bill_status VARCHAR(50),
payment_method VARCHAR(50),
constraint pk_bill primary key (id_bill)
);
If you are using Java+PostgreSQL:
this can happen if you accidentially insert null strings into your statement.
String N = System.getProperty("line.seperator");
Returned null, screwing up my sql statement.
FIXES: (Choose One)
String N = System.lineSeparator();
String N = "\n";
Exact string as it appeared in my code:
String CT =(""
//:+"---------10--------20--------30-------39
//:+"0123456789012345678901234567890123456789
+"CREATE TABLE t_1( "+N//:0
+"id SERIAL PRIMARY KEY "+N//:30
+" ,c_1 INT "+N//:60
+" ,c_2 VARCHAR (255) "+N//:90
+" ,c_3 CHARACTER( 8 ) "+N//:120
+" ,c_4 BOOLEAN "+N//:150
+" ,c_5 BYTEA "+N//:180
+" ,c_6 DATE "+N//:210
+"); " //:240
);;
Exact Error Message when N==null instead of "\n":
org.postgresql.util.PSQLException:
ERROR: conflicting NULL/NOT NULL declarations
for column "nullid" of table "t_1"
Stack: Java+Heroku+Tomcat9+PostGreSQL 9.5.13
I try to create MS Access Table with autoincrement ID and Default Date field, but next query always says "Syntax error in CREATE TABLE statement.":
CREATE TABLE Table1
(
[ID] AUTOINCREMENT,
[Email] TEXT(255),
[ProductID] NUMBER,
[DateCreate] DATETIME,
[DateSend] DATETIME
);
ALTER TABLE Table1
ALTER [DateSend] DATETIME DEFAULT NOW() NOT NULL;
Who can help me to fix that query. Thanks!
There are many NUMBER types in Ms-Access, so you have to be specific. I guess you want Integer.
CREATE TABLE Table1
(
[ID] AUTOINCREMENT,
[Email] TEXT(255),
[ProductID] INTEGER,
[DateCreate] DATETIME,
[DateSend] DATETIME
);
The ALTER TABLE syntax requires ALTER COLUMN :
ALTER TABLE Table1
ALTER COLUMN
[DateSend] DATETIME DEFAULT NOW() NOT NULL;
You could also have those two in one statement:
CREATE TABLE Table1
(
[ID] AUTOINCREMENT,
[Email] TEXT(255),
[ProductID] INTEGER,
[DateCreate] DATETIME,
[DateSend] DATETIME DEFAULT NOW() NOT NULL
);
It's best practise to have a PRIMARY KEY on every table, and you probably intended that for the ID:
[ID] AUTOINCREMENT PRIMARY KEY,
A page with a lot of useful information about how to handle Access with SQL:
Intermediate Microsoft Jet SQL for Access 2000
CREATE TABLE Tblcontact
(
contactid AUTOINCREMENT PRIMARY KEY ,
firstname CHAR (60),
lastname CHAR (60),
email VARCHAR (75)
);
New to SQLite so I don't know what I'm doing wrong. I'm just getting an error saying:
SQLSTATE[HY000]: General error: 1 near "CREATE": syntax error
Here's my SQL:
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY,
date_created DATETIME NOT NULL,
date_updated DATETIME NOT NULL,
username VARCHAR(32) NOT NULL,
password VARCHAR(32) NOT NULL,
role VARCHAR(32) NOT NULL DEFAULT 'member',
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(128) NOT NULL
)
CREATE TABLE subscribers (
id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(40) DEFAULT NULL,
email VARCHAR(255) NOT NULL UNIQUE
)
CREATE TABLE weekly_download (
id INTEGER NOT NULL PRIMARY KEY,
filename TEXT NOT NULL,
download_date DATE NOT NULL,
body TEXT
)
put a semicolon after each statement.
CREATE TABLE ( ... ) ;
CREATE TABLE ( ... ) ;
Start with simple statements using the sqlite3 CLI.
Then, if you forget a ;, you will get quick feedback and can build up to more complex SQL.
$ sqlite3 /tmp/test.db
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> create table badsyntax;
SQL error: near ";": syntax error
sqlite> create table abc (x,y);
sqlite>
Don't forget semi-colons!