oracle: update a column with not null value - sql

I wanna update a column of a table:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
)
the SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
mustn't return a null result, if it's null, the update will not be made
How to do that?

You need to filter the rows to be updated in the WHERE clause as well:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
)
WHERE EXISTS (SELECT 42
FROM supplier s2
WHERE s2.prodno = prod.prodno);
It might be faster using a MERGE (assuming prodno is the primary key in product):
merge into product
using
(
select p.prodno,
s.prod_supplier_id
from product p
join supplier s on s.prodno = p.prodno
) t on (t.prodno = prod.prodno)
when matched then update
set prod_supplier_id = t.prod_supplier_id;
Not tested!

first of all create a back up table:
CREATE TABLE productBAK AS SELECT * FROM product;
now you can use update query like this:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno and
s.prod_supplier_id is not null
)
WHERE prod.prodno in (SELECT s1.prodno FROM supplier s1 where s1.prod_supplier_id is not null);

The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

I see prod_supplier_id being a column in both product and supplier.
Can you explain in which table the prod_supplier_id must not be null?
Looking at your query you want something like
update product
set prod_supplier_id = (select prod_supplier_id
from supplier
where prod_supplier_id is not null);
Issues you're facing:
all product records will be updated with the same prod_supplier_id
if the subselect statement returns more than one record -let's say you have 5 suppliers, so 5 prod_supplier_id's, the update statement will fail.
Can you tell us:
do you want all your products to be set to 1 supplier?
if not, do you have a list of which product should have which supplier?
R

Related

Change values in a row if the primary key is not present in another table

UPDATE Customers,OrderLine
Set customerID = "---"
Where Customers.customerID != OrderLine.CustomerID;
I'm not too sure how to make this work
What it is meant to do is change the values in customerID to --- but only if that value is not present in the CustomerID column in the OrderLine table
A column like customerID is I guess the PRIMARY KEY of the table Customers, right?
If this is the case then simply you can't update more than 1 row to '---' because this would create duplicates and this is not allowed.
If this is not the case then use NOT EXISTS:
UPDATE Customers
SET customerID = "---"
WHERE NOT EXISTS (
SELECT 1 FROM OrderLine
WHERE Customers.customerID = OrderLine.customerID
)
With NOT IN:
UPDATE Customers
SET Customers.customerID = "---"
WHERE Customers.ID NOT IN (SELECT OrderLine.CustomerID FROM OrderLine);

Case statement for join condition

Hi I want to use case statement in where condition or some similar logic.
I want to ignore where condition when there are no rows in tmp_collaboration table and use the condition when table has some rows in it.
Select clbid from tmp_collaboration;
select customer_id, product_id ,clbid
from customers c
where hdr_id = 10
and clbid in (select clbid from tmp_collaboration)
and status = 'y';
Is this what you want?
select customer_id, product_id ,clbid
from customers c
where hdr_id = 10 and status = 'y' and
(clbid in (select clbid from tmp_collaboration) or
not exists (select 1 from tmp_collaboration)
);
why not use a JOIN .. if there are rows in table the rows are involved otherwise not .
select customer_id, product_id ,clbid
from customers c
INNER JOIN tmp_collaboration t on t.clbid = c.clbid
AND hdr_id = 10
AND status = 'y';

how to update single column in one table when two column are match in another table?

