Question

Use the Northwind database. Choose the best answer from those given below. Sometimes a database may...

Use the Northwind database. Choose the best answer from those given below.

Sometimes a database may have a separate table to log certain activities taking place in the database.
Create a new table named ‘ChangeLog’ in the Northwind database as follows:
ChangeID int   identity(1,1)    primary key
EmpID       int                             (will contain the ID of the employee being changed)
User          nvarchar(30)           (will contain the login of the user making the change)
Date          smalldatetime        (will contain the date of the change)
OldRate    money                     (will contain the old payrate of the employee)
NewRate  money                     (will contain the new payrate of the employee)

You also need to Alter the Employees table and add a PayRate Column and set a pay rate for the employees.
CREATE TRIGGER payrate_change ON Employees FOR UPDATE
As
If UPDATE (PayRate) ...
After creating the ChangeLog table, create a trigger on the Employees table for UPDATE. Use this trigger to monitor the PayRate column, and if the PayRate of an employee gets changed, have the trigger insert a row into the ChangeLog table with the appropriate data. [Use the function suser_sname() to get the user making the change and insert this into the user column.]

Why is the given solution wrong:
CREATE TRIGGER payrate_change ON [dbo].[Employees]
FOR UPDATE
As
declare @empid int
declare @user nvarchar(50)
declare @date smalldatetime
declare @oldRate money
declare @newRate money
select @empid = e.EmployeeID from [dbo].[Employees] as e;
select @oldRate = e.PayRate from [dbo].[Employees] as e;
If UPDATE(PayRate)
BEGIN
set @user = SUSER_SNAME();
set @date = GETDATE();
select @newRate = e.PayRate from [dbo].[Employees] as e;
insert into [dbo].[ChangeLog](EmpID,[User],[Date],OldRate,NewRate)
values (@empid, @user, @date, @oldRate, @newRate)
END

Based upon the trigger problem from above, pick the correct solution(s) below. (Can be several)

A -

CREATE TRIGGER payrate_change ON Employees FOR UPDATE
AS
DECLARE @EmpID int;
DECLARE @User nvarchar(30);
DECLARE @Date smalldatetime;
DECLARE @OldRate money;
DECLARE @NewRate money;
IF UPDATE (PayRate)
BEGIN
SELECT @OldRate = (SELECT PayRate FROM DELETED);
SELECT @NewRate = (SELECT Payrate FROM INSERTED);
SELECT @EmpID = (SELECT EmployeeID FROM INSERTED);
SELECT @User = suser_sname();
SELECT @Date = getdate();  
INSERT INTO ChangeLog(EmpID, [User], [Date], OldRate, NewRate)
VALUES (@EmpID, @User, @Date, @OldRate, @NewRate);
END

B -

create Trigger payrate_change ON Employees FOR UPDATE
As
Declare @EmplID int
Declare @user nvarchar(30)
Declare @date smalldatetime
Declare @OldRate money
Declare @NewRate money
Select @EmplID=Employees.employeeID from employees
Select @user = suser_sname()
Select @date = getdate()
Select @oldrate = payrate from deleted
Select @newrate = payrate from inserted
If UPDATE (PayRate)
Insert into ChangeLog
( EmplID , [User] , [Date] , OldRate , NewRate )
Values ( @EmplID, @user, @date, @OldRate, @NewRate)

C -

CREATE Trigger payrate_change
ON Employees FOR UPDATE
As
DECLARE @OldRate money
DECLARE @NewRate money
DECLARE @EmpID int
If UPDATE (PayRate)
BEGIN
SELECT @OldRate - (SELECT PayRate FROM Deleted)
SELECT @NewRate - (SELECT PayRate FROM Inserted)
SELECT @EmpID - (SELECT EmployeeID FROM Inserted)
INSERT INTO ChangeLog (EmpID, User, OldRAte, NewRate)
VALUES (@EmpID, suser_sname(), @OldRate, @NewRate)
END;
ELSE
BEGIN
PRINT 'Commands executed successfully'
END

D -

CREATE TRIGGER payrate_change ON Employees FOR UPDATE
AS
DECLARE @OldRate money
DECLARE @NewRate money
DECLARE @EmpID = int
IF UPDATE (PayRate)
BEGIN
SELECT @OldRate = (SELECT PayRate FROM Deleted)
SELECT @NewRate = (SELECT PayRate FROM Inserted)
SELECT @EmpID = (SELECT EmployeeID FROM Employees WHERE EmpID = Deleted.EmpID )  
INSERT INTO ChangeLog (EmpID, [User], [Date], NewRate, OldRate)
VALUES (@EmpID, suser_sname(), getdate(), @OldRate, @NewRate)
END;
ELSE
BEGIN
PRINT 'Commands executed successfully'
END;

E -

CREATE TRIGGER payrate_change ON Employees FOR UPDATE
AS
DECLARE @OldRate money
DECLARE @NewRate money
DECLARE @EmpID int
IF UPDATE (PayRate)
BEGIN
SELECT @OldRate = (SELECT PayRate FROM Deleted)
SELECT @NewRate = (SELECT PayRate FROM Inserted)
SELECT @EmpID = (SELECT EmployeeID FROM Employees WHERE 1 =1)  
INSERT INTO ChangeLog (EmpID, [User], [Date], NewRate, OldRate)
VALUES (@EmpID, suser_sname(), getdate(), @OldRate, @NewRate)
END;
ELSE
BEGIN
PRINT 'Commands executed successfully'
END;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

create table changelog(ChangeID int, identity number(1,1) primary key, EmpID int, User nvarchar(30), Date smalldatetime, OldRate money, NewRate money);

alter table employees add(PayRate int);

update employees set Payrate = 100;

CREATE TRIGGER payrate_change ON Employees FOR UPDATE
As
Declare
begin
insert into changelog values(:old.cid, :old.identity, :oldempid, :old.user, :old.date, :old.oldrate, :old.newrate);

end;

Add a comment
Know the answer?
Add Answer to:
Use the Northwind database. Choose the best answer from those given below. Sometimes a database may...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NEED TO ADD COMMENTS ON WHAT EACH PIECE OF SQL CODE DOES CREATE TABLE Production.ProductAuditTrail (AuditID...

    NEED TO ADD COMMENTS ON WHAT EACH PIECE OF SQL CODE DOES CREATE TABLE Production.ProductAuditTrail (AuditID INT IDENTITY(1,1), AuditDate DATETIME NOT NULL, ChangeUser SYSNAME NOT NULL, ProductID INT NOT NULL, BeforeListPrice MONEY NOT NULL, AfterListPrice MONEY NOT NULL) GO CREATE TRIGGER tu_ProductAuditTrail ON Production.Product FOR UPDATE AS INSERT INTO Production.ProductAuditTrail (AuditDate, ChangeUser, ProductID, BeforeListPrice, AfterListPrice) SELECT GETDATE(), SUSER_SNAME(), i.ProductID, d.ListPrice, i.ListPrice FROM inserted i INNER JOIN deleted d ON i.ProductID = d.ProductID GO SELECT * FROM Production.ProductAuditTrail GO UPDATE Production.Product...

  • Questions are based on the Northwind database. a. A data dictionary (Excel file) that describes each...

    Questions are based on the Northwind database. a. A data dictionary (Excel file) that describes each of the tables. (Attached in question) b.    Write good, clean SQL that answers the following questions. c.     Separate your queries as I have done in the following example. End each query with a semicolon (;) and insert the word "go" between each query. Queries can span multiple lines if required. Select CustomerID from Customers; go Select Count(*) from Employees; go Select max(productID) from Products; 18. Produce...

  • --Create procedure for GetPartNums in SQL --GetPartNums- retrieve all part nums from the database --Database if...

    --Create procedure for GetPartNums in SQL --GetPartNums- retrieve all part nums from the database --Database if db_id('TestPremierProducts') is not null begin use master alter database TestPremierProducts set SINGLE_USER with rollback immediate drop database TestPremierProducts end create database TestPremierProducts go USE TestPremierProducts; go /* create the tables */ CREATE TABLE Rep (RepNum int Identity(1,1), LastName VARCHAR(15), FirstName VARCHAR(15), Street VARCHAR(15), City VARCHAR(15), State VARCHAR(2), Zip VARCHAR(5), Commission MONEY, Rate DECIMAL(3,2), PRIMARY KEY (RepNum)); go CREATE TABLE Customer (CustomerNum int Identity(1,1) PRIMARY...

  • 1. This trigger is from Figure 16.2 of the text. Copy and run this trigger so it is created in th...

    1. This trigger is from Figure 16.2 of the text. Copy and run this trigger so it is created in the ap database. You can run the UPDATE below to test it. Notice two things. The error message appears as a pink screen just like a syntax error would. Also, if you then query the invoices table, the UPDATE statement did not execute. The trigger prevented the erroneous UPDATE from changing the data.                 DELIMITER // CREATE TRIGGER invoices_before_update    BEFORE UPDATE...

  • - - Requirements Building upon your project 1 and project 2, the park will be a Star Wars themed ...

    - - Requirements Building upon your project 1 and project 2, the park will be a Star Wars themed park. You must design additional parts of the database and create the following SQL Script. Step 1: Create table audits via triggers The system must log any insertion, deletion, or updates to the following tables Employee table (project 1) Job table (project 1) ProjectMain table (Project 2) ActivityMain table (Project 2) . . For each one of the table above, you...

  • Step 1: Create table audits via triggers The system must log any insertion, deletion, or updates...

    Step 1: Create table audits via triggers The system must log any insertion, deletion, or updates to the following tables: • Employee table (project 1) create table Employee ( empNumber char(8) not null, firstName varchar(25) null, lastName varchar(25) null, ssn char(9) null, address varchar(50) null, state char(2) null, zip char(5) null, jobCode char(4) null, dateOfBirth date null, certification bit null, salary money null, constraint PK_EMP PRIMARY KEY(empNumber), constraint EMP_STATECHECK CHECK(state in ('CA','FL')) ) GO • Job table (project 1) create...

  • For each question below select the best answer from those listed and give your reasoning. Your...

    For each question below select the best answer from those listed and give your reasoning. Your reasoning need only be a sentence or two. It is not enough to get the right answer, you must know why it is the right answer. Question 5 Fred's friend claimed that Canadians tend to be jerks. Fred wondered if that was true, and tested it by checking to see how many Canadian jerks he could think of. Fred's cognitive strategy is             ["the availability...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT