Question

In: Computer Science

Write a trigger to record any insertion changes in the stock table. Be sure to create...

Write a trigger to record any insertion changes in the stock table. Be sure to create a table to record any changes. Submit all your SQL code as well a screenshot of the new table showing any new insertion entries.

Solutions

Expert Solution

Hi,

Please find the Table and Trigger created in SQL: Please make sure the below tables are not exists in your database

--DROP TABLE IF EXISTS dbo.Results;
--DROP TABLE IF EXISTS dbo.ThereIsATriggerOnThisTable;
 
CREATE TABLE dbo.Results (Results VARCHAR(4000));

CREATE TABLE dbo.ThereIsATriggerOnThisTable (
  col1 VARCHAR(100) PRIMARY KEY
, col2 VARCHAR(100) NOT NULL DEFAULT(NEWID())
, col3 VARCHAR(100) NULL);
GO

CREATE TRIGGER dbo.MyTrigger ON dbo.ThereIsATriggerOnThisTable AFTER INSERT,UPDATE,DELETE AS
BEGIN
  DECLARE @Message VARCHAR(4000);
  SET @Message = 'I am proof the trigger did execute for ' + CAST(@@ROWCOUNT AS VARCHAR(4)) + ' row(s).'
  INSERT INTO dbo.Results VALUES (@Message);
 
  IF UPDATE(col1) INSERT INTO dbo.Results VALUES ('UPDATED says I updated col1');
  IF UPDATE(col2) INSERT INTO dbo.Results VALUES ('UPDATED says I updated col2');  
  IF UPDATE(col3) INSERT INTO dbo.Results VALUES ('UPDATED says I updated col3');
 
       IF COLUMNS_UPDATED() & 7 = 7 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col1, col2, AND col3');  
  ELSE IF COLUMNS_UPDATED() & 6 = 6 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col2 AND col3');
  ELSE IF COLUMNS_UPDATED() & 5 = 5 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col1 AND col3');
  ELSE IF COLUMNS_UPDATED() & 4 = 4 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col3');
  ELSE IF COLUMNS_UPDATED() & 3 = 3 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col1 AND col2');  
  ELSE IF COLUMNS_UPDATED() & 2 = 2 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col2');  
  ELSE IF COLUMNS_UPDATED() & 1 = 1 INSERT INTO dbo.Results VALUES ('COLUMNS_UPDATED says I updated col1');
 
END;

once you execute the above SQL then

Let's observe the Insert Trigger:

--Test SQL Server Trigger for INSERT

-- clear out table
TRUNCATE TABLE dbo.Results;

-- issue an insert command
INSERT INTO dbo.ThereIsATriggerOnThisTable(col1) VALUES ('BEFORE');

-- see results
SELECT * FROM dbo.Results;
SELECT * FROM dbo.ThereIsATriggerOnThisTable;
GO

Here you can see that if we insert any record in the stock table then Insert trigger created new entries in the our newly created table.

Let's observe the Update Trigger:

--Test SQL Server Trigger for UPDATE

-- clear out table
TRUNCATE TABLE dbo.Results;

-- issue an update command
UPDATE dbo.ThereIsATriggerOnThisTable SET col1 = 'AFTER';

-- see results
SELECT * FROM dbo.Results;
SELECT * FROM dbo.ThereIsATriggerOnThisTable;
GO

Update trigger created new entries in the newly created table. Here we have deleted the previous records from the newly created table.

Let's observe for Delete table:

--Test SQL Server Trigger for DELETE

-- clear out table
TRUNCATE TABLE dbo.Results;

-- issue a delete command
DELETE dbo.ThereIsATriggerOnThisTable;

-- see results
SELECT * FROM dbo.Results;
SELECT * FROM dbo.ThereIsATriggerOnThisTable;
GO

Here you can see that we have deleted the record from the stock table and the Delete trigger inserted a new row in the newly created table.

Note: you can comment on the truncate table which removes the previous data from the table (Results) which we created for the reference point of view i.e. if any Insert, Update or Delete operation performs on the stock table (ThereIsATriggerOnThisTable) then related entries will be added in this table.

Thanks.


Related Solutions

Write an AFTER Insert trigger for the following Employee table. Check Date of Birth and calculate...
Write an AFTER Insert trigger for the following Employee table. Check Date of Birth and calculate age. Update AGE field with calculated value in Employee_Info table Employee (Emp_ID int, DOB date) Employee_Info (Emp_ID int, Fname char, Lname char, Age int) Set variable Current_Date to CURDATE() Substract DOB from Current_Date to find Age
create a VHDL program for the Truth Table below. Please make sure to create your code...
create a VHDL program for the Truth Table below. Please make sure to create your code for the simplified circuit. A B C Z 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0
Create a table with a minimum of 10 Records. Each Record should have a Minimum of...
Create a table with a minimum of 10 Records. Each Record should have a Minimum of 8 fields, one of which should be a phone, one a birth date and 1 (Memo) a long Text. Don't forget a Key Field Using Microsoft Access
Write CREATE TABLE and INSERT INTO statements in order to create and populate five tables in...
Write CREATE TABLE and INSERT INTO statements in order to create and populate five tables in Oracle’s SQL*Plus.The information you need about the database ARE IN THE CHARTS BELOW. Each worksheet includes the following information about its associated table: ➢ Column names (for example, the jr_order table contains the orderID, customerID, orderDate, orderStatus, and orderShippedDate columns); ➢ Column data types (for example, orderID is INT, orderStatus is VARCHAR2(2), etc.); ➢ Column constraints, if any (for example, orderID in the jr_order...
Stock A and stock B both follow geometric Brownian motion. Changes in any short interval of...
Stock A and stock B both follow geometric Brownian motion. Changes in any short interval of time are uncorrelated with each other. Does the value of a portfolio consisting of ONE of stock A and TWO of stock B follow geometric Brownian motion? Explain your answer.
Follow a public traded stock and analyze the changes in stock price. Write a presentation and...
Follow a public traded stock and analyze the changes in stock price. Write a presentation and an executive memo of your findings. Choose one of the following companies: Amazon Alphabet Microsoft Facebook Apple In your presentation you should: Describe graphically the trend of the stock prices during the last 2 months and, in more detail, during the last 3 weeks. Explain the observed trends Compare the trends with relevant indexes Relate these trends to relevant contextual information (example: Covid crisis)....
Follow a public traded stock and analyze the changes in stock price. Write a presentation and...
Follow a public traded stock and analyze the changes in stock price. Write a presentation and an executive memo of your findings. Choose one of the following companies: Amazon Alphabet Microsoft Facebook Apple In your presentation you should: Describe graphically the trend of the stock prices during the last 2 months and, in more detail, during the last 3 weeks. Explain the observed trends Compare the trends with relevant indexes Relate these trends to relevant contextual information (example: Covid crisis)....
Create a table in SQL with foreign key reference: 1.Create the three tables without any columns...
Create a table in SQL with foreign key reference: 1.Create the three tables without any columns 2.Alter the tables to add the columns 3.Alter the tables to create the primary and foreign keys
briefly explain how the stock of manufactured capital changes over time: be sure to discuss investment...
briefly explain how the stock of manufactured capital changes over time: be sure to discuss investment and depreciation and be sure to use the “bathtub” model to illustrate the stock of manufactured capital. Explain why the Bureau of Economic Analysis defines depreciation at the consumption of fixed capital.
Write create table statements to create tables for the Exoproduct employees computers database depicted by the...
Write create table statements to create tables for the Exoproduct employees computers database depicted by the relational schema created in a mini case MC5 in chapter 3. Use insert into statements to insert no fewer than 2 and no more than 10 records per table.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT