Question

Please write SQL Query for following questions using Northwind Database: 1. Create a procedure to add...

Please write SQL Query for following questions using Northwind Database:

1. Create a procedure to add a new record to Territories table. Call it NewTerr. Add the following records using the procedure.

TerritoryID       

TerritoryDescription

    Region

88888

Brooklyn

1

99999

Waco

4

77777

Long Beach

2

2. Create an Update Procedure that will change the Region from 1 to 3 for record with TerritoryID 88888. Call it Update88.

3. Create a Delete Procedure that will delete the one record from the Territories table that has Waco in the TerritoryDescription.

4. Define a trigger. Give an example when it should be used.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SQL Solution

Stored Procedures
Stored procedures are set of sql statements(block) to perform some actions on database and which can be called from various applications.

Syntax of Stored Procedure:
CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters]
AS   
Declaration section
BEGIN   
Execution section

END

The following SQL statements creates stored procedures that insert,update and delete records to/from the Territories table:


1.
CREATE OR REPLACE PROCEDURE NewTerr (TerrID INT, TerrDescr varchar(50),region INT)
AS
BEGIN
insert into Territories values(TerrID,TerrDescr,region);
END

2.
CREATE OR REPLACE PROCEDURE Update88
AS
BEGIN
Update Territories set Region=3 where TerritoryID=88888 and Region=1;
END
3.
CREATE OR REPLACE PROCEDURE deletewaco
AS
BEGIN
Delete from Territories where TerritoryDescription='Waco';

END

Triggers
Triggers are special type of stored procedures that executes automatically when specific actions occur in database like insert, update or delete records.
Triggers can be used 'BEFORE','AFTER' or 'INSTEADOF' any SQL actions like 'INSERT','DELETE' or 'UPDATE' records.

Eg;-
Suppose you have another table 'Terrhistory ' to store action history on Territories table: then
to store history on insert record we can create a trigger:

Eg1.
CREATE OR REPLACE TRIGGER TerrAction
BEFORE INSERT ON Territories
FOR EACH ROW  
WHEN (NEW.TerritoryID > 0)  

BEGIN  
insert into Terrhistory values(NEW.TerritoryID,'ADDED')
END;  

Eg2. Before delete record

CREATE OR REPLACE TRIGGER TerrActionDel
BEFORE DELETE ON Territories
FOR EACH ROW  
WHEN (NEW.TerritoryID > 0)  

BEGIN  
insert into Terrhistory values(NEW.TerritoryID,'DELETED')
END;  

Add a comment
Know the answer?
Add Answer to:
Please write SQL Query for following questions using Northwind Database: 1. Create a procedure to add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create the database and tables for the database. Show all SQL statements. Include primary and foreign...

    Create the database and tables for the database. Show all SQL statements. Include primary and foreign keys. Insert data into each table. Show select statements and display the output of each table. Note:Student’s name must be inserted into table as part of the data! Perform the SQL below: Query one table and use WHERE to filter the results. The SELECT clause should have a column list, not an asterisk (*). State the purpose of the query; show the query and...

  • Please access to TUN SQL/Assistant using the default database = db_pvfc12_std 1.- Run a query to...

    Please access to TUN SQL/Assistant using the default database = db_pvfc12_std 1.- Run a query to retrieve all the data field from db_pvfc12_std in table CUSTOMER_T 2.- Run a query to retrieve record --> CustomerID = 11, How many records did you get and why? 3.- Run a query to retrieve all the data field from table Order_T               Once retrieved:                              3.1.- Please name the fields showing: Primary Key, Foreign Key and other fields                              3.2.- What type of...

  • I am using Oracle SQL Live so please write the SQL Query in the format that...

    I am using Oracle SQL Live so please write the SQL Query in the format that Oracle SQL Live can run I need to create a trigger that will update the Product QoH when a new product is purchased. (A new product is purchased when a row is added to the line table). I have linked the code of the script since it exceed Chegg’s Character Limit: https://docs.google.com/document/d/1HbHnMrk6Qw99B72kpDyYCFibUJVsYEi-6RKDsmb3fg4/edit?usp=sharing

  • The Northwind database created by Microsoft contains the sales data for a fictitious company called Northwind...

    The Northwind database created by Microsoft contains the sales data for a fictitious company called Northwind Traders, which imports and exports specialty foods from around the world. Here is the schema of the database: Products (ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued); Suppliers (SupplierID, CompanyName, ContactName , ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax, HomePage); Categories (CategoryID, CategoryName, Description, Picture); Orders (OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia,  Freight, ShipName,ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry); Order_details (ID, OrderID,...

  • The procedure is a sample and needs to be modified. The software used is oracle 11g. Provide screen shot of the program running. EX.NO. 6 SQL TRIGGER AIM To develop the package for employee managemen...

    The procedure is a sample and needs to be modified. The software used is oracle 11g. Provide screen shot of the program running. EX.NO. 6 SQL TRIGGER AIM To develop the package for employee management system using SQL triggers. DATABASE SCHEMA EMP (EMPID, NAME, SALARY, COMMISSION, DEPT NO) PROCEDURE STEP 1: Start STEP 2: Initialize the trigger with specific table id STEP 3: Develop and execute SQL trigger to carry out the following action. Create a trigger on EMP table...

  • Oracle PL/SQL Using Dynamic SQL to create a procedure named add_row_sp for inserting a row into...

    Oracle PL/SQL Using Dynamic SQL to create a procedure named add_row_sp for inserting a row into a table with three columns: add_rows (p_table_name VARCHAR2, p_column1 NUMBER, p_column2 VARCHAR2, p_column3 NUMBER). Testing the procedure by using the following block to add a record to the BB_TAX table: BEGIN add_row_sp ('BB_TAX', 4, 'SD', 0.02); END;

  • Database For this lab you will be using SQL SELECT statements to query your database tables....

    Database For this lab you will be using SQL SELECT statements to query your database tables. You will be turning in the results of the following queries: 1. List all Patients and what Bed they are assigned to 2. List all patients who had Treatments and what Treatment they received 3. List all patients who had tests and what Test they had 4. List the employees (doctors, nurses, etc.) who assisted each patient. 5. List all patients in alphabetical order...

  • Using the MySQL Workbench create a new database using your design specifications Add at least 10...

    Using the MySQL Workbench create a new database using your design specifications Add at least 10 records to your tables. Note: Certain tables may not require 10 records and that is ok as long as your main tables have 10 or more Create MySQL statements that will retrieve all records or rows from the tables in your database Create 10 MySQL statements that will retrieve specified records or rows from one table in your database Create 10 MySQL statements that...

  • Create the following SQL Server queries that access the Northwind database Same as problem 3 but,...

    Create the following SQL Server queries that access the Northwind database Same as problem 3 but, limit the list to all customers who placed 3 or more orders. The product id, product name, last date that the product was ordered for all items in the Grains/Cereals category. (Hint: Use MAX) The product ID, product name, and number of distinct customers who ordered that product in 1996 Thank you. Categories Customers Employees Order Details Orders Products Shippers 9 CategoryID CategoryName Description...

  • SQL QUERIES 1) Using the Product and PC relations, create both tables in your database and...

    SQL QUERIES 1) Using the Product and PC relations, create both tables in your database and insert all the data. Show the SQL statements to create each table and show a representative SQL insert statement for the data in each table (i.e. you do not need to show insert statements for all the data). – For the remaining questions, assume that your SQL is part of a program function and the function provides the input needed for your SQL query....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT