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 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....
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...
1. Write the statements to create a table named REQUIREMENTS. The table has the following columns:...
1. Write the statements to create a table named REQUIREMENTS. The table has the following columns: credits number (primary key) and degree name. 2. Write the statements to create a table named CANDIDATE with the following columns names. Pick the best column type: student_id, first name, last name, credits and graduation date. The credits column is a foreign key to the credits column in the REQUIREMENTS table. 3. Write the statement(s) to Insert 2 rows in your REQUIREMENTS table. Make...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance for the company as of July 31, 2017. The last two columns contain the adjusted trial balance as of the same date. Unadjusted Trial Balance Adjusted Trial Balance Cash $ 108,120 $ 108,120 Accounts receivable 10,000 20,000 Office supplies 17,200 6,500 Prepaid insurance 7,540 3,160 Office equipment 90,000 90,000 Accum. Depreciation—Office equip. $ 25,000 $ 32,000 Accounts payable 10,100 24,000 Interest payable 0 3,000...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance for the company as of July 31, 2017. The last two columns contain the adjusted trial balance as of the same date. Unadjusted Trial Balance Adjusted Trial Balance Cash $ 34,000 $ 34,000 Accounts receivable 14,000 22,000 Office supplies 16,000 2,000 Prepaid insurance 8,540 2,960 Office equipment 84,000 84,000 Accum. Depreciation—Office equip. $ 14,000 $ 20,000 Accounts payable 9,100 10,000 Interest payable 0 1,000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT