Update all tables using trigger - triggers

My customer table is (customer_id, customerName). Almost all the tables have customerName column. I need to make a trigger that will update all the customerNames' in Database when the customerName in customer table is updated or changed.

A better approach is to normalise your database, so that other tables do not hold a customer name, rather they hold a reference to the customer_id. This way customer details only need to be updated in the customer table.

Related

PostgreSQL: Table that points to other Tables?

Just getting started in PostgreSQL and wanted to ask some questions.
Suppose that I have a table of Vendors. Each Vendors has an attribute called Sales Record, which is a time series data about their sales. For each Vendors, I want to be one associated Sales Record Table that has the timeseries sales data for that specific vendor.
How might I want to code that?
You shouldn't have a table per vendor.
Rather, create one big table for all. The table contains a column like vendor_id that is a foreign key to vendors and identifies to which vendor a record belongs.
If you create an index on vendor_id, searching the big table for the data of a vendor will be efficient.

inserting data into multiple tables using procedure in oracle

I have 4 Tables
Customer(cust_id (PK), cust_phone, name)
Order((order_id,cust_id) (PK), order_date, order_amt)
Order_line((order_id,Product_id)(PK), qty)
Product(product_id, Product_name, product_price)
The order table has a composite key saying an order only exists with a customer.
The Order_line table has a composite key as it is a bridge table between product and order table. the order_amt is a calculated column, its the total order value.
The question is how do i insert data into the order table and the Order_line table? using procedure, triggers or a temp table or any other way? I am really stuck at this. Help!!
Do I need to make any changes in the model?

Not able to delete value from a table that is associated with another with foreignkeyconstraint

Hi I have two table Customer and Orders.
Customer Id is primary in Customer and Foreign key in Orders.
I have done the following coding:
ForeignKeyConstraint custOrderFK = new ForeignKeyConstraint("CustOrderFK",
custDS.Tables["CustTable"].Columns["CustomerID"],
custDS.Tables["OrdersTable"].Columns["CustomerID"]);
custOrderFK.DeleteRule = Rule.None;
custDS.Tables["OrdersTable"].Constraints.Add(custOrderFK);
Since I have mentioned custOrderFK.DeleteRule = Rule.None; deleting an entry in customer's table should not affect order's table. But I am not able to delete a row from Customer table. It throws exception. I am new to ado.net.
Maybe something is wrong with my understanding of rules.
Use delete cascade option with foreign key.

SQL Merge Query - Executing Additional Query

I have written a working T-SQL MERGE statement. The premise is that Database A contains records about customer's support calls. If they are returning a product for repair, Database B is to be populated with certain data elements from Database A (e.g. customer name, address, product ID, serial number, etc.) So I will run an SQL Server job that executes an SSIS package every half hour or so, in which the MERGE will do one of the following:
If the support call in Database A requires a product return and it
is not in Database B, INSERT it into Database B..
If the support call in Database A requires a product return and it
is in Database B - but data has changed - UPDATE it in Database B.
If there is a product return in Database B but it is no longer
indicated as a product return in Database A (yes, this can happen - a customer can change their mind at a later time/date and not want to pay for a replacement product), DELETE it from Database
B.
My problem is that Database B has an additional table with a 1-to-many FK relationship with the table being populated in the MERGE. I do not know how, or even if, I can go about using a MERGE statement to first delete the records in the table with FK constraint before deleting the records as I am currently doing in my MERGE statement.
Obviously, one way would be to get rid of the DELETE in the MERGE and hack out writing IDs to delete in a temp table, then deleting from the FK table, then the PK table. But if I can somehow delete from both tables in WHEN NOT MATCHED BY SOURCE that would be cleaner code. Can this be done?
You can only UPDATE, DELETE, or INSERT into/from one table per query.
However, if you added an ON DELETE CASCADE to the FK relationship, the sub-table would be cleaned up as you delete from the primary table, and it would be handled in a single operation.

foreign key constraint in sql

I have 2 tables in sql server with primary keys set to identity. They are related and work fine.
I then created a form in vb 2008 and tried inserting some values into my database the respective primary keys work but the primary key in the parent table wont show up in the child table.I did create a relationship in vb using ado.net and all the details of my table are defineed in the data table. For example
cust tables (custid,name,..)
book table(bookid,bookname,..,custid)
in vb my insert statement is something like Insert into cust(name) values(#name)
insert into book(bookname) values(#bookname). I do not include the id columns as they auto generate in the database(tables).
My question is that how do i get to insert the custid in the book table when the data is stored back into the tavles in my database.
Please advice with an example as im not half as good as you guys.
Kind Regards
You have to know which customer you want to associate with the book before INSERTing the book. If you don't know before hand, you can't. So somewhere in your Form there should be a way to select a customer. Then when you create a book, you grab that customer's ID and insert it along with the other book info.
You don't actually say that you created a foreign key constraint between the two tables!
You need to:
Ensure that you create an explicit foreign key on the BOOK table to point to a customer in the CUST table.
First insert the customer.
Then find out what the customer's auto-generated ID was. That value is in ##IDENTITY. Store it somewhere e.g. #CUSTID.
Insert the book, specifying #CUSTID as the customer's ID.