Having error while executing postgresqlquery - sql

I am facing error in my query while deleting row from two table which have same primary and foreign key :
Query:
DELETE FROM TABLE1 INNER JOIN TABLE2 ON (TABLE1.id=TABLE2.id) WHERE TABLE1.id='21306';
ERROR: syntax error at or near "INNER"
Using rdbms POSTGRESQL

You can't have a join in the from clause of a delete in Postgresql (although this is supported in SQL Server). Any additional table that you want to participate in the delete must be added to the using clause. Try this instead:
DELETE FROM TABLE1
USING TABLE2
WHERE (TABLE1.id=TABLE2.id) AND TABLE1.id='21306';

Related

"WITH AS" Working in Postgres but not in H2 dabatabse

I am writing a single query to insert data into 2 tables using "WITH AS". The query works fine on Postgres but on H2 database it is throwing syntax error.
I have 2 tables.
Table 1 has 2 columns -- a Primary Key table1_ID and a table1_value column.
Table 2 has 3 columns -- a PK table2_Id and table2_value and table1_id as Foreign key.
The query is like this:
WITH ins as (
INSERT INTO table_1 (table1_value) VALUES ("table1_value")
RETURNING table1_ID as t1_id
)
INSERT INTO table_2 (table2_value, tab1_id) VALUES ("table2_value", (SELECT t1_id FROM ins));
This query works fine on Postgres but on H2 DB it throws syntax error with a message
"; expected "(, WITH, SELECT, FROM"; SQL statement
hadatabase reference link:
http://www.h2database.com/html/advanced.html#recursive_queries
http://www.h2database.com/html/commands.html?highlight=insert&search=insert#firstFound
see Compatibility section: https://www.postgresql.org/docs/current/sql-insert.html
INSERT conforms to the SQL standard, except that the RETURNING clause
is a PostgreSQL extension, as is the ability to use WITH with INSERT,
and the ability to specify an alternative action with ON CONFLICT.
Also, the case in which a column name list is omitted, but not all the
columns are filled from the VALUES clause or query, is disallowed by
the standard.

Clickhouse deleting data with WHERE

I am trying to delete some data using WHERE, note that I need 2 tables in order to identify the rows that should be deleted, so I need to join them, I thought of something like:
ALTER TABLE sample_db.test_first_table DELETE WHERE
(
SELECT s.value
FROM sample_db.test_first_table ft
JOIN sample_db.test_second_table st ON (ft.value=st.value)
WHERE `EXPRESSION HERE`
)
I understood that this Alter operation is a mutation, so when checking system.mutations table I see there is this fail reason: Code: 125, e.displayText() = DB::Exception: Scalar subquery returned more than one row
I checked that the expression I am writing is fine with a simple SELECT statement, so I am out of ideas how I can delete multiple rows based on an expression, any help is much appreciated
First of all: MUTATIONS are admin operations. They cannot be used on daily bases.
ALTER TABLE sample_db.test_first_table DELETE WHERE
value in
( SELECT value
from sample_db.test_second_table
WHERE `EXPRESSION HERE`
)

Convert MySQL update SQL statement to Oracle update statement [duplicate]

This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
syntax error with update query when join with some table
(1 answer)
Update with self-join
(3 answers)
Closed 3 years ago.
I have query which is working fine with MySQL, but if I execute it in Oracle database I get the following error:
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly
MySQL query:
UPDATE T1
INNER JOIN T2 ON T1.UIDPK =T2.UIDFK
SET T1.C1=CONCAT('EMPTY_',T2.UID) WHERE T2.C1 IS NULL ;
Changed query for Oracle:
UPDATE
(
SELECT T1.C1 AS OLD ,CONCAT('EMPTY_',T2.UID) AS NEW FROM T1
INNER JOIN T2 ON T1.UIDPK= T2.UIDFK WHERE T1.C1 IS NULL
) T3
SET T3.OLD = T3.NEW
Above query is not working in Oracle database.
The update syntax you are using only works if your version of Oracle decides that the subquery aliased as T3 is an updatable view. You may use a correlated subquery instead of this:
UPDATE T1
SET C1 = (SELECT CONCAT('EMPTY_', T2.UID) FROM T2
WHERE T1.UIDPK = T2.UIDFK AND T2.C1 IS NULL);
You can also check if you have indexes on T2.UIDFK and T1.UIDP columns.
If not, create them and your update might work after that.
Not having indexes on those columns would result in this error / restriction.
Note:
You can always delete those indexes after this update, although it would seem that those are intended as a foreign key and a primary key column, respectively, and it's always good to have created FK and PK constraints on such columns (which would also lead to existing indexes on those columns).

SQL Delete Query Foreign Key constraint

I am trying to Delete record with primary key as foreign key in two tables. Everytime I try to run the query it gives me this error:
Query:
DELETE FROM BusinessPlus_Post
FROM BusinessPlus_Post
INNER JOIN BusinessPlus_PostImage ON BusinessPlus_Post.Post_ID = BusinessPlus_PostImage.BusinessPlusPost_ID
INNER JOIN BusinessPlus_PostTag ON BusinessPlus_Post.Post_ID = BusinessPlus_PostTag.BusinessPlusPost_ID
WHERE
(BusinessPlus_Post.Post_ID = 3)
AND (BusinessPlus_PostImage.BusinessPlusPost_ID = 3)
AND (BusinessPlus_PostTag.BusinessPlusPost_ID = 3)
Error:
The DELETE statement conflicted with the REFERENCE constraint
"FK_BusinessPlusPostImage". The conflict Occurred in database
"BusinessPlus_AdminPanel_Database", table
"dbo.BusinessPlus_PostImage", column 'BusinessPlusPost_ID'. The
statement was terminated.
Right now, you are only stating that you want to delete the BusinessPlus_Post record, but not the BusinessPlus_PostImage and BusinessPlus_PostTag records. This would lead to problems, as we then would have 'orphan' PostImage and PostTag records with no corresponding Post records.
Apparently, it is not possible to delete from multiple tables in SQL Server (it is supported in MySQL, for example).
You have to split your queries, and delete from the 'child' tables first:
DELETE FROM BusinessPlus_PostImage
WHERE BusinessPlusPost_ID = 3
DELETE FROM BusinessPlus_PostTag
WHERE BusinessPlusPost_ID = 3
DELETE FROM BusinessPlus_Post
WHERE Post_ID = 3
Error: The DELETE statement conflicted with the REFERENCE constraint
"FK_BusinessPlusPostImage". The conflict Occurred in database
"BusinessPlus_AdminPanel_Database", table
"dbo.BusinessPlus_PostImage", column 'BusinessPlusPost_ID'. The
statement was terminated.
Error denotes that you have data referencing the foreign ,hence you cannot delete.
Delete the datas in BusinessPlus_AdminPanel_Database table
dbo.BusinessPlus_PostImage, column BusinessPlusPost_ID ,and then try delete

Oracle 10g update table i from table 2 column join error

I have the following Oracle 10g sql which to me looks about right:
update ( select OLD1.TC_CUSTOMER_NUMBER,NEW1.PRD_CUST_NUMBER
FROM TBYC84_PROFILE_ACCOUNT OLD1,
TMP_PRD_KEP NEW1
WHERE
OLD1.TC_CUSTOMER_NUMBER = NEW1.KEP_CUST_NUMBER )
SET
TC_CUSTOMER_NUMBER = PRD_CUST_NUMBER
But i am getting this error when i run the script:
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
I have done done some research on this error but not quite sure how to remedy.
So my question is, how can i fix this or is there a better way to write the update sql?
Any help would be appreciated.
many thanks
UPDATE
I have changed the update sql to this:
update
TBYC84_PROFILE_ACCOUNT PA
set
(
PA.TC_CUSTOMER_NUMBER
) = (
select
TPK.PRD_CUST_NUMBER
from
TMP_PRD_KEP TPK
where
TPK.KEP_CUST_NUMBER = PA.TC_CUSTOMER_NUMBER
)
Now this has updated the TBYC84_PROFILE_ACCOUNT table AND nulled out the TC_CUSTOMER_NUMBER
column.
Why did it do this?
There may be more than one row in the TBYC84_PROFILE_ACCOUNT.TC_CUSTOMER_NUMBER
that has the same account number but for different user_id's.
Please can anyone assist in helping me resolve this.
All I need to to is update the TBYC84_PROFILE_ACCOUNT.TC_CUSTOMER_NUMBER to the one that is xrefed in the TMP_PRD_KEP, surely this is not impossible.
many thanks
For an UPDATE statement, all the columns that are updated must be extracted from a key-preserved table.
Also:
A key-preserved table is one for which every primary key or unique key value in the base table is also unique in the join view.
Here.
In this case, TBYC84_PROFILE_ACCOUNT is being updated. So, it must be key-preserved in the view's subquery. Currently it is not. It must be changed in a way that it becomes key-preserved by involving primary or unique columns in the where clause. If not possible, you should try to update the base table instead.
UPDATE
In case of the table update problem, assuming the subquery returns at most one distinct value for the TC_CUSTOMER_NUMBER column, the reason you get NULLs is that all records are being updated even if they do not have any matching records in the TMP_PRD_KEP table. So, the parent update statement needs to be fitted with a where clause:
update
TBYC84_PROFILE_ACCOUNT PA
set
(
PA.TC_CUSTOMER_NUMBER
) = (
select
TPK.PRD_CUST_NUMBER
from
TMP_PRD_KEP TPK
where
TPK.KEP_CUST_NUMBER = PA.TC_CUSTOMER_NUMBER
)
where exists(select *
from TMP_PRD_KEP TPK
where TPK.KEP_CUST_NUMBER = PA.TC_CUSTOMER_NUMBER)
;
Create a index on the columns used in your where clause predicates. That should solve your problem.