Everyone I am beginner in SQL.
I have two tables like first, second.
pid column of first table and pid column of second table are same. I have to get price from the first table and quantity from second table, multiply those values (final) and update that final values in second table at the price column.
I tried but it doesn't work. Can anyone help me? Thanks in advance.
FIRST TABLE
Name Null? Type
------ -------- ------------
PID NOT NULL NUMBER(38)
PNAME NOT NULL VARCHAR2(20)
PPRICE NOT NULL FLOAT(126)
PAVAIL NOT NULL NUMBER(38)
SECOND TABLE
Name Null? Type
------------ -------- ------------
ORDERID NOT NULL NUMBER(38)
PID NUMBER(38)
CUSTOMERNAME NOT NULL VARCHAR2(20)
PHNO NOT NULL NUMBER(38)
QTY NOT NULL NUMBER(38)
PRICE NOT NULL FLOAT(126)
I used this query, but it doesn't work:
update first f, second s
SET s.price = f.pprice * s.qty
where s.pid EXISTS (select second.pid from first, second
where (first.pid=second.pid);
Oracle does not support update join syntax. One workaround is to use an update with a correlated subquery.
UPDATE second s
SET s.price = s.qty * (SELECT f.pprice FROM first f WHERE f.pid = s.pid)
WHERE EXISTS (SELECT 1 FROM first f WHERE f.pid = s.pid)
The exists clause is necessary to make sure that a record in second which does not match to anything in first does not have its price update to NULL. If this can't happen, or you don't care if it does, then you may also remove the WHERE clause.
Update using a join is done with the MERGE statement in Oracle and that's what you need. In your case:
MERGE INTO second a
USING FIRST b
ON (a.pid = b.pid)
WHEN MATCHED
THEN
UPDATE SET a.price = b.price * a.qty;
Check this statement:
update second s
set s.price = s.qty * (select f.pprice from first f where f.pid = s.pid )
where exists (select f.pprice from first f where f.pid = s.pid );
you may try this also..
update s
set s.price =
s.qty * f.pprice
from second s,first f
where s.pid = f.pid
if any mistake..revert me back

Update table column based on value of key in other column POSTGRES

My table name is companies having is_nse,a boolean column and exchanges column type json having value like "[{"exchange":"NSE","ticker":"ABC"},{"exchange":"BSE","ticker":"ABC"}]"
Now i have this query
update companies set is_nse=0 from (SELECT is_nse, obj.value->>'exchange' As exch FROM (SELECT * FROM companies WHERE sector is not null) u JOIN LATERAL json_array_elements(exchanges) obj(value) ON obj.value->>'exchange' = 'BSE')y
But it is updating all the rows in companies table rather than rows of the subquery.Can anybody tell me where am I going wrong?
You need to connect the companies in the update to the from clause. Assuming you have an id, you can do:
update companies
set is_nse = 0
from (select c.*
from companies u join lateral
json_array_elements(exchanges) obj(value)
on obj.value->>'exchange' = 'BSE'
where u.sector is not null
) y
where y.companyid = companies.companyid ;

Update Data on One Table Based on Another - SQL

I have two tables.
One is SUPPLIER with SupplierID as its PK and it has a State attribute.
The other is SUPPLIEDPART. It has a UnitCost attribute. SupplierID is a FK.
I'm trying to update the UnitCost based on whether or not the Supplier is located in the state of Texas. I have tried multiple queries, but they always fail. Here are some of the latest attempts. Any help would be appreciated. I have attached a picture of the relationship between the two tables if that will help.
INSERT INTO SUPPLIEDPART (UnitCost)
SELECT UnitCost * 1.1
FROM SUPPLIEDPART SP INNER JOIN SUPPLIER S
ON SP.SupplierID = S.SupplierID
WHERE State = 'TX';
----This one results in a "cannot insert nulls into brewer34.suppliedpart.supplierid
UPDATE SUPPLIEDPART
SET UnitCost = UnitCost * 1.1
WHERE SupplierID = (SELECT SupplierID
FROM SUPPLIER
WHERE State = 'TX')
GROUP BY PartNumber;
----This query results in a command not properly ended----
I think you were close:
UPDATE SUPPLIEDPART
SET UnitCost = UnitCost * 1.1
WHERE SupplierID IN (SELECT SupplierID -- <-- IN instead of =
FROM SUPPLIER
WHERE State = 'TX')
-- no GROUP BY
On most DBMS you can do:
UPDATE SUPPLIEDPART AS a
INNER JOIN SUPPLIER AS b ON a.SupplierID = b.SupplierID
SET a.UnitCost = a.UnitCost * 1.1
WHERE b.State = 'TX';