I have 2 tables. tablea is parent and tableb is child and associated with scode
tablea
scode
rcode
sabbr
sname
tablea : primar key : scode
tableb
aid
scode
acode
aname
tableb : primary key : aid. This is AUTO GENERATED
st_ar_UX(scode,acode)
st_ar_fk(scode)
I am using spring batch+spring data jpa to insert records into tableb only (That's my REQUIRMENT).
When the spring batch is completed, I see 3 rows inserted which are below as scode, scode, aname into tableb. But I get an errors saying
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (st_ar_UX) violated. Why would this come and how to resolve.
01|001|AUTAUGA
01|003|BALDWIN
01|005|BARBOUR
Related
I have two tables:
table1:
"table1_pkey" PRIMARY KEY, btree (id)
"table1_user_id" btree (user_id)
"table1_active" btree (bool_to_int(active))
...
Referenced by:
TABLE "table2" CONSTRAINT "my_id" FOREIGN KEY (my_id) REFERENCES table1(id) DEFERRABLE INITIALLY DEFERRED
table2:
"table2_pkey" PRIMARY KEY, btree (id)
"table2_my_id" btree (my_id)
...
"my_id" FOREIGN KEY (my_id) REFERENCES table1(id)
DEFERRABLE INITIALLY DEFERRED
I'm trying to perform the following statement:
delete from table2 using table1 where user_id = 3
and my_id = any(array[1, 2, 3])
What do I expect:
table2 and table1 are joined using a foreign key my_id <-> id
Rows with user_id = 3 from table1 and all the my_ids from given array from table2 are selected for delete.
Rows are deleted.
However, what happens is that this statement deletes all the rows which pass on part of the condition with my_id = any(array[1, 2, 3]) completely ignoring the user_id = 3 part. Causing a delete of records of other users.
So the question is: why does delete with using clause is not working as expected? What am I missing?
I already figured out a more simple solution to this problem which seem to be working as expected:
delete from table2 where my_id in (select my_id from table2 join table1 on table1.id = alert_id where user_id = 3 and my_id = any(array[1, 2, 3]))
But the original question still remains.
Given:
Table A with multiple rows and attributes: (A_attr1 (key) , A_attr2).
Table B with attributes (B_attr1 (key) , A_attr1 (foreign key), B_attr2).
How do I insert some values in the table B only if the foreign key exists?
In Postgres, we can use the Where Exists to implement your use case.
Here is an example of using it.
Insert Into Table_B Select 'Value 1', 'Foreign Value', 'Value 2' Where Exists
(Select 1 From Table_A Where A_attr1 = 'Foreign Value');
This will insert only if the "Foreign Value" is present in Table_A.
Hope this helps
First, we need to consider the fact that the condition (existence of foreign key in table A) is fundamental, in fact, if we try to add values in Table_B with an A_attr1 that it doesn't exist in Table_A we get an error of this type:
ERROR: the INSERT or the UPDATE on the TABLE table_B violates the foreign key constraint
"_A_attr1_"
DETAIL: the key (A_attr1)=(wrong_value) it's not present in the table "Table_A"
This is a possible solution:
INSERT INTO Table_B(B_attr1, A_attr1, B_attr2)
SELECT x.*
FROM (VALUES
(something,something_else, something_else2),
(something_else3,something_else4, something_else5),
...
(something_else20, something_else21,something_else22)
) x(B_attr1, A_attr1, B_attr2)
WHERE EXISTS(
SELECT FROM TABLE_A y
WHERE (y.A_attr1)=(x.A_attr1)
FOR SHARE);
The result is the addition in B of all the tuples that are acceptable (that is the one with the existing foreign keys).
This post is an extension of the following question:
PostgreSQL insert if foreign key exists
The solution is based on the comments on this post:
https://dba.stackexchange.com/questions/252875/how-to-make-on-conflict-work-for-compound-foreign-key-columns/252925#252925
I have a situation with two tables where one has a foreign key pointing to the other table (simplified) schema:
CREATE TABLE table1 (
name VARCHAR(64) NOT NULL,
PRIMARY KEY(name)
);
CREATE TABLE table2 (
id SERIAL PRIMARY KEY,
table1_name VARCHAR(64) NOT NULL REFERENCES table1(name)
);
Now I regret using the name column as primary key in table1 - and would like to add integer serial key instead. Since I already have data in the database I guess I need to do this carefully. My current plan is as follows:
Drop the foreign key constraint: table2(name) with ALTER TABLE table2 DROP CONSTRAINT table2_table1_name_fkey;
Drop the primary key constraint on table1(name) with ALTER TABLE table1 DROP CONSTRAINT name_pkey;.
Add a unique constraint on table1(name) with ALTER TABLE table1 ADD UNIQUE(name);
Add a automatic primary key to table1 with ALTER TABLE table1 ADD COLUMN ID SERIAL PRIMARY KEY;.
Add a new column table1_id to table2 with ALTER TABLE table2 ADD COLUMN table1_id INT;
Update all rows in table2 - so that the new column (which will be promoted to a foreign key) gets the correct value - as inferred by the previous (still present) foreign key table1_name.
I have completed steps up to an including step 5, but the UPDATE (with JOIN?) required to complete 6 is beyond my SQL paygrade. My current (google based ...) attempt looks like:
UPDATE
table2
SET
table2.table1_id = t1.id
FROM
table1 t1
LEFT JOIN table2 t2
ON t2.table1_name = t1.name;
You do not need JOIN in UPDATE.
UPDATE
table2 t2
SET
table1_id = t1.id
FROM
table1 t1
WHERE
t2.table1_name = t1.name;
I have three tables on postgresql DB, and tried to update table but I failed to get result what I want. Please help me with getting valid result.
The first table is "employee".
On this table, the first three characters of "employee_id" mean employee type.
For example, employee_id="AA1-11111" is a member of employee_type="AA1".
employee_id
department
AA1-11111
A
AA1-22222
B
AB1-11111
A
The second table is "assessment".
On this table, assessment criteria is defined for (employee_type, department).
For example, an employee of employee_type="AA1" and department="A" will be evaluated by assessment_criteria="XX1X".
employee_type
department
assessment_criteria
AA1
A
XX1X
AA1
B
XX1Y
AA2
A
XX2X
The third table is "employee_assessment". On this table assessment_criteria for each employee is defined. (This table is calculated from "employee" and "assessment" by night batch processing.)
employee_id
department
assessment_criteria
AA1-11111
A
XX1X
AA1-22222
B
XX1Y
AB1-11111
A
Null
What I want to do is... to update "employee_assessment" table when "assessment" table is updated.
When "assessment" table is updated as like below...
employee_type
department
assessment_criteria
AA1
A
XX1X
AA1
B
NEW
AA2
A
Null
I want to update "employee_assessment" table like this.
employee_id
department
assessment_criteria
AA1-11111
A
XX1X
AA1-22222
B
NEW
AB1-11111
A
Null
I tried
UPDATE
employee_assessment
SET assessment_criteria=employee_assessment.assessment_criteria
FROM employee
LEFT JOIN (SELECT employee_id, LEFT(employee_id,3) as emp_type, department as emp_department from employee) as t1
ON
employee.employee_id=t1.employee_id
and
employee.department=t1.emp_department
left join assessment
on
t1.emp_type=assessment.employee_type
and
t1.emp_department=assessment.department;
But I got this result.
employee_id
department
assessment_criteria
AA1-11111
A
XX1X
AA1-22222
B
XX1X
AB1-11111
A
XX1X
My query seems to be wrong.
The actual cause of the problem is that you schema isn't properly normalized. Therefore you should solve this by fixing and normalizing your schema. Then you can simply use a view, that is "updated" automatically.
First have tables for the types and departments (unless you have that already (that's unclear)).
CREATE TABLE type
(id serial,
name varchar(64),
PRIMARY KEY (id));
CREATE TABLE department
(id serial,
name varchar(64),
PRIMARY KEY (id));
Then, in the table for the employees, just reference the types and departments. Don't have a column that actually are two columns, i.e. the type id has to have its own column and must not be concatenated to any other.
CREATE TABLE employee
(id serial,
type integer,
department integer,
given_name varchar(64),
surname varchar(64),
PRIMARY KEY (id),
FOREIGN KEY (type)
REFERENCES type
(id),
FOREIGN KEY (department)
REFERENCES department
(id));
In the table for the assessments reference the types and departments too.
CREATE TABLE assessment
(id serial,
type integer,
department integer,
name varchar(64),
criteria varchar(64),
PRIMARY KEY (id),
FOREIGN KEY (type)
REFERENCES type
(id),
FOREIGN KEY (department)
REFERENCES department
(id));
Now you can create view for the employee assessments that joins the data from the other tables and is always up to date. There's no need for any manual UPDATE.
CREATE VIEW employee_assessment
AS
SELECT e.id employee_id,
e.department employee_department,
a.criteria assessment_criteria
FROM employee e
LEFT JOIN assessment a
ON a.type = e.type
AND a.department = e.department;
A view also has the advantage that it cannot contain inconsistent data as the table you have now could.
Hi I have a Postgres table named Light which has 4 columns as objects.
I am trying to write some sample data but getting error as below, referred similar question too, my syntax looks correct.
Can someone help me where i am missing ?
INSERT INTO default$default."light"
VALUES ('sid',
'(1,2,3)',
'model123456',
(SELECT id
FROM default$default."lightstate"
WHERE id = 'cjy6y1iv500ag0721x7abj1um'),
'SouthZone',
(SELECT id
FROM default$default."flashstate"
WHERE id = 'cjy5u9li4006u0721ae6ul1ka'),
(SELECT id
FROM default$default."lightgrpctrl"
WHERE id = 'cjy5ugpy500 7r07213minpvqw'),
(SELECT id
FROM default$default."powermeter"
WHERE id = 'cjy5tcqkv004z07219knfw9z6'));
ERROR: insert or update on table "Light" violates foreign key constraint "Light_state_fkey"
DETAIL: Key (state)=(cjy5u9li4006u0721ae6ul1ka) is not present in table "LightState".
Schema as below,