how to create a view and insert data into it - sql-server-2008

how to insert data into above tables with below conditions:
conditional & dynamic
automated transaction behavior using triggers
Create a view to include a join of relevant queries then define a
trigger on the view then define a sp to insert data into the view

Related

Is possible to log sqlite changes in a trigger without list all the fields, defining a custom function?

I need to log all the changes made to most tables in my sqlite db.
Using triggers I need to list all the fields for it, and duplicate it for all crud events (insert, update, delete).
I understand is not possible to use NEW/OLD as values so I wonder if registering a custom function that I could do in Rust, I can add this functionality so my triggers get simplified to:
CREATE TRIGGER visit_log
AFTER INSERT ON visit
BEGIN
INSERT INTO log (table, pk, data, date) VALUES(.., data = to_my_json(???));
END;

Does Truncating the Table Data Automatically Delete or truncate the View data?

I created a view using the Members table. If I truncate the Members table will it automatically truncate the view data?
A view is like a virtual table and does not have a Physical existence. A View is derived from an SQL Query which may pull data from a Single table or Multiple ones.
So when you do a query on a view, What it actually does is running the View Query on the background. So if you make any changes in the data on the Tables using the Underlying script, then the same will be effected automatically in the view

copy rows from one table to another triggered by insert on a 3rd table

In my database there are four tables: task, tasknotes, task_archive and tasknotes_archive. When an entry is copied from the task table to the task_archive table I want to use a trigger to perform the following:
copy the related task notes from the tasknotes table to the tasknotes_archive table.
delete the entry in the task table I just copied to task_archive
delete the entries from tasknotes that I just copied to tasknotes_archive
The application that interfaces with the database is built in Java using JDBC. I could achieve the above results as either a series of calls to the database or as a transaction. However it would seem more efficient to have the initial insert statement than copies the row from task to task_archive trigger the rest of the events. I initially tested this by seeing if I could get a trigger to delete the entry from the task table based on the insert into task_archive. This seemed to work fine. However when I started trying to add in the script to cause the DB to copy from tasknotes to tasknotes_archive I got error messages stating that it doesn't recognise task_archive.task_id in the first where clause. Importantly, tasknotes and tasknotes_archive have the exact same table structure so this insert method should be possible as discussed in the answer to this question: MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column?. I then tried changing this to new.task_id based on answers to other questions on stack. Still got error messages. The following code is the insert trigger contained in task_archive, which should I'm trying to develop to perform the above actions on tasknotes_archive and task:
CREATE
TRIGGER `myDB`.`task_archive_AFTER_INSERT`
AFTER INSERT ON `myDB`.`task_archive`
FOR EACH ROW
BEGIN
INSERT INTO tasknotes_archive
SELECT tasknotes.* FROM tasknotes
WHERE tasknotes.task_id = task_archive.task_id;
DELETE FROM task
USING task, task_archive
WHERE task.task_id = task_archive.task_id;
END
My question is, is it possible to have multiple events run as a trigger as described? Am I correct in assuming this is a more efficient way of performing this rather than multiple calls to the DB in java? Finally, what is the correct way to write this trigger?
You need to use NEW.task_id to get the task related to the current row of the trigger.
And if you're doing this using a CLI, you need the DELIMITER statement so you can have ; between the statements in the trigger.
DELIMITER $$
CREATE
TRIGGER `myDB`.`task_archive_AFTER_INSERT`
AFTER INSERT ON `myDB`.`task_archive`
FOR EACH ROW
BEGIN
INSERT INTO tasknotes_archive
SELECT tasknotes.* FROM tasknotes
WHERE tasknotes.task_id = NEW.task_id;
DELETE task, tasknotes
FROM task JOIN tasknotes USING (task_id)
WHERE task.task_id = NEW.task_id;
END
$$
DELIMITER ;

Combine three tables to make an updatable new table

Is there any way (except views) to create a new table in mysql that combines data from three or more different tables and when inserting new records into the initial tables data are also displayed in the new merged table?
Introduction
One thing though except for views, would be to use triggers.
When an update is made on this new table, your on insert or on update triggers could call a stored procedure to update the related data.
How to Pages:
See the following related pages:
mysql after insert trigger which updates another table's column
Using an update trigger to update another table
Update another table after insert using a trigger?

How can I make a MYSQL view update after a trigger causes an insert on table used in view

I have a MYSQL table that I insert into based on a trigger and I have a view that uses this table (and joins on others). When my trigger fires and inserts a new row it shows up fine in the table but the view does not update. Is there a way I can get the view to update or is this a limitation of triggers?
Note: regular inserts into the table update the view just fine, only inserts from a trigger are missing from the view.
Thanks in advance.
Turns out the issue was with the joins I used to make the view - I had inner joins on fields that were only NULL during inserts from the trigger. I switched them to left joins and now the view updates fine regardless of whether the insert is from a trigger or not.
Also, #PM-77-1's answer is correct but in this case I was not altering any columns, just inserting new rows of data.
From the docs (with my emphasis):
The view definition is “frozen” at creation time, so changes to the
underlying tables afterward do not affect the view definition. For
example, if a view is defined as SELECT * on a table, new columns
added to the table later do not become part of the view.
So your trigger code should include ALTER VIEW statement (if you are absolutely certain that the view exists) or CREATE OR REPLACE (if you are not).
For additional information on MySQL views (not related to the question at hand) see Restrictions on Views.