Postgresql INSERT ON CONFLICT syntax - sql

Greetings with the following test table:
CREATE TEMP SEQUENCE pid start 10000;
SELECT nextval ('pid');
CREATE TEMP TABLE project_test (
id int DEFAULT nextval('pid') primary key,
project_id int UNIQUE NOT NULL
);
I am attempting to make a statement that either inserts a new value for project_id returning the id or returns the id if the project_id exists.
I have attempted the following:
INSERT INTO project_test(project_id) VALUES(50) RETURNING id
ON CONFLICT (project_id) DO UPDATE project_test SET project_id = 50 WHERE project_id = 50 RETURNING id;
I have read the documentation here:
https://www.postgresql.org/docs/13/sql-insert.html
I am on Postgres 13
I am getting a syntax error
ERROR: syntax error at or near "ON"
Thanks in advance for any tips

INSERT INTO project_test(project_id) VALUES(50)
ON CONFLICT (project_id) DO UPDATE SET project_id = 50 WHERE project_test.project_id = 50 RETURNING id;
The problem was the RETURNING id needs to go at the end, and then it thought column reference "project_id" was ambiguous, which is odd.
Thanks all

Related

PostgreSQL conditional select throwing error

I have a PostgreSQL database hosted on Heroku which is throwing me this error that I can't wrap my head around.
CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT, age SMALLINT, right_handed BOOLEAN)
SELECT 1 id FROM people WHERE age IN (1) AND right_handed IN (1)
It gives me the error:
Error in query: ERROR: syntax error at or near "IN"
Don't know how to proceed, any help would be greatly appreciated.
AUTOINCREMENT is not a valid option for CREATE TABLE in Postgres
You can use SERIAL or BIGSERIAL:
ALTER TABLE myTable ADD COLUMN myColumn BIGSERIAL PRIMARY KEY;

What is 'Duplicate key value specified.' error in AS/400 -SQL?

When I ran the below query in AS/400 - SQL , I get an error called 'Duplicate key value specified.'
update Table1
set Field1 = '0'
where Field0 = '0' and Field1 = '8888888'
Can someone please tell me what am I missing?
Here is how the table goes?
Table
I think your field1 column has the primary key constraint. And if you try to update the field with the value which is already present in the table then it will give you the.
ERROR: duplicate key value violates unique constraint "test1_pkey"
Detail: Key (field0)=(1) already exists.
I have reproduced the issue with the help of following steps:
create table test1(field0 varchar(30) primary key, field1 varchar(30));
insert into test1 values('0','10');
insert into test1 values('1','10');
update test1 set field0 = '1' where field1='10' and field0='0';

missing or invalid option , in creating table due to if not exists

i have tried to create a table using if not exists using mysql in sql plus, but i got error
SQL> CREATE TABLE IF NOT EXISTS tasks (
2 todo_id int auto_increment,
3 task_id int);
CREATE TABLE IF NOT EXISTS tasks (
*
ERROR at line 1:
ORA-00922: missing or invalid option
You message indicates that you are using Oracles SQL PLUSand not mysql.,
so use
CREATE TABLE tasks (
todo_id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
task_id NUMBER(5)
);
Please check the manual, for more information

PostgreSQL randomize foreign key

I have 2 tables in Postgres ( inventory table and event table ).
In the inventory table my primary key consist of 8 strings.
Example '03163161'.
Now I have to input them in the Event table randomly with the primary key that is on the inventory table.
In what way can I do it? I've tried googling it but no one seems to have a solution on it and I'm trying to experiment on how i can random input the PK of inventory table to the event table since the data is almost 3k.
Update#1
I've tried using the code below
INSERT INTO dashboard.event(terminal_id) VALUES (SELECT terminal_id FROM dashboard.inventory)
but i get the error:
ERROR: syntax error at or near "SELECT"
LINE 1: INSERT INTO dashboard.event(terminal_id) VALUES (SELECT term...
^
SQL state: 42601
Character: 50
Don't use the keyword VALUES with a select in the insert. See this question:
Update or Insert (multiple rows and columns) from subquery in PostgreSQL
INSERT INTO dashboard.event(terminal_id)
SELECT terminal_id
FROM dashboard.inventory --should work
I think you want the 'references' keyword. For example:
https://www.postgresql.org/docs/current/ddl-constraints.html
create table inventory
(
id INTEGER NOT NULL UNIQUE,
... other fields
);
create table events
(
eid INTEGER REFERENCES inventory(id),
... other fields
);

update postgresql with syntax errors

I have one table called test, which has 4 columns:
id INT
v_out INT
v_in INT
label CHARACTER
I'm trying to update the table with the following query:
String sql = "
update
test
set
v_out = temp.outV
, v_in = temp.inV
, label = temp.label
from (
values
(1,234,235,'[abc]') // these value are read from other places
,(2,234,5585,'[def]') //[abc] = object.toString();
) as temp (e_id, outV, inV, label)
where
id = temp.e_id;
When I execute it, I got the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "["
before get this error, i already update over 3000 rows.
so whats the reason caused this? is it because "[" this character?
this is the original table:
create table edges(
id serial not null primary key,
vertex_out int,
vertex_in int,
label character varying(255),
constraint fk_vertex_out foreign key (vertex_out) references vertices(id) on delete cascade,
constraint fk_vertex_in foreign key (vertex_in) references vertices(id) on delete cascade
);
The most likely problem here is string interpolation in SQL query (it is a very bad practice).
Look at this example:
values
(1,234,235,'[abc]''),
(2,234,5585,'[def]')
The ' symbol in the name of the first object breaches the string boundaries causing ERROR: syntax error at or near "[": on the second line.
You can search for SQL Injection in the internet to get details about this problem.