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...
create a file in java where you can store the first name, last name and the...
create a file in java where you can store the first name, last name and the grade of the student. this will repeats until the answer of the question "are you done?" is.equals (y). then write another program that you can read from this file the student's information . I have dome most of it but my program stores only 1 student's information. WRITE IN FILE public static void main(String[] args) { System.out.println("Enter the file name"); Scanner keyboard=new Scanner(System.in); String...
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?
Specifications Create an abstract Employee class that provides private attributes for the first name, last name,...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name, email address, and social security number. This class should provide functions that set and return the employee’s first name, last name, email address, and social security number. This class has a function: get_net_income which returns 0. Create a Manager class that inherits the Employee class. This class should add private attributes for years of experience and the annual salary. This class should also provide...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT