Question

In: Computer Science

Using Triggers (5 pts.) Execute the following SQL to create the customer_audit table in the premier...

  1. Using Triggers (5 pts.)
    1. Execute the following SQL to create the customer_audit table in the premier schema.

CREATE TABLE IF NOT EXISTS customer_audit (

customer_num CHAR(3) NOT NULL,

  customer_name VARCHAR(35) NOT NULL,

street VARCHAR(15),

city VARCHAR(15),

state CHAR(2),

zip CHAR(5),

credit_limit DECIMAL(8,2),

date_changed DATETIME NOT NULL,

changed_by VARCHAR(45) NOT NULL);

  1. Notice that the audit table does not have a primary key defined. Explain why this might be acceptable.

  1. Based on the current attributes in the customer_audit table, suggest a possible primary key and explain why it might be unique.
  1. Create a trigger that will insert a record into the customer_audit table when the customer data is changed. Only insert a new record if the attributes that are being audited change. The attributes being audited are those that exist in the customer_audit table. For example the customer.balance attribute is not being audited so if it is the only attribute that changes as part of the transaction then we do not need to insert a new record into the audit table. Provide the SQL used to create this trigger.

  1. Write an SQL update statement that will change a customer and cause an audit record to be created. Provide the SQL statement you executed for this.

  1. Write an SQL statement that will display what is in the customer_audit table. Provide the SQL used.

Solutions

Expert Solution

Q. Notice that the audit table does not have a primary key defined. Explain why this might be acceptable. Answer:

  • I notice that Audit table does not have a primary key defined. This is acceptable because sometimes you can have tables without a primary key for storing all records that company wants to knew.
  • Basically, A table should have a primary key so that you could identify each row uniquely with it.
  • Technically, you can have tables without a primary key, but you'll be breaking good database design rules.

Q. Based on the current attributes in the customer_audit table, suggest a possible primary key and explain why it might be unique. Answer: Based on the current attributes in customer_audit table, I think an only possible primary key is customer_num attribute Because customer number is unique number for all customer.

Q. Create a trigger that will insert a record into the customer_audit table when the customer data is changed. Only insert a new record if the attributes that are being audited change. The attributes being audited are those that exist in the customer_audit table. For example the customer.balance attribute is not being audited so if it is the only attribute that changes as part of the transaction then we do not need to insert a new record into the audit table. Provide the SQL used to create this trigger.

Answer:

CREATE OR REPLACE TRIGGER display_Updation_audit BEFORE DELETE OR INSERT OR UPDATE ON customer_audit FOR EACH ROW WHEN (Customer_num > 0) DECLARE BEGIN dbms_output.put_line('New customer_num: ' || :NEW.customer_num);   dbms_output.put_line('New customer_name: ' || :NEW.customer_name);   dbms_output.put_line('New street: ' || :NEW.street);   dbms_output.put_line('New city: ' || :NEW.city);   dbms_output.put_line('New state: ' || :NEW.state);   dbms_output.put_line('New zip: ' || :NEW.zip);   dbms_output.put_line('New credit_limit: ' || :NEW.credit_limit);   dbms_output.put_line('New date_changed: ' || :NEW.date_changed);     dbms_output.put_line('New changed_by: ' || :NEW.changed_by);    END;

Use of Trigger:

  • Maintain Audit of database
  • store history for certain action of query

Q. Write an SQL update statement that will change a customer and cause an audit record to be created. Provide the SQL statement you executed for this.

Answer:

  • SQL Statement:
  • UPDATE customer_audit  SET credit_limit = 5000 WHERE credit_limit > 5000;
  • When we execute this query in database then after sometimes customer shopping and they reach their to credit_limit but if customer wants to buy more product then customer have to create new customer_ num for them And that cause an audit record to be created.

Q. Write an SQL statement that will display what is in the customer_audit table. Provide the SQL used.

Answer:

SQL Statement:

  • SELECT * FROM customer_audit;
  • This will display all the records present in customer_audit.

SQL Use:

  • SQL is used for administering, updating, maintaining and manipulating the databases or tables
  • SQL is usually used to fetch data, update the records,attributes of the table, or operate on the structure of the database or tables.
  • SQL can be used for both relational and multidimensional types of databases.

Related Solutions

Using SQL create a new database called school_app. Create a student table with fields id (auto...
Using SQL create a new database called school_app. Create a student table with fields id (auto increment), first_name, last_name. Create a course table with fields id (auto increment), course code (such as ITC or MTH), and course number (such as 100 or 295). Note that the relationship between student and course is many-to-many (n:m). Create a join table called student_course that implements the n:m relationship with fields id (auto increment), student_id, course_id, and grade (which has values 0, 1, 2,...
Create a ‘Student’ table using SQL query. The table must have at least five attributes from...
Create a ‘Student’ table using SQL query. The table must have at least five attributes from your choice with different data types.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
Use a single SQL statement to create a relational table and to load into the table...
Use a single SQL statement to create a relational table and to load into the table department name, subject code, year of running and session of running that offered by the departments. Note that a running subject offered by a department means a lecturer of the department has been assigned to teach the subject. Next, enforce the appropriate consistency constraints on the new table.    When ready use SELECT statement to list the contents of the relational table created and...
Create a new SQL table in your phpMyAdmin account that collects the following fields (columns) -  (10...
Create a new SQL table in your phpMyAdmin account that collects the following fields (columns) -   1) The name of a medicine, sanitizer, or tip/trick for protecting yourself against transmission or fight the disease - STRING 2) The amount of money the item costs - NUMBER 3) The UPC code of the item - NUMBER 4) Manufacturing country of the item - STRING Connect to the SQL database with your HTML code, and make sure to show the results of...
Write the SQL DDL to create the following 5 tables for an App store: Publisher, Category,...
Write the SQL DDL to create the following 5 tables for an App store: Publisher, Category, App, AppVersion, AppVersionReview: A Publisher table where each publisher is identified by an integer id and has a name (up to 40 characters). (1 mark) A Category table where each category has an id (integer), a name (up to 50 characters), and a parentId to identify its parent category. The parentId should be a foreign key to the Category table. (1.5 marks) An App...
Using SQL Developer ONLY! Lab 5 1. Create a lab report file (MS Word compatible) and...
Using SQL Developer ONLY! Lab 5 1. Create a lab report file (MS Word compatible) and name it as “IT4153_Lab 5_ Your D2L ID”. a. Create two tables: Employee: empID (PK), empFname, empLname, deptID(FK) and Department: deptID(PK), deptName, chairID chairID is empID from Employee table b. Insert at least 3 rows in the Department table and at least 6 rows in the Employee table. c. Create trigger on update of chairID that enforces the following business rules • One employee...
Create a table in SQL with foreign key reference: 1.Create the three tables without any columns...
Create a table in SQL with foreign key reference: 1.Create the three tables without any columns 2.Alter the tables to add the columns 3.Alter the tables to create the primary and foreign keys
Please write the SQL statement for the following. I am using the Adventurework2014 database. 1. Create...
Please write the SQL statement for the following. I am using the Adventurework2014 database. 1. Create a login for AdventureWorks employees. An employee login should be composed of the first letter of a person's first name combined with their last name. In addition, the login should be all lower case characters. All the required information is located in Person.Person table. Employees can be identified by "EM" value in the PersonType field. The output should include BusinessEntityID, first name (FirstName), last...
Assignment 2- 9   USING A FOR LOOP Create a PL/SQL block using a FOR loop to...
Assignment 2- 9   USING A FOR LOOP Create a PL/SQL block using a FOR loop to generate a payment schedule for a donor’s pledge, which is to be paid monthly in equal increments. The values variable for the block is starting payment due date, monthly payment amount, and a number of total monthly payments for the pledge. The list that’s generated should display a line for each monthly payment showing payment number, date due, payment amount, and donation balance (remaining...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT