Question

In: Computer Science

Create a table with two columns. Name the table First Initial _ Last Name (e.g. John...

Create a table with two columns. Name the table First Initial _ Last Name (e.g. John Dow will create table j_dow). You have to audit all DML statements on your table. To do this you write two triggers: 1. To log any DML statements that users might run on this table. The results must be stored in the First Initial _ Last Name _ Log table (e.g. John Dow will create table j_dow_log). The table should have unique event ID, values for both the Oracle and the system user who ran the query, the time it was executed and the type of DML query user ran. 2. To capture any data that was changed in the table. The results must be stored in the First Initial _ Last Name _ History table (e.g. John Dow will create table j_dow_history). The table should reference the event ID from the log table, and store both old and new values for both columns.

Solutions

Expert Solution

Answer:

Here i have user SQL Server 2012 for creating the sql scripts, tables and views

Below is the SQL query for creating tables j_dow, j_dow_log and j_dow_history

j_dow Table

This table holds the user data and has two columns First Name and Last Name, the script to create j_dow table is given below

CREATE TABLE [dbo].[j_dow](

[Id] [int] IDENTITY(1,1) NOT NULL,

[FirstName] [varchar](100) NULL,

[LastName] [varchar](100) NULL

) ON [PRIMARY]

GO

j_dow_log Table

This table holds the log such as user who triggers and the session both combined in one column EventId (username_session), the time the script executed and the sql script ran by the user

CREATE TABLE [dbo].[j_dow_log](

[EventId] [varchar](150) NULL,

[TimeExecuted] [datetime] NULL,

[QueryCommand] [varchar](max) NULL,

[Id] [int] IDENTITY(1,1) NOT NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

j_dow_history Table

This table holds the log information of INSERT, UPDATE and DELETE. If user do any DML operation in the j_dow table this table records the old value and new value and the column name it get changed

CREATE TABLE [dbo].[j_dow_history](

[Id] [int] IDENTITY(1,1) NOT NULL,

[LogEventId] [varchar](150) NULL,

[OldValue] [varchar](150) NULL,

[NewValue] [varchar](150) NULL,

[ColumnName] [varchar](150) NULL

) ON [PRIMARY]

GO

The select query screen for all three tables are below

Now we have to create trigger for j_dow table one to log the sql query tracking and other to log the data change

The Query is given below

  • j_dow_log trigger for recording the time of executing and captures user info who executed it

CREATE TRIGGER [dbo].[j_dow_trigger] ON [dbo].[j_dow]

FOR INSERT, UPDATE, DELETE

AS

DECLARE @TEMP TABLE

(EventType NVARCHAR(30), Parameters INT, EventInfo NVARCHAR(4000))

INSERT INTO @TEMP EXEC('DBCC INPUTBUFFER(@@SPID)')

DECLARE @SQLQuery VARCHAR(MAX)

(SELECT Top 1 @SQLQuery = EventInfo FROM @TEMP)

INSERT INTO j_dow_log (EventId, TimeExecuted, QueryCommand)

SELECT

SUSER_NAME() + '_' + convert(varchar(50), @@spid)

,

CURRENT_TIMESTAMP

,

@SQLQuery

  • And the other trigger to reocrd the data change is given below

CREATE TRIGGER [dbo].[j_dow_history_trigger] ON [dbo].[j_dow]

FOR INSERT, UPDATE, DELETE

AS BEGIN

IF @@ROWCOUNT = 0 RETURN;

Declare @MaxEventId INT

SELECT @MaxEventId = MAX(Id) FROM j_dow_log

INSERT INTO j_dow_history

SELECT @MaxEventId,d.FirstName AS [OldValue], i.FirstName AS [NewValue], 'FirstName'

FROM inserted i

FULL OUTER JOIN deleted d ON i.Id = d.Id

INSERT INTO j_dow_history

SELECT @MaxEventId, d.LastName AS [OldValue], i.LastName AS [NewValue], 'LastName'

FROM inserted i

FULL OUTER JOIN deleted d ON i.Id = d.Id

END;

  • Once after executing all the scripts that given above the table will look like in the below screen

When trigger executes you can see following messages in output window

Thank you:)


Related Solutions

Create a table ‘StudentInfo’ with following fields: ID First Name Last Name SSN Date of Birth...
Create a table ‘StudentInfo’ with following fields: ID First Name Last Name SSN Date of Birth Create a table ‘ClassInfo’ table: ID Class Name Class Description Create a table ‘RegisteredClasses’ table: StudentID ClassID The RegisteredClasses table should have a foreign key relationship to StudentInfo and ClassInfo tables for the respective IDs. Also the IDs in StudentInfo and ClassInfo need to be primary keys. When you submit the file your email should also contain the following SQL Queries: Query to show...
"Create a program that displays a table consisting of four rows and five columns. The first...
"Create a program that displays a table consisting of four rows and five columns. The first column should display the numbers 1 through 4. The second and sub-sequent columns should display the result of multiplying the number in the first column by the numbers 2 through 5. If necessary, create a new project named Introductory14 Project, and save it in the Cpp8\Chap08 folder. Enter the C++ instructions into a source file named Introductory14.cpp. Also enter appropriate comments and any additional...
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
1 – Create a webpage that contains a table with exactly three rows and two columns....
1 – Create a webpage that contains a table with exactly three rows and two columns. The first row will contain a table heading containing the name of a US National Park, that spans across all columns. Hint: use the colspan attribute inside the opening th tag Give the table heading an onmouseover that will change the text of the heading when it is moused over to read My Favorites Park! (Hint: use innerHTML). Use onmouseout to change it back....
1 – Create a webpage that contains a table with exactly three rows and two columns....
1 – Create a webpage that contains a table with exactly three rows and two columns. The first row will contain a table heading containing the name of a US National Park, that spans across all columns. Hint: use the colspan attribute inside the opening th tag Give the table heading an onmouseover that will change the text of the heading when it is moused over to read My Favorites Park! (Hint: use innerHTML). Use onmouseout to change it back....
How do I get the first initial of a first, middle, and last name? Also when...
How do I get the first initial of a first, middle, and last name? Also when I look to count characters in the name I want to be abel to count the spaces in-between the names how can i do this?
Part 1: Create a character array and save your first and last name in it
PROGRAMMING IN C:Part 1:Create a character array and save your first and last name in itNote: You can assign the name directly or you can use the scanf function.Display your name on the screen.Display the address (memory location) in hexadecimal notation of the array. (hint: use %p)Use a for loop to display each letter of your name on a separate line.Part 2:Create a one dimensional array and initialize it with 10 integers of your choice.Create a function and pass the...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name,...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate...
Create a table showing the payment of a mortgage of $239,000 month by month. Create columns...
Create a table showing the payment of a mortgage of $239,000 month by month. Create columns for: - Time (in years) - Interest (for that month) - Payment (always the same value) - Payment against principal - Remaining Principal The mortgage is to last 33 years, and the nominal interest rate is 6.03% (a) What is the monthly payment? (b) What is the interest paid at the end of the 9th month? (c) How much of the principal is paid...
c# language Create a class “Person” which included first name, last name, age, gender, salary, and...
c# language Create a class “Person” which included first name, last name, age, gender, salary, and havekids (Boolean) variables. You have to create constructors and prosperities for the class. Create a “MatchingDemo” class. In the main function, the program reads the number of people in the database from the “PersonInfo.txt” file and creates a dynamic array of the object. It also reads the people's information from “PersonInfo.txt” file and save them into the array. Give the user the ability to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT