SQL using nested INSERT INTO's? - sql

I am using postgreSQL for storing chat logs, and I have this sample schema:
CREATE TABLE contacts (
"id" BIGSERIAL PRIMARY KEY,
"user" BIGINT NOT NULL,
"contact" BIGINT NOT NULL,
"savedAs" VARCHAR(36),
CONSTRAINT user_fk FOREIGN KEY("user") REFERENCES users("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT contact_fk FOREIGN KEY("contact") REFERENCES users("id") ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE("user", "contact")
);
CREATE TABLE messages (
"id" BIGSERIAL PRIMARY KEY,
"contact" BIGINT NOT NULL,
"direction" direction_type NOT NULL,
"type" message_type default 'text',
"body" VARCHAR(1000) NULL,
"status" status_type DEFAULT 'none',
"time" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT contact_fk FOREIGN KEY("contact") REFERENCES contacts("id") ON DELETE CASCADE
);
CREATE TABLE last_message (
"id" BIGSERIAL PRIMARY KEY,
"chat" BIGINT NOT NULL UNIQUE,
"message" BIGINT NOT NULL,
CONSTRAINT message_fk FOREIGN KEY("message") REFERENCES messages("id"),
CONSTRAINT chat_fk FOREIGN KEY("chat") REFERENCES contacts("id") ON DELETE CASCADE
);
What I want to do, is store the last message for a particular chat in the last_message table. I was thinking of doing it like this(but not working):
INSERT INTO last_message (chat, message) VALUES (
9,
(INSERT INTO messages (contact, direction, body) VALUES (9, 'sent', 'hello there') RETURNING id)
)
But I get a syntax error(syntax error at or near "into"), so here are my questions,
what is wrong with the above query?
is there a better a way to do this? how?
is there anything that can be improved?

Use a CTE:
WITH toinsert as (
INSERT INTO messages (contact, direction, body)
VALUES (9, 'sent', 'hello there')
RETURNING id
)
INSERT INTO last_message (chat, message)
SELECT 9, id
FROM toinsert;

Step 1
drop table last_message
You can get the last message like this:
select contact, direction, body
from messages
join
(select chat, max(time) maxTime
from messages
group by chat
) temp on messages.chat = temp.chat
and time = maxTime
You can create a view or whatever with this logic, whatever your requirements happent to be.

Related

PostgreSQL cyclic foreign keys to multiple tables

I have 3 tables.
chat_room
create table chat_room
(
id uuid default uuid_generate_v4() not null
constraint chat_room_pk
primary key
constraint fk__chat_room__group_chat_room
references group_chat_room
on update cascade on delete cascade
constraint fk__chat_room__private_chat_room
references private_chat_room
on update cascade on delete cascade,
name varchar(255) not null,
description varchar(255),
profile_pic varchar(128),
created_at timestamp with time zone default now() not null,
updated_at timestamp with time zone
);
group_chat_room
create table group_chat_room
(
id uuid not null
constraint group_chat_room_pk
primary key
constraint fk__group_chat_room___chat_room
references chat_room
on update cascade on delete cascade,
pus_code char(7) not null
constraint fk__group_chat_room__puskesmas
references puskesmas
on update cascade on delete cascade
);
create unique index group_chat_room_pus_code_uindex
on group_chat_room (pus_code);
private_chat_room
create table private_chat_room
(
id uuid not null
constraint private_chat_room_pk
primary key
constraint fk__private_chat_room__chat_room
references chat_room
on update cascade on delete cascade
);
As you can see chat_room has foreign key constraints that refer to group_chat_room and private_chat_room. Also both group_chat_room and private_chat_room have FK constraints that refer to chat_room.
When I want to INSERT a row into group_chat_room I would use this
with chat_room as (
insert into chat_room (id, name) values ('Some ID', 'Some Name')
)
insert into group_chat_room(id, pus_code) values ('Some ID', 'Some Code');
However because of those constraints this would produce an error
[23503] ERROR: insert or update on table "chat_room" violates foreign key constraint "fk__chat_room__private_chat_room" Detail: Key (id)=(cef8c655-d46a-4f63-bdc8-77113b1b74b4) is not present in table "private_chat_room".
How do I only insert to group_chat_room without having to insert it to private_chat_room?
The main problem here is creating multiple required bidirectional foreign keys. You can probably work around it, eventually. But it makes the data model more complicated and the code much more so. And it is completely unnecessary. Everything you have can be accomplished with just 1 table. If group_chat_room and private_chat_room are independently required then create a view for each. Further, as simple views, they are fully update-able.
You accomplish this by moving the column 'pus_code' to chat_room and adding 2 boolean values to indicate if this is a private or group room or both. Yes, strange as it sounds you can get a private_group_chat_room. (Note: there is nothing in your design preventing it and the error you are getting is because it is required). If you do want that then create a check constraint requiring at least 1 of the boolean columns be false.
create table chat_room
(
id integer generated always as identity
constraint chat_room_pk
primary key,
name varchar(255) not null,
description varchar(255),
profile_pic varchar(128),
is_private boolean not null default false,
is_group boolean not null default false,
pus_code varchar(7)
constraint fk__group_chat_room__puskesmas
references puskesmas
on update cascade on delete cascade,
created_at timestamp with time zone default now() not null,
updated_at timestamp with time zone,
constraint not_group_of_pus_code_check
check ( (not is_group and pus_code is null)
or (is_group and pus_code is not null)
)
);
-- create unique partial index
create unique index group_chat_room_pus_code_uindex on chat_room(pus_code)
where is_group;
-- group_chat_room
create view group_chat_room
( id
, name
, description
, profile_pic
, is_private
, pus_code
, created_at
, updated_at
) as
select id
, name
, description
, profile_pic
, is_private
, pus_code
, created_at
, updated_at
from chat_room
where is_group;
-- private_chat_room
create view private_chat_room
( id
, name
, description
, profile_pic
, is_group
, pus_code
, created_at
, updated_at
) as
select id
, name
, description
, profile_pic
, is_group
, pus_code
, created_at
, updated_at
from chat_room
where is_private;
See fiddle for full example and a few tests. Note: fiddle has an issue with generate_uuid_v4() (did not exist) so for demo I changed to identity. It will fine in an operational environment.

Fill in bridge table with query

I have four tables, the main purpose of these tables is to have a many to many keyword to message relationship. each keyword can have many messages and each message can have many keywords they are related together if the category id matches.
CREATE TABLE public.trigger_category
(
id integer NOT NULL DEFAULT nextval('trigger_category_id_seq'::regclass),
description text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT trigger_category_id PRIMARY KEY (id)
)
CREATE TABLE public.trigger_keyword
(
id integer NOT NULL DEFAULT nextval('trigger_keyword_id_seq'::regclass),
keyword text COLLATE pg_catalog."default" NOT NULL,
category_id bigint NOT NULL,
CONSTRAINT trigger_keyword_id PRIMARY KEY (id),
CONSTRAINT trigger_keyword_category_id_fkey FOREIGN KEY (category_id)
REFERENCES public.trigger_category (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE NO ACTION
)
CREATE TABLE public.trigger_message
(
id integer NOT NULL DEFAULT nextval('trigger_message_id_seq'::regclass),
message text COLLATE pg_catalog."default" NOT NULL,
category_id bigint NOT NULL,
CONSTRAINT trigger_message_id PRIMARY KEY (id),
CONSTRAINT trigger_message_category_id_fkey FOREIGN KEY (category_id)
REFERENCES public.trigger_category (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
CREATE TABLE public.trigger_keyword_trigger_message
(
trigger_keyword_id bigint NOT NULL,
trigger_message_id bigint NOT NULL,
CONSTRAINT trigger_keyword_trigger_message_trigger_keyword_id_trigger_mess PRIMARY KEY (trigger_keyword_id, trigger_message_id),
CONSTRAINT trigger_keyword_trigger_message_trigger_keyword_id_fkey FOREIGN KEY (trigger_keyword_id)
REFERENCES public.trigger_keyword (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE NO ACTION,
CONSTRAINT trigger_keyword_trigger_message_trigger_message_id_fkey FOREIGN KEY (trigger_message_id)
REFERENCES public.trigger_message (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE NO ACTION
)
I manually insert keywords in the trigger_keyword table and I manually insert messages in the trigger_message table, if they are related then they would get the same category_id
Is it possible to write a query that would automatically go through the rows and if a keyword and message have the same category_id then it would create all the appropriate rows for the bridge table trigger_keyword_trigger_message?
You could achieve this with an Oracle Merge Query.
The USING clause selects all records to insert, and the WHEN MATCHED does to inserts in the bridge table.
MERGE INTO trigger_keyword_trigger_message tktm
USING (
SELECT tk.id tk_id, tm.id tm_id
FROM
trigger_keyword tk
INNER JOIN trigger_message tm on tm.category_id = tk.category_id
) us
WHEN MATCHED THEN
INSERT (tktm.trigger_keyword_id, tktm.trigger_message_id)
VALUES (us.tk_id, us.tm_id)
;

Second table does not get inserted if first table already exist

I have a user and a log table. I want to keep adding new log, and if the user name does not exist in user, add that too.
What I have however, will only insert into log if user does not exist. If user already exist, no log gets added.
How can I fix this to make it work as intended? Thanks in advance.
CREATE TABLE user (
id serial NOT NULL,
name char(60) NOT NULL,
CONSTRAINT user_pk PRIMARY KEY (id),
CONSTRAINT user_un UNIQUE (name)
)
CREATE TABLE log (
id serial NOT NULL,
name_id int NOT NULL,
detail char(512) NULL,
CONSTRAINT detail_pk PRIMARY KEY (id),
CONSTRAINT detail_user_fk FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE RESTRICT ON UPDATE RESTRICT
)
my attempt
with ins1 as (
insert into user (name)
values ('myname')
on conflict do nothing
returning id as user_id
)
insert into detail (user_id, detail)
select user_id, 'some detail' from ins1;
Following example from other question I changed do nothing to update where false, but still no log is being inserted
with ins1 as (
insert into "user" (name)
values ('myuser')
on conflict (name) do update
set name = null where FALSE
returning id as user_id
)
insert into log (user_id, detail)
select user_id, 'some description' from ins1;

Inheritance PostgreSQL

I have 2 tables under PostgreSQL:
a table property that is the mother table (id, created_at ..) and
a habitation table (surface, room, etc...) that inherits property. On the mother table, I have a many-many relationship table. I have added results in the "habitation" table.
When I try to insert "property_table" with habitation IDs, I have a SQL error telling me that the property_id ID does not exist. It exists when I go to see the property or habitation table. I want to insert habitation IDs (so property) in property_tag , should I force insert? Help please
The SQL schemas :
CREATE TABLE "property"
(
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
address_id int NOT NULL,
permission int NOT NULL DEFAULT 6,
user_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (address_id) REFERENCES "address"(id),
FOREIGN KEY (user_id) REFERENCES "user"(id)
);
CREATE TABLE habitation (
total_surface float,
living_surface float,
ground_surface float,
ground_surface_unity character varying(15) DEFAULT 'm2',
room integer,
bedroom integer,
floor integer,
level integer,
year integer
) INHERITS (property);
CREATE TABLE property_tag
(
tag_id int NOT NULL,
property_id int NOT NULL,
PRIMARY KEY (tag_id, property_id),
FOREIGN KEY (tag_id) REFERENCES "tag"(id) ON DELETE CASCADE,
FOREIGN KEY (property_id) REFERENCES "property"(id) ON DELETE CASCADE
);
CREATE TABLE "tag"
(
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
name character varying(127) NOT NULL,
user_id int NOT NULL,
color character varying(7) NOT NULL DEFAULT 'A4A4A8',
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
);
When you insert into table "habitation", the row exists physically only in table "habitation", but not in "property".
Your table "property_tag" references to table "property" (which have no rows, cause rows are in table "habitation") and therefore you are getting error during insert.
You have to change your reference in table "property_tag" from "property" to "habitation".

Trouble with composite foreign keys in sqlite3

there! I have a failing foreign key constraint in sqlite3 and I really have no idea why. I'm using a composite foreign key as discribed here (http://www.sqlite.org/foreignkeys.html#fk_composite) and a "normal" foreign key.
Here are my schemas:
sqlite> .schema sources
CREATE TABLE sources(
source_id VARCHAR(16) NOT NULL,
source_type INTEGER NOT NULL,
title VARCHAR(128) NOT NULL,
year INTEGER,
month INTEGER,
PRIMARY KEY(source_id),
UNIQUE(title)
);
sqlite> .schema author_aliases
CREATE TABLE author_aliases(
author_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
forenames VARCHAR(128),
surname VARCHAR(128) NOT NULL,
PRIMARY KEY(author_id, alias_id),
FOREIGN KEY(author_id) REFERENCES authors(author_id),
UNIQUE(forenames, surname)
);
sqlite> .schema alias_source_relations
CREATE TABLE alias_source_relations(
source_id VARCHAR(16) NOT NULL,
author_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
PRIMARY KEY(source_id, author_id, alias_id),
FOREIGN KEY(source_id) REFERENCES sources(source_id),
FOREIGN KEY(author_id, alias_id) REFERENCES author_aliases(author_id, alias_id)
);
Here is the data the foreign key is referring to:
sqlite> SELECT * FROM sources WHERE source_id='Allen1980';
Allen1980|0|The definition of electronegativity and the chemistry of the noble gases|1980|
sqlite> SELECT * FROM author_aliases WHERE author_id=1 and alias_id=1;
1|1|Leland C.|Allen
sqlite> SELECT * FROM authors WHERE author_id=1;
1|Leland Cullen|Allen
And here is my insert:
sqlite> INSERT INTO alias_source_relations VALUES(1, 1, 'Allen1980');
Error: foreign key constraint failed
Does anyone know what I am missing? Thanks for your help!
Regards,
Marian
Check column order!
INSERT INTO alias_source_relations VALUES('Allen1980', 1, 1);
The order of the inserted values does not match the column order as defined in the table schema.
It's safer and also more descriptive to include the column names. Like this, the order does not need to match the one in the table schema and someone who does not know the table schema by heart still understands the command:
INSERT INTO alias_source_relations (source_id, author_id, alias_id)
VALUES ('Allen1980', 1, 1);