How to insert data from one table to another table in oracle - sql

I have a two table 'Inventory' and 'Tendor' where Inventory has primary key pk_id ,i updated 'Inventory' table when Inventory.ti_name = Tendor.ki_name by using following query
Update Inventory A set (Used_NAME, ACCOUNT_NUMBER, ti_STATUS)
= (Select B.Using_NAME, B.ACCOUNT_NO, B.ki_STATUS from
Tendor B where A.ti_name = B.ki_name and a.pk_id is not null);
Anything wrong in this query or any optimized one??
After updation for those who is not satisfying the condition Inventory.ti_name = Tendor.ki_name i want to insert it as new rows in 'Inventory' table with primary key pk_id should be changed
how to do this? for pk_id do i need to do some logic like 'SEQ.NEXTVAL FROM DUAL'
Can anybody suggest a query

This query updates searches matching rows in tables inventory and tendor. When both tables contain row with the same value of ki_name, it updates row in table inventory. All rows from tendor, that was not found in inventory, will be inserted there:
merge into Inventory a
using Tendor b
on (A.ti_name = B.ki_name)
when matched then update
set a.Used_NAME = B.Using_NAME,
a.ACCOUNT_NUMBER = B.ACCOUNT_NO,
a.ti_STATUS = B.ki_STATUS
when not matched then
insert (pk_id, Used_NAME, ACCOUNT_NUMBER, ti_STATUS)
values (your_seq.nextval, B.Using_NAME, B.ACCOUNT_NO, B.ki_STATUS)
Also you can use sequence in insert statement.

Related

Automatically remove a row without foreign references

I am using sqlite3.
I have one "currencies" table, and two tables that reference the currencies table using a foreign key, as follows:
CREATE TABLE currencies (
currency TEXT NOT NULL PRIMARY KEY
);
CREATE TABLE table1 (
currency TEXT NOT NULL PRIMARY KEY,
FOREIGN KEY(currency)
REFERENCES currencies(currency)
);
CREATE TABLE table2 (
currency TEXT NOT NULL PRIMARY KEY,
FOREIGN KEY(currency)
REFERENCES currencies(currency)
);
I would like to make sure that rows in the "currencies" table that are not referenced by any row from "table1" and "table2" will be removed automatically. This should behave like some kind of ref-counted object. When the reference count reaches zero, the relevant row from the "currencies" table should be erased.
What is the "SQL way" to solve this problem?
I am willing to redesign my tables if it could lead to an elegant solution.
I prefer to avoid solutions that require extra work from the application side, or solutions that require periodic cleanup.
Create an AFTER DELETE TRIGGER in each of table1 and table2:
CREATE TRIGGER remove_currencies_1 AFTER DELETE ON table1
BEGIN
DELETE FROM currencies
WHERE currency = OLD.currency
AND NOT EXISTS (SELECT 1 FROM table2 WHERE currency = OLD.currency);
END;
CREATE TRIGGER remove_currencies_2 AFTER DELETE ON table2
BEGIN
DELETE FROM currencies
WHERE currency = OLD.currency
AND NOT EXISTS (SELECT 1 FROM table1 WHERE currency = OLD.currency);
END;
Every time that you delete a row in either table1 or table2, the trigger involved will check the other table if it contains the deleted currency and if it does not contain it, it will be deleted from currencies.
See the demo.
There is no automatic way of doing this. The reverse can be handling using cascading delete foreign key references. The reverse is that when a currency is deleted all related rows are.
You could schedule a job daily running something like:
delete from currencies c
where not exists (select 1 from table1 t1 where t1.currency = c.currency) and
not exists (select 1 from table2 t2 where t2.currency = c.currency);
If you need an automatic way for doing that, then most dbms provide a trigger mechanism. You can create a trigger on update and delete operations that run the folowing query:
you can use a left join for that:
https://www.w3schools.com/sql/sql_join_left.asp
It return a row for all rows from the left table, even if there is no corresponding row in the right table, replacing the rows form the right with null. You can then check a not null right table field for null with is null. This will filter for the rows the have no counterpart in the right table.
For example:
SELECT currencies.currency FROM currencies LEFT JOIN table1 WHERE table1.currency IS NULL
will show the relevant rows for table1.
You can do the same with table two.
This will give you two queries, that shows which rows have no couterpart.
You can then use intersect on the result, so that you have the rows that have not couterpart in either:
SELECT * FROM query1 INTERSECT SELECT * FROM query2
Now you have the list of currencies to be deleted.
You can finish this by using a subqueried delete:
DELETE FROM currencies WHERE currency IN (SELECT ...)

Creating a trigger that replaces null values in an insert with values already present in the table in SQL Server

I have a table, known as Fruit_Veg_Product_Table which is used to contain the characteristics of certain fruit and vegetable stock.
The table has the following columns:
Product_ID
Product_Type
Product_Name
Product_Colour
Product_Price
Product_In_Sale
Product_Stock_Level
Product_Height
Product_Width
Product_Depth
Product_Package_Height
Product_Package_Width
Product_Package_Depth
When a new product is inserted into the table, sometimes the product is inserted without any dimensions (the columns from Product_Height all the way to Product_Package_Depth). In this circumstance, the dimensions are entered as NULL.
I am in need of a SQL Server trigger that will replace all the NULL values from the attempted insert with the values corresponding to products that are already stored in the table which share a common Product_Type with the product that is being entered.
Any help with this problem is greatly appreciated.
Triggers have an INSERTED logical table that can be used to join the inserted row data back to the physical table. Here is an example:
CREATE TRIGGER Fruit_Veg_Product_Table_Trg
ON dbo.Fruit_Veg_Product_Table
FOR INSERT
AS
UPDATE dbo.Fruit_Veg_Product_Table
SET Product_Package_Height = ca.Product_Package_Height,
Product_Package_Width = ca.Product_Package_Width,
Product_Package_Depth = ca.Product_Package_Depth
FROM dbo.Fruit_Veg_Product_Table
CROSS APPLY
(
SELECT TOP 1
Product_Package_Height,
Product_Package_Width,
Product_Package_Depth
FROM dbo.Fruit_Veg_Product_Table AS fvpt
WHERE dbo.Fruit_Veg_Product_Table.Product_Type = fvpt.Product_Type
AND Product_Package_Height IS NOT NULL
AND Product_Package_Width IS NOT NULL
AND Product_Package_Depth IS NOT NULL
) AS ca
WHERE EXISTS
(
SELECT *
FROM INSERTED
WHERE INSERTED.Product_ID = dbo.Fruit_Veg_Product_Table.Product_ID
AND INSERTED.Product_Package_Height IS NULL
AND INSERTED.Product_Package_Width IS NULL
AND INSERTED.Product_Package_Depth IS NULL
);
GO

Insert columnA values of Table 1 into another table if match occur

I have two tables.Table A has 4 columns. And table B has two columns.I want to insert value of one column of tabel A from one column of table B based on condtion if id matches.
how i can do this ? For example if [Movieid] in 1st table =[IMDBid] in second table then insert [count] of table 1=[CB] in table 2.
i want to do it once for full table.
[column] -> these are colums
i m using sql server.
Tabel 1 : Movieid,count,
Tabel 2: IMDBid, CB
Results which i want: i want to insert values of CB column in count where Movieid=IMDBid
Tabel 1 :
(Nick Id,MovieId,Rating,MovId)-> (1,4972,6.25,?)(1,24216,7.25,?)
Tabel 2 :
(Imdbid,Title,ImdbPyId,Id)-> (4972,hello,32450,1)(24216,hi,62450,2)
Insert /fill value of MovId(tabel1) using values Id(tabel2)where MovieId(tabel1)==Imdbid(tabel2)
You can do it like,
UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
But it is gonna blow up if you have multiple values returning from the subquery.
So make sure to use the appropriate function to get the single value from that subquery whichever meets your requirements.
If your tables have 1:1 relationship then it should be fine. But if it is 1:n then you need to use either aggragate functions or the TOP 1 clause in that subqyery.
solution is UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)

Advanced sql query

I have two tables in a sql database, I am trying to update a column from my committed table (committedtbl) with values from my vendor table (vendortbl) based on a common column from both tables.
There is a column with the vendor identification number (vendorno) in both tables, I tried adding the vendor description (vendorname) column from the vendortbl to the committedtbl but there are no values in it.
I need to insert values into the vendorname based on the corresponding numbers from vendorno... How do I accomplish this?
The vendorname column already exists in my committedtbl.
I have tried this, but got an error:
update v_vendorname
set v_vendorname = v_vendorno
from vendortbl vt
where v_vendorno = vt.v_venkey
update committedtbl
set c.vendorname = v.vendorname
from committedtbl c
inner join vendortbl v on v.vendorno = c.vendorno

SQL: insert records is not in order

I am having a problem while a record is inserted into a table.
each parking_cost is inserted after its related record. It should be with its record. why is this happening?
Any ideas ?
Regards.
If you are doing two separate queries to populate the row in the table, the first query needs to be an INSERT, the second query needs to be an UPDATE, eg:
INSERT INTO Customers (Pre_Payed_Card, Parking_ID) VALUES ('1234', 1)
Then
UPDATE Customers SET Parking_Cost = <cost> WHERE Parking_ID = 1
An INSERT will always create a new row; two INSERT queries for the same logical entity will result in two separate rows like you have in the screenshot.
Update: re-reading that query, you're probably after something like
UPDATE Customers SET Parking_Cost = p.Parking_Cost
FROM Customers c INNER JOIN Parking p ON c.Parking_ID = p.Parking_ID
WHERE c.Parking_ID = 1