Can we update same rows in two tables in postgresql? - postgresql

Is it possible to update a row in two tables in Postgresql?
The table names are patron and cir_transaction
The column name is patron_id
I need to update the same rows of patron_id which is present in both above tables
I wrote like below
update patron p, cir_transaction c
set patron_id = '4BW14MBA10'
where patron_id = '4BW14MBA10 ' and c.patron_id = patron_id

Related

is it possible to copy data from table to another based off if data in one table matches the data in the other table in a specific column

I have a table A with the following columns and data:
Tag_ = P-111
Description_ = Pump
HP_ = 100
RPM_ = 1,800
I have another table B with same columns:
Tag_ = P-111
Description_
HP_
RPM_
Is there a way when I enter data in the Tag_ column in Table B and there is matching data in the same Tag_ column in Table A that I can set up a trigger or automatic command that sends the data from the other columns in Table A to Table B?

Building a query which sets a column according to the data in a join table

I have a table af with columns af.id, etc. and a table af_pb with columns af_id and pb_id (which assigns entities from table pb to the entities of table af).
What i want:
add a new column precedence in table af
for each af.id in af:
if there is a pair (af_id, pb_id) with af.id = af_id and some pb_id in the join table af_pb, then set af.precedence = 0
if there is no such pair, set af.precedence = 1
How can i reach this in PostgreSQL? I already read about the case-when-else-statement but I didn't managed to implement it such that the column precedence is set correctly.
While this can be done in with a case expression, it is not necessary. If you want a default value for later inserts into table af then alter the table with it, then update to set the non-default.
alter table af add column precedence integer default 1;
update af
set precedence = 0
where exists (select null
from af_pb
where af.af_id = af_pb.af_id);
If a default is not desired then a just add the column and afterward update to set the appropriate value:
alter table af add column precedence integer;
update af
set precedence =
( not (exists (select null
from af_pb
where af.af_id = af_pb.af_id)))::integer;

How can I insert a a row with first ID in non empty table?

I have a table with autoincrement id. This table is non empty.
I need to update my table for insert a new row with id 1.
How can I move my entire table one row down ?
My table :
Name : rem_taux
Column : rtx_id | rtx_code | rtx_taux | rtx_date
Thanks.
three steps:
update rem_taux set rtx_id = rtx_id + 1; to move rows down
alter sequence restart with next_val (or just select nextval)
insert you row with not default rtx_id value
like this:
INSERT INTO rem_taux (rtx_id, rtx_code, rtx_taux, rtx_date)
VALUES (1, <some>, <some>, <some>)
this is with assumption you dont have FK or other dependant structure

Update from two differents table with postgresql

I want to update a table with postgresql.
In fact, I have a table (TABLE_ONE) with two column (old_id and new_id). I have a second table (TABLE_TWO) with colums (id,column1,column2,...).
I want to update the column id from TABLE_TWO. The wanted behavior is that when TABLE_ONE.id = TABLE_TWO.old_id, we set id to new_id.
How can i do that?
You want an UPDATE FROM statement:
UPDATE table_one
SET table_one.id = table_two.id
FROM table_two
WHERE table_one.id = table_two.old_id;

t sql select into existing table new column

Hi I have a temp table (#temptable1) and I want to add a column from another temp table (#temptable2) into that, my query is as follows:
select
Customer
,CustName
,KeyAccountGroups
,sum(Weeksales) as Weeksales
into #temptable1
group by Customer
,CustName
,KeyAccountGroups
select
SUM(QtyInvoiced) as MonthTot
,Customer
into #temptalbe2
from SalesSum
where InvoiceDate between #dtMonthStart and #dtMonthEnd
group by Customer
INSERT INTO #temptable1
SELECT MonthTot FROM #temptable2
where #temptable1.Customer = #temptable2.Customer
I get the following: Column name or number of supplied values does not match table definition.
In an INSERT statement you cannot reference the table you are inserting into. An insert works under the assumption that a new row is to be created. That means there is no existing row that could be referenced.
The functionality you are looking for is provided by the UPDATE statement:
UPDATE t1
SET MonthTot = t2.MonthTot
FROM #temptable1 t1
JOIN #temptable2 t2
ON t1.Customer = t2.Customer;
Be aware however, that this logic requires the Customer column in t2 to be unique. If you have duplicate values in that table the query will seem to run fine, however you will end up with randomly changing results.
For more details on how to combine two tables in an UPDATE or DELETE check out my A Join A Day - UPDATE & DELETE post.
If I understand it correctly you want to do two things.
1: Alter table #temptable1 and add a new column.
2: Fill that column with the values of #temptable2
ALTER #temptable1 ADD COLUMN MothTot DATETIME
UPDATE #temptable1 SET MothTot = (
SELECT MonthTot
FROM #temptable2
WHERE #temptable2.Customer = #temptable1.Customer)