How To Update All Tables In SQL - sql

I'm self learning SQL, using Microsoft Server management and have a question
Let say you have Customer table, Store table and Delivery table. The store needs to update their stocks count every time a Customer purchases a product or a Deliveryis made(INSERT INTO TABLE).
How to update your stock count in Store table whenever a data is Inserted into Customer table and Delivery table? And how do you Insert a New product from Delivery to your Store Table?
I think nested queries is the correct way to do both of these problems but just not sure how to do nested query for INSERT query

It is generally not possible to update multiple fields from multiple tables on a single update query (depends on your DBMS). You can add triggers, e.g. updating the Stock table after an insert on the Delivery or Customer table. If you want these updates to be done at the exact same time, use a transaction.

Related

How can i add row when there in an update for each table in Presto SQL

I want to make a table which records update history for every single day.
I was trying to write sql, but got no idea how to make this table especially with update date.
How can I record update from all tables when it's been updated?

stock table for inventory database design architecture

I am currently building a management system. Is it good practice to create a balance table for inventory to store the inventory at hand and constantly update the table if there are changes, or should one just directly query total inventory ordered table - total inventory used table? Which would be the most efficient and fastest way to do?
It is likely a bad idea to use two separate tables. You will have to perform a join which is unnecessary. Simply have one table with an 'ordered' column and a 'used' column. In your query you can very efficiently calculate the net value e.g. :
SELECT ordered, used, (ordered - used) as net FROM inventory

Incremental load for Updates into Warehouse

I am planning for an incremental load into warehouse (especially for updates of source tables in RDBMS).
Capturing the updated rows in staging tables from RDBMS based the updates datetime. But how do I determine which column of a particular row needs to be updated in the target warehouse tables?
Or do I just delete a particular row in the warehouse table (based on the primary key of the row in staging table) and insert the new updated row?
Which is the best way to implement the incremental load between the RDBMS and Warehouse using PL/SQL and SQL coding?
In my opinion, the easiest way to accomplish this is as follows:
Create a stage table identical to your host table. When you do your incremental/net-change load, load all changed records into this table (based on whatever your "last updated" field is)
Delete the records from your actual table based on the primary key. For example, if your primary key is customer, part, the query might look like this:
delete from main_table m
where exists (
select null
from stage_table s
where
m.customer = s.customer and
m.part = s.part
);
Insert the records from the stage to the main table.
You could also do an update existing records / insert new records, but either way that's two steps. The advantage of the method I listed is that it will work even if your tables have partitions and the newly updated data violates one of the original partition rules, whereas an update would not accomplish that. Also, the syntax is much simpler as your update would have to list every single field, whereas the delete from / insert into allows you list only the primary key fields.
Oracle also has a merge clause that will update if it exists or insert if it does not. I honestly don't know how that would be impacted if you had partitions.
One major caveat. If your updates include deletes -- records that need to be deleted from the main table, none of these will resolve that and you will need some other way to handle that. It may not be necessary, depending on your circumstances, but it's something to consider.

Oracle PL/SQL query regarding handling data

I have a live production table which has more than 1 million records. Now i don't need to tamper anything on this table and would like to create another table which fetches all records from this live production table. I would schedule a job which can take entries from my main table and inserts them to my new table. But i don't want all the records daily; i just need the records added on a daily basis in the production table to get added in my new table.
Please suggest a faster and efficient approach.
You could do this with an INSERT/UPDATE/DELETE trigger to send the INSERTED/UPDATED/DELETED row to the new table, however this feels like reinventing the wheel on the most basic level.
You could just use asynchronous replication rather than hand-rolling it all yourself, this is probably safer, more sustainable and scalable. You could add as many tables as you like to the replicated source.
Copying one million records from an existing table to a new table should not take very long -- and might even be faster than figuring out what records to copy. You could do something like:
truncate table copytable;
insert into copytable
select *
from productiontable;
Note that you should explicitly list the columns when doing the insert.
You can also readily add new records -- assuming you have some form of id on the production table, such as an id assigned by a sequence. Then you can do:
insert into copytable
select *
from productiontable p
where p.id > (select max(id) from copytable);

Help in SSIS task of adding Data

I am trying to create a task of data import, my case is :
Taking ProductOrderID from ORDERS table, and searching it in MAINORDERS table and fetch all the records that match the POrderID and once the match is found, I insert the result set to ORDERDETAILS table.
and also in ORDERDETAILS table I like to add ORDERS's table OrderID as Foreign key.
Need a advice on how to perform this task.
Thanks
You would need to have a data source (OleDb or SqlServer probably, you don't state the DB being used), which selects the ProductOrderID from the Orders table, and the OrderID as well, since you'll need that later on. Then add a Lookup task to the MainOrders table, sending the matching rows to OrderDetails with an OleDb or SqlServer Destination task - the OrderID from the original source would be used to populate the FK. What do you need to do with non-matching rows?