SQL - \'Take a look at the tables below first\' ----- A system must log any inse
ID: 3858900 • Letter: S
Question
SQL -
'Take a look at the tables below first'
-----A system must log any insertion and deletion of a project AND activity AND employee into their respective audit tables via TRIGGERS and must capture the data that got added or deleted, plus the operation (ADD, DELETE), date of operation, and user who performed operation---------
Create The Triggers
1) Trg_ProjectAudit: One for Project to handle Addition, Update and Deletion from the main project table (the table that contains the most fields). Audit Table name should ProjectAudit.
2) Trg_ActivityAudit: One for Activity to handle Addition, Update and Deletion from the main activity table (the table that contains the most fields). Audit Table name should ActivityAudit
3) Trg_EmployeeAudit: One for Employee to handle Addition, Update and Deletion from the main employee table (the table that contains the most fields). Audit Table name should EmployeeAudit
Tables:
Create Table Employee
(
empNumber CHAR (8) PRIMARY KEY NOT NULL,
SSN CHAR (9) NULL,
firstName VARCHAR (25) NOT NULL,
lastName VARCHAR (25) NOT NULL,
address VARCHAR (50) NULL,
state CHAR (2) NULL,
zip CHAR (5) NULL,
job VARCHAR (50) NOT NULL,
CONSTRAINT checkJob CHECK (Job IN ('Cast Member', 'Engineer', 'Inspector', 'Project Manager'))
)
Create Table Project
(
projectID char(4) PRIMARY KEY NOT NULL,
projectName VARCHAR (50) NOT NULL,
fundedBudget DECIMAL(16,2) NULL,
firmFedID CHAR(9) NOT NULL,
startDate DATE NULL,
projectTypeCode CHAR(5) NOT NULL,
projectTypeDesc VARCHAR (50) NULL,
projectStatus VARCHAR(25) NULL,
CONSTRAINT checkProjStat CHECK (projectStatus IN ('Active', 'Inactive', 'Cancelled', 'On-Hold', 'Completed')),
projectEndDate DATE NULL,
projectManager CHAR(8) FOREIGN KEY REFERENCES employee(empNumber)
)
Create Table Activity
(
activityID CHAR (4) NOT NULL PRIMARY KEY,
activityName VARCHAR (50) NOT NULL,
activityTypeCode char (2) NOT NULL,
activityStatus VARCHAR (25) NULL,
CONSTRAINT checkActStat CHECK (activityStatus IN ('Active', 'Inactive', 'Cancelled', 'On-Hold', 'Completed')),
activityTypeDesc VARCHAR (50) NULL,
costToDate DECIMAL (16,2) NULL,
startDate DATE NULL,
endDate DATE NULL,
projectID char(4) FOREIGN KEY REFERENCES Project(projectID)
)
Create Table Firm
(
firmFedID char (9) NOT NULL PRIMARY KEY,
firmName varchar (50) NOT NULL,
firmAddress varchar (50) NULL
)
Create Table Job
(
projectID char (4) NOT NULL,
activityID char (4) NOT NULL,
)
Explanation / Answer
create table EmployeeAudit
(
EmpAuditId int,OperationType nvarchar(64),DateofOperation datetime,AcivatedByUser nvarchar(64)
)
create table ProjectAudit
(
PrjAuditId int,OperationType nvarchar(64),DateofOperation datetime,AcivatedByUser nvarchar(64)
)
create table ActivityAudit
(
ActAuditId int,OperationType nvarchar(64),DateofOperation datetime,AcivatedByUser nvarchar(64)
)
-----------emp
create trigger Trg_EmployeeAudit on Employee
after update, insert, delete
as
begin
IF EXISTS(SELECT * FROM inserted)
IF EXISTS(SELECT * FROM deleted)
insert into EmployeeAudit
select i.empNumber,'update' ,getdate(), SUSER_SNAME()
from Employee e
inner join inserted i on e.empNumber=i.empNumber
ELSE
insert into EmployeeAudit
select i.empNumber, 'insert',getdate(), SUSER_SNAME()
from Employee e
inner join inserted i on e.empNumber=i.empNumber
ELSE
IF EXISTS(SELECT * FROM deleted)
begin
insert into EmployeeAudit
select i.empNumber,'delete' ,getdate(), SUSER_SNAME()
from Employee e
inner join inserted i on e.empNumber=i.empNumber
end
end
--Activity
create trigger Trg_ActivityAudit on Activity
after update, insert, delete
as
begin
IF EXISTS(SELECT * FROM inserted)
IF EXISTS(SELECT * FROM deleted)
insert into ActivityAudit
select i.ActivityId,'update' ,getdate(), SUSER_SNAME()
from Activity e
inner join inserted i on e.ActivityId=i.ActivityId
ELSE
insert into ActivityAudit
select i.ActivityId, 'insert',getdate(), SUSER_SNAME()
from Activity e
inner join inserted i on e.ActivityId=i.ActivityId
ELSE
IF EXISTS(SELECT * FROM deleted)
begin
insert into ActivityAudit
select i.ActivityId,'delete' ,getdate(), SUSER_SNAME()
from Activity e
inner join inserted i on e.ActivityId=i.ActivityId
end
end
create trigger Trg_ProjectAudit on Project
after update, insert, delete
as
begin
IF EXISTS(SELECT * FROM inserted)
IF EXISTS(SELECT * FROM deleted)
insert into ProjectAudit
select i.projectID,'update' ,getdate(), SUSER_SNAME()
from Project e
inner join inserted i on e.projectID=i.projectID
ELSE
insert into ProjectAudit
select i.projectID, 'insert',getdate(), SUSER_SNAME()
from Project e
inner join inserted i on e.projectID=i.projectID
ELSE
IF EXISTS(SELECT * FROM deleted)
begin
insert into ProjectAudit
select i.projectID,'delete' ,getdate(), SUSER_SNAME()
from Project e
inner join inserted i on e.projectID=i.projectID
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.