Updating a row in postgres from another row - postgresql

I have two tables one called details and the other called c_details. Both tables are exact the same except for different table names
No I a row with data in both of these tables
Is it possible to update the row in details with the row in c_details
Eg.
update details SET (Select * from c_details)?

You have to describe explicitly the list of columns to be updated, as well as the key to match columns between the two tables.
The syntax for updating one table from another table is described in detail in the UPDATE chapter of the PostgreSQL documentation.
UPDATE
details
SET
name = c.name,
description = c.description
FROM
c_details AS c
WHERE
c.id=details.id;

If you need to UPDATE FROM SELECT and SET a field based on an aggregate function (MIN) then then SQL should read:
UPDATE
details
SET
name = (
SELECT
MIN(c.create_at)
FROM
c_details AS c
WHERE
c.type = 4
)
WHERE
c.id = details.id;

Use this sql query:
INSERT INTO details SELECT * FROM c_details

Related

I get an Sql syntax error with Liquibase when I want to add a new column with conditional value

Context
Goal
I want to add a new column in a table.
I also want all existing columns to have a default value, depending on another column's value in another table.
What I have
I have created a new changelog file :
Issue
When I do a clean/install, I get the following error :
What have I done wrong ?
Can you try changing your SQL update statement with the below one and check if it works:
update assignment set location = 'SITE_TSN' from requirement where (requirement.id = assignment.requirement_id and requirement.location = 'INTRAMUROS');
In postgres when we want to update data in a table based on values in another table, we can use PostgreSQL UPDATE JOIN syntax :
UPDATE t1
SET t1.c1 = new_value
FROM t2
WHERE t1.c2 = t2.c2;
To join to another table in the UPDATE statement, you specify the joined table in the FROM clause and provide the join condition in the WHERE clause. The FROM clause must appear immediately after the SET clause.
For each row of table t1, the UPDATE statement examines every row of table t2. If the value in the c2 column of table t1 equals the value in the c2 column of table t2, the UPDATE statement updates the value in the c1 column of the table t1 the new value (new_value).
For a detailed example of this JOIN syntax, please visit this link.

PostgreSQL update certain row

I want to update verified from 'f' to 't' for certain rows, but it will update all the rows when I do this:
UPDATE
news
SET
verified = 't'
FROM
(
SELECT
verified
, ROW_NUMBER() OVER () AS rownum
FROM
news
) AS foo
WHERE
rownum = 1 or rownum = 15 or rownum = 32 or rownum = 54;
Can someone tell me where the problem is? Thanks
From the PostgreSQL documentation for the UPDATE clause:
When a FROM clause is present, what essentially happens is that the target table is joined to the tables mentioned in the from_list, and each output row of the join represents an update operation for the target table.
So, your query creates the cross product of the news table with the results of the SELECT clause, which of course is also the news table (which I will call "foo" because of your AS clause). Every single row from the news table is accompanied by every single row from the foo result table, creating nxn rows where n is the number of rows in news. So, for every news row, there is at least one foo row that is selected by the WHERE clause. Thus, every row of the news table is modified.
It's not clear to me what you are trying to do. (Can the OVER parameter be blank? And why select verified in the foo query?) But you may want to join each foo row to the proper news row in the WHERE clause, perhaps by primary key.

Update or insert with outer join in postgres

Is it possible to add a new column to an existing table from another table using insert or update in conjunction with full outer join .
In my main table i am missing some records in one column in the other table i have all those records i want to take the full record set into the maintable table. Something like this;
UPDATE maintable
SET all_records= othertable.records
FROM
FULL JOIN othertable on maintable.col = othertable.records;
Where maintable.col has same id a othertable.records.
I know i could simply join the tables but i have a lot of comments in the maintable i don't want to have to copy paste back in if possible. As i understand using where is equivalent of a left join so won't show me what i'm missing
EDIT:
What i want is effectively a new maintable.col with all the records i can then pare down based on presence of records in other cols from other tables
Try this:
UPDATE maintable
SET all_records = o.records
FROM othertable o
WHERE maintable.col = o.records;
This is the general syntax to use in postgres when updating via a join.
HTH
EDIT
BTW you will need to change this - I used your example, but you are updating the maintable with the column used for the join! Your set needs to be something like SET missingcol = o.extracol
AMENDED GENERALISED ANSWER (following off-line chat)
To take a simplified example, suppose that you have two tables maintable and subtable, each with the same columns, but where the subtable has extra records. For both tables id is the primary key. To fill maintable with the missing records, for pre 9.5 versions of Postgres you must use the following syntax:
INSERT INTO maintable (SELECT * FROM subtable s WHERE NOT EXISTS
(SELECT 1 FROM maintable m WHERE m.id = s.id));
Since 9.5 there is a (preferred) alternative:
INSERT INTO maintable (SELECT * FROM subtable) ON CONFLICT DO NOTHING;
This is preferred because (apart from being simpler) it avoids the situation that has been known to arise in the former, where a race condition is created between the INSERT and the sub-SELECT.
Obviously when the columns are different, you need to specify in the INSERT statement which columns are inserted from which. Something like:
INSERT INTO maintable (id, ColA, ColB)
(SELECT id, ColE, ColG FROM subtable ....)
Similarly the common field might not be id in both tables. However, the simplified example should be enough to point you in the right direction.

How to get list of constraints of a table along with their respective column names in SQL Server 2008 R2

I want to list all constraint names with their respective column name for a table. I am using the below mentioned code :
select *
from sys.objects
where parent_object_id = object_id('qw') --this gives me the constraints lists but does not give me columns in which they are applied.
select *
from sys.columns
where object_id = object_id('qw') -- this gives me the column list of the table.
My problem is that I am not able to join these two queries to get columns along with their constraints.
Sounds like you need CONSTRAINT_COLUMN_USAGE.

a dual variable not in statement?

I have the need to look at two tables that share two variables and get a list of the data from one table that does not have matching data in the other table. Example:
Table A
xName
Date
Place
xAmount
Table B
yName
Date
Place
yAmount
I need to be able to write a query that will check Table A and find entries that have no corresponding entry in Table B. If it was a one variable issue I could use not in statement but I can't think of a way to do that with two variables. A left join also does not appear like you could do it. Since looking at it by a specific date or place name would not work since we are talking about thousands of dates and hundreds of place names.
Thanks in advance to anyone who can help out.
SELECT TableA.Date,
TableA.Place,
TableA.xName,
TableA.xAmount,
TableB.yName,
TableB.yAmount
FROM TableA
LEFT OUTER JOIN TableB
ON TableA.Date = TableB.Date
AND TableA.Place = TableB.Place
WHERE TableB.yName IS NULL
OR TableB.yAmount IS NULL
SELECT * FROM A WHERE NOT EXISTS
(SELECT 1 FROM B
WHERE A.xName = B.yName AND A.Date = B.Date AND A.Place = B.Place AND A.xAmount = B.yAmount)
in ORACLE:
select xName , xAmount from tableA
MINUS
select yName , yAmount from tableB