SlideShare une entreprise Scribd logo
1  sur  16
BRANCH
BRANCH _MANAGER
BRANCH_SUPPLIERS
CUSTOMER
INVOICE
ORDERS
ORDER_PART
PARTS
PAYMENT
SERVICE
SERVICE_STOCK
SERVICE _TYPE
SPECIALIST
STAFF
STOCK
SUBPART
SUPPLIERS
VEHICLE
VEHICLE_PARTS
3.1
CREATE OR REPLACE FUNCTION INVOICETOTAL( --- This function will apply to
InvoiceTotal
I_IN_ID INVOICE.IN_ID%TYPE) --- the customer bill Return Number
RETURN NUMBER
IS
V_INVOICE_TOTAL INVOICE.TOTAL%TYPE;
BEGIN
SELECT SUM(LABOUR_COST+SET_PRICE+P_PRICE) --- The select statement will
sum labour cost, service price and part price
INTO V_INVOICE_TOTAL
FROM SERVICE,SERVICE_TYPE,PARTS,STOCK,SERVICE_STOCK --- will retrieve
details from this table
WHERE SERVICE_TYPE.SET_ID=SERVICE.SET_ID --- join tables together using
thier primary key
AND PARTS.P_ID=STOCK.P_ID
AND STOCK.ST_ID=SERVICE_STOCK.ST_ID
AND SERVICE.SER_ID=SERVICE_STOCK.SER_ID
AND SERVICE.SER_ID = I_IN_ID;
RETURN V_INVOICE_TOTAL; --- returns v_invoice_total back to procedure
END ;
/
CREATE OR REPLACE PROCEDURE POP_AMOUNTOTAL --- creat procedure to populate
AmountTotal
AS
CURSOR C_INVOICE --- cursor that holds the select statement
IS
SELECT SER_ID --- create a select statment
FROM SERVICE;
-- SELECT IN_ID
-- FROM INVOICE;
V_IN_ID INVOICE.IN_ID%TYPE; --- declare variables
V_INVOICE_TOTAL INVOICE.TOTAL%TYPE;
BEGIN
OPEN C_INVOICE;
LOOP --- will execute sequence of statement
FETCH C_INVOICE INTO V_IN_ID; --- fetches the invoice into the
variable V_IV_ID
EXIT WHEN C_INVOICE%NOTFOUND; --- exit loop if condition is true
V_INVOICE_TOTAL := INVOICETOTAL(V_IN_ID); /* calls function, with SER_ID
*/
DBMS_OUTPUT.PUT_LINE('TOTAL AMOUNT IS :'|| TO_CHAR(V_INVOICE_TOTAL));
-- UPDATE INVOICE --- Update the invoice total
-- SET TOTAL=V_INVOICE_TOTAL
-- WHERE IN_ID=V_IN_ID;
END LOOP;
DBMS_OUTPUT.PUT_LINE('TOTAL = ' || TO_CHAR(V_INVOICE_TOTAL));
CLOSE C_INVOICE;
END;
/
3.2. CREATE OR REPLACE TRIGGER LIMIT_BRANCH_TO_10_SERVICE --- create a
before trigger to check service
BEFORE UPDATE OR INSERT ON SERVICE
FOR EACH ROW
DECLARE
SERVICECOUNT NUMBER; --- declare variables
MAX_SERVICE NUMBER := 10;
BEGIN
SELECT COUNT(*) --- The select statement will count number of
INTO SERVICECOUNT --- vehicle serviced in a particular branch
FROM SERVICE --- statement count all from service table
WHERE S_DATE = :NEW.S_DATE
AND B_ID = :NEW.B_ID;
IF SERVICECOUNT <= MAX_SERVICE THEN --- will be raised when above if
statment equals true
RAISE_APPLICATION_ERROR (-20000,'BRANCH_' ||:NEW.B_ID || ' IS FULL
FOR SERVICES. PLEASE TRY ANOTHER BRANCH');
END IF;
END;
/
3.3.
CREATE OR REPLACE TRIGGER CHECK_STOCK --- CREATE A TRIGGER TO CHECK_STOCK
BEFORE INSERT OR UPDATE OF QINS,MOH ON STOCK
FOR EACH ROW
DECLARE
CURSOR C_STK IS --- CREATE A CURSOR WITH THE
SELECT STATEMENT BELOW
SELECT S_NAME,SU_NAME
FROM STAFF E, SUPPLIERS S, BRANCH B, BRANCH_SUPPLIERS A --- STATEMENT
FETCHES STAFF NAME AND SUPPLIER NAME
WHERE E.S_ID=:new.S_ID
AND B.B_ID=:new.B_ID
AND S.SU_ID = A.SU_ID;
V_S_N STAFF.S_NAME%TYPE;
V_SU_N SUPPLIERS.SU_NAME%TYPE;
BEGIN
OPEN C_STK; --- opens cursor and populates
FETCH C_STK INTO V_S_N,V_SU_N; --- variables V_S_N and V_SU_N
IF:NEW.QINS<= :NEW.MOH THEN --- checks newly inserted/updated stock
quantity against minimum stock
:NEW.REORDER := 'YES'; --- if stock quantity lower than
minimum stock then display the msg below
DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');---
message will display staff name and
DBMS_OUTPUT.PUT_LINE(V_S_N||' RE-ORDER STOCK FOR '|| :NEW.ST_ID||' FROM
'||V_SU_N); --- supplier name for that stock
DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
ELSE
:NEW.REORDER :='NO'; --- if stock quantity higher than
minimum stock then display the msg below
DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXX');
DBMS_OUTPUT.PUT_LINE('STOCK LEVEL GOOD FOR '|| :NEW.ST_ID);
DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXX');
END IF;
END;
/
Below is the stock quantity in stock and minimum in hand.
After the trigger is created, stock ST81 is updated to be lower than minimum in hand, and the
message display below.
The table above shows which stock needs to be reordered.
When the stock quantity is above the minimum stock, the message above display for that stock.
The minimum quantity in stock table above, displays message when it’s updated
INTEGRITY CONSTRAINTS
4.1. Customer gender should be recorded as ‘M’ or ‘F’.
ALTER TABLE CUSTOMER ADD CONSTRAINT GENDER CHECK (C_GENDER IN ('M','F'));
4.2. The Supplier’s name must be Unique and not null.
ALTER TABLE SUPPLIERS ADD CONSTRAINT SU_NAME UNIQUE (SU_NAME) MODIFY
SU_NAME NOT NULL;
4.3. Implement at least one table that demonstrate primary and foreign key constraints using
the ALTER TABLE command.
ALTER TABLE CITY ADD PRIMARY KEY (CITY_ID) ADD FOREIGN KEY (COUNTRY_ID)
REFERENCES COUNTRY (COUNTRY_ID);
END USER QUESTIONS
4.4 Can you list all customers’ details?
SELECT * FROM CUSTOMER;
4.5 List all the customers, their car details in for a service and the mechanic who made the
booking.
SELECT
A.C_NO,D.V_ID,A.V_MAKE,A.V_MODEL,A.CURRENT_MILEAGE,D.S_ID_BK,D.SER_ID
FROM SERVICE D,CUSTOMER C,STAFF B,VEHICLE A
WHERE A.C_NO=C.C_NO
AND D.V_ID=A.V_ID
AND D.S_ID_BK=B.S_ID;
4.6. List by branch the number of cars booked in for service at each branch order by date.
SELECT C.B_ID,B.NAME,C.V_ID,A.V_MAKE,C.SER_ID,C.S_DATE,COUNT(*)AS"NUMBER OF
VEHICLES"
FROM SERVICE C,BRANCH B,VEHICLE A
WHERE C.B_ID=B.B_ID
AND C.V_ID=A.V_ID
GROUP BY C.B_ID,B.NAME,C.V_ID,A.V_MAKE,C.SER_ID,C.S_DATE
ORDER BY S_DATE;
4.7. Can you list all the sub-parts that go into the Vauxhall Astra’s engine and their individual
prices?
SELECT DISTINCT P_NAME,P_PRICE,V_MODEL,V_MAKE
FROM PARTS, VEHICLE, SUBPARTS, VEHICLE_PARTS
WHERE PARTS.P_ID=VEHICLE_PARTS.P_ID
AND VEHICLE.V_ID=VEHICLE_PARTS.V_ID
AND PARTS.P_ID=SUBPARTS.PART2
AND VEHICLE.V_MAKE='VAUXHALL'
AND VEHICLE.V_MODEL='ASTRA';
ADV DB SCREENSHOTS

Contenu connexe

Tendances

Excel project chapter 08
Excel project   chapter 08Excel project   chapter 08
Excel project chapter 08homeworkecrater
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answersMichael Belete
 
Object oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPObject oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPNoman Mohamed Hanif
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queriesSyed Zaid Irshad
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueriesecomputernotes
 
Fsg Tips And Tricks
Fsg Tips And TricksFsg Tips And Tricks
Fsg Tips And TricksAttiq Ahmed
 
abap list viewer (alv)
abap list viewer (alv)abap list viewer (alv)
abap list viewer (alv)Kranthi Kumar
 
Common SQL Programming Mistakes
Common SQL Programming MistakesCommon SQL Programming Mistakes
Common SQL Programming MistakesPlamen Ratchev
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 
Plsql task
Plsql taskPlsql task
Plsql taskNawaz Sk
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementsecomputernotes
 

Tendances (18)

Les15
Les15Les15
Les15
 
Excel project chapter 08
Excel project   chapter 08Excel project   chapter 08
Excel project chapter 08
 
Alv theory
Alv theoryAlv theory
Alv theory
 
Sql wksht-5
Sql wksht-5Sql wksht-5
Sql wksht-5
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
Les07
Les07Les07
Les07
 
Object oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPObject oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAP
 
1 z0 047
1 z0 0471 z0 047
1 z0 047
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
1 z0 001
1 z0 0011 z0 001
1 z0 001
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
 
Fsg Tips And Tricks
Fsg Tips And TricksFsg Tips And Tricks
Fsg Tips And Tricks
 
abap list viewer (alv)
abap list viewer (alv)abap list viewer (alv)
abap list viewer (alv)
 
Common SQL Programming Mistakes
Common SQL Programming MistakesCommon SQL Programming Mistakes
Common SQL Programming Mistakes
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 
1 z1 051
1 z1 0511 z1 051
1 z1 051
 
Plsql task
Plsql taskPlsql task
Plsql task
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
 

En vedette

Charleston 2015: Collection Lifecycle Management
Charleston 2015: Collection Lifecycle ManagementCharleston 2015: Collection Lifecycle Management
Charleston 2015: Collection Lifecycle ManagementAnnie Bélanger
 
IBL Biotechnology_Brochure
IBL Biotechnology_BrochureIBL Biotechnology_Brochure
IBL Biotechnology_BrochureVincent Merven
 
Elizabeth McCrae (Kepos St) Playground Draft Design
Elizabeth McCrae (Kepos St) Playground Draft DesignElizabeth McCrae (Kepos St) Playground Draft Design
Elizabeth McCrae (Kepos St) Playground Draft Designcommunityconsultation
 
Presentation1
Presentation1Presentation1
Presentation1petya17
 
The survivalist
The survivalistThe survivalist
The survivalistpetya17
 
SAINT-ALLOUESTRE_Aménagement de la place du Souvenir Français
SAINT-ALLOUESTRE_Aménagement de la place du Souvenir FrançaisSAINT-ALLOUESTRE_Aménagement de la place du Souvenir Français
SAINT-ALLOUESTRE_Aménagement de la place du Souvenir FrançaisPascal Le Gac
 
Technology skills powerpoint petya
Technology skills powerpoint   petyaTechnology skills powerpoint   petya
Technology skills powerpoint petyapetya17
 
Evaluating the rough cut3
Evaluating the rough cut3Evaluating the rough cut3
Evaluating the rough cut3petya17
 
презентация
презентацияпрезентация
презентацияDimaskinoo
 
Evaluating the rough cut
Evaluating the rough cutEvaluating the rough cut
Evaluating the rough cutpetya17
 
Questionnaire answers
Questionnaire answersQuestionnaire answers
Questionnaire answersphelpzy
 
Question 3
Question 3Question 3
Question 3petya17
 
Ingénierie financière-1
Ingénierie financière-1Ingénierie financière-1
Ingénierie financière-1Hicham Talbi
 
Presentation - 2
Presentation - 2Presentation - 2
Presentation - 2petya17
 
Photo album2
Photo album2Photo album2
Photo album2petya17
 
Photo album1
Photo album1Photo album1
Photo album1petya17
 
Photo album3
Photo album3Photo album3
Photo album3petya17
 
Photo albumrrr
Photo albumrrrPhoto albumrrr
Photo albumrrrpetya17
 

En vedette (20)

Charleston 2015: Collection Lifecycle Management
Charleston 2015: Collection Lifecycle ManagementCharleston 2015: Collection Lifecycle Management
Charleston 2015: Collection Lifecycle Management
 
Caron Hammond_CV
Caron Hammond_CVCaron Hammond_CV
Caron Hammond_CV
 
IBL Biotechnology_Brochure
IBL Biotechnology_BrochureIBL Biotechnology_Brochure
IBL Biotechnology_Brochure
 
Elizabeth McCrae (Kepos St) Playground Draft Design
Elizabeth McCrae (Kepos St) Playground Draft DesignElizabeth McCrae (Kepos St) Playground Draft Design
Elizabeth McCrae (Kepos St) Playground Draft Design
 
Presentation1
Presentation1Presentation1
Presentation1
 
The survivalist
The survivalistThe survivalist
The survivalist
 
SAINT-ALLOUESTRE_Aménagement de la place du Souvenir Français
SAINT-ALLOUESTRE_Aménagement de la place du Souvenir FrançaisSAINT-ALLOUESTRE_Aménagement de la place du Souvenir Français
SAINT-ALLOUESTRE_Aménagement de la place du Souvenir Français
 
Technology skills powerpoint petya
Technology skills powerpoint   petyaTechnology skills powerpoint   petya
Technology skills powerpoint petya
 
Evaluating the rough cut3
Evaluating the rough cut3Evaluating the rough cut3
Evaluating the rough cut3
 
презентация
презентацияпрезентация
презентация
 
Evaluating the rough cut
Evaluating the rough cutEvaluating the rough cut
Evaluating the rough cut
 
Questionnaire answers
Questionnaire answersQuestionnaire answers
Questionnaire answers
 
Documents
DocumentsDocuments
Documents
 
Question 3
Question 3Question 3
Question 3
 
Ingénierie financière-1
Ingénierie financière-1Ingénierie financière-1
Ingénierie financière-1
 
Presentation - 2
Presentation - 2Presentation - 2
Presentation - 2
 
Photo album2
Photo album2Photo album2
Photo album2
 
Photo album1
Photo album1Photo album1
Photo album1
 
Photo album3
Photo album3Photo album3
Photo album3
 
Photo albumrrr
Photo albumrrrPhoto albumrrr
Photo albumrrr
 

Similaire à ADV DB SCREENSHOTS

SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3Mohd Tousif
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions setMohd Tousif
 
CIS276DB Module 6 Assignment 1. Write a select sta.docx
CIS276DB Module 6 Assignment   1. Write a select sta.docxCIS276DB Module 6 Assignment   1. Write a select sta.docx
CIS276DB Module 6 Assignment 1. Write a select sta.docxclarebernice
 
Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Mohd Tousif
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)Huda Alameen
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set Mohd Tousif
 
Sql task answers
Sql task answersSql task answers
Sql task answersNawaz Sk
 
SQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankSQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankMd Mudassir
 
Databricks Sql cheatseet for professional exam
Databricks Sql cheatseet for professional examDatabricks Sql cheatseet for professional exam
Databricks Sql cheatseet for professional examRupiniSarguru
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Thuan Nguyen
 
Oracle APPS :Receivables Auto Invoice
Oracle APPS :Receivables Auto InvoiceOracle APPS :Receivables Auto Invoice
Oracle APPS :Receivables Auto InvoiceSekhar Byna
 
Pick pack and ship confirm process in oracle apps
Pick pack and ship confirm process in oracle appsPick pack and ship confirm process in oracle apps
Pick pack and ship confirm process in oracle appsshravan kumar chelika
 
Oracle Advanced Pricing (Creating a discount modifier using qualifiers)
Oracle Advanced Pricing (Creating a discount modifier using qualifiers)Oracle Advanced Pricing (Creating a discount modifier using qualifiers)
Oracle Advanced Pricing (Creating a discount modifier using qualifiers)Ahmed Elshayeb
 
Intro to tsql unit 9
Intro to tsql   unit 9Intro to tsql   unit 9
Intro to tsql unit 9Syed Asrarali
 

Similaire à ADV DB SCREENSHOTS (20)

SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Les03
Les03Les03
Les03
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions set
 
Dbms assignment 2
Dbms assignment 2Dbms assignment 2
Dbms assignment 2
 
CIS276DB Module 6 Assignment 1. Write a select sta.docx
CIS276DB Module 6 Assignment   1. Write a select sta.docxCIS276DB Module 6 Assignment   1. Write a select sta.docx
CIS276DB Module 6 Assignment 1. Write a select sta.docx
 
Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set
 
Interface
InterfaceInterface
Interface
 
Sql task answers
Sql task answersSql task answers
Sql task answers
 
SQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankSQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question Bank
 
Databricks Sql cheatseet for professional exam
Databricks Sql cheatseet for professional examDatabricks Sql cheatseet for professional exam
Databricks Sql cheatseet for professional exam
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05
 
Oracle APPS :Receivables Auto Invoice
Oracle APPS :Receivables Auto InvoiceOracle APPS :Receivables Auto Invoice
Oracle APPS :Receivables Auto Invoice
 
Pick pack and ship confirm process in oracle apps
Pick pack and ship confirm process in oracle appsPick pack and ship confirm process in oracle apps
Pick pack and ship confirm process in oracle apps
 
Oracle Advanced Pricing (Creating a discount modifier using qualifiers)
Oracle Advanced Pricing (Creating a discount modifier using qualifiers)Oracle Advanced Pricing (Creating a discount modifier using qualifiers)
Oracle Advanced Pricing (Creating a discount modifier using qualifiers)
 
Module03
Module03Module03
Module03
 
Intro to tsql unit 9
Intro to tsql   unit 9Intro to tsql   unit 9
Intro to tsql unit 9
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 

ADV DB SCREENSHOTS

  • 1.
  • 8. VEHICLE_PARTS 3.1 CREATE OR REPLACE FUNCTION INVOICETOTAL( --- This function will apply to InvoiceTotal I_IN_ID INVOICE.IN_ID%TYPE) --- the customer bill Return Number RETURN NUMBER IS V_INVOICE_TOTAL INVOICE.TOTAL%TYPE; BEGIN SELECT SUM(LABOUR_COST+SET_PRICE+P_PRICE) --- The select statement will sum labour cost, service price and part price INTO V_INVOICE_TOTAL FROM SERVICE,SERVICE_TYPE,PARTS,STOCK,SERVICE_STOCK --- will retrieve details from this table WHERE SERVICE_TYPE.SET_ID=SERVICE.SET_ID --- join tables together using thier primary key AND PARTS.P_ID=STOCK.P_ID AND STOCK.ST_ID=SERVICE_STOCK.ST_ID AND SERVICE.SER_ID=SERVICE_STOCK.SER_ID AND SERVICE.SER_ID = I_IN_ID; RETURN V_INVOICE_TOTAL; --- returns v_invoice_total back to procedure END ; /
  • 9. CREATE OR REPLACE PROCEDURE POP_AMOUNTOTAL --- creat procedure to populate AmountTotal AS CURSOR C_INVOICE --- cursor that holds the select statement IS SELECT SER_ID --- create a select statment FROM SERVICE; -- SELECT IN_ID -- FROM INVOICE; V_IN_ID INVOICE.IN_ID%TYPE; --- declare variables V_INVOICE_TOTAL INVOICE.TOTAL%TYPE; BEGIN OPEN C_INVOICE; LOOP --- will execute sequence of statement FETCH C_INVOICE INTO V_IN_ID; --- fetches the invoice into the variable V_IV_ID EXIT WHEN C_INVOICE%NOTFOUND; --- exit loop if condition is true V_INVOICE_TOTAL := INVOICETOTAL(V_IN_ID); /* calls function, with SER_ID */ DBMS_OUTPUT.PUT_LINE('TOTAL AMOUNT IS :'|| TO_CHAR(V_INVOICE_TOTAL)); -- UPDATE INVOICE --- Update the invoice total -- SET TOTAL=V_INVOICE_TOTAL -- WHERE IN_ID=V_IN_ID; END LOOP; DBMS_OUTPUT.PUT_LINE('TOTAL = ' || TO_CHAR(V_INVOICE_TOTAL)); CLOSE C_INVOICE; END; /
  • 10. 3.2. CREATE OR REPLACE TRIGGER LIMIT_BRANCH_TO_10_SERVICE --- create a before trigger to check service BEFORE UPDATE OR INSERT ON SERVICE FOR EACH ROW DECLARE SERVICECOUNT NUMBER; --- declare variables MAX_SERVICE NUMBER := 10; BEGIN SELECT COUNT(*) --- The select statement will count number of INTO SERVICECOUNT --- vehicle serviced in a particular branch FROM SERVICE --- statement count all from service table WHERE S_DATE = :NEW.S_DATE AND B_ID = :NEW.B_ID; IF SERVICECOUNT <= MAX_SERVICE THEN --- will be raised when above if statment equals true RAISE_APPLICATION_ERROR (-20000,'BRANCH_' ||:NEW.B_ID || ' IS FULL FOR SERVICES. PLEASE TRY ANOTHER BRANCH'); END IF; END; /
  • 11. 3.3. CREATE OR REPLACE TRIGGER CHECK_STOCK --- CREATE A TRIGGER TO CHECK_STOCK BEFORE INSERT OR UPDATE OF QINS,MOH ON STOCK FOR EACH ROW DECLARE CURSOR C_STK IS --- CREATE A CURSOR WITH THE SELECT STATEMENT BELOW SELECT S_NAME,SU_NAME FROM STAFF E, SUPPLIERS S, BRANCH B, BRANCH_SUPPLIERS A --- STATEMENT FETCHES STAFF NAME AND SUPPLIER NAME WHERE E.S_ID=:new.S_ID AND B.B_ID=:new.B_ID AND S.SU_ID = A.SU_ID; V_S_N STAFF.S_NAME%TYPE; V_SU_N SUPPLIERS.SU_NAME%TYPE; BEGIN OPEN C_STK; --- opens cursor and populates FETCH C_STK INTO V_S_N,V_SU_N; --- variables V_S_N and V_SU_N IF:NEW.QINS<= :NEW.MOH THEN --- checks newly inserted/updated stock quantity against minimum stock :NEW.REORDER := 'YES'; --- if stock quantity lower than minimum stock then display the msg below DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');--- message will display staff name and DBMS_OUTPUT.PUT_LINE(V_S_N||' RE-ORDER STOCK FOR '|| :NEW.ST_ID||' FROM '||V_SU_N); --- supplier name for that stock DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); ELSE :NEW.REORDER :='NO'; --- if stock quantity higher than minimum stock then display the msg below DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXX'); DBMS_OUTPUT.PUT_LINE('STOCK LEVEL GOOD FOR '|| :NEW.ST_ID); DBMS_OUTPUT.PUT_LINE('XXXXXXXXXXXXXXXXXXXXXXXXXXXX'); END IF; END; /
  • 12. Below is the stock quantity in stock and minimum in hand. After the trigger is created, stock ST81 is updated to be lower than minimum in hand, and the message display below. The table above shows which stock needs to be reordered.
  • 13. When the stock quantity is above the minimum stock, the message above display for that stock. The minimum quantity in stock table above, displays message when it’s updated INTEGRITY CONSTRAINTS 4.1. Customer gender should be recorded as ‘M’ or ‘F’. ALTER TABLE CUSTOMER ADD CONSTRAINT GENDER CHECK (C_GENDER IN ('M','F')); 4.2. The Supplier’s name must be Unique and not null. ALTER TABLE SUPPLIERS ADD CONSTRAINT SU_NAME UNIQUE (SU_NAME) MODIFY SU_NAME NOT NULL;
  • 14. 4.3. Implement at least one table that demonstrate primary and foreign key constraints using the ALTER TABLE command. ALTER TABLE CITY ADD PRIMARY KEY (CITY_ID) ADD FOREIGN KEY (COUNTRY_ID) REFERENCES COUNTRY (COUNTRY_ID); END USER QUESTIONS 4.4 Can you list all customers’ details? SELECT * FROM CUSTOMER; 4.5 List all the customers, their car details in for a service and the mechanic who made the booking. SELECT A.C_NO,D.V_ID,A.V_MAKE,A.V_MODEL,A.CURRENT_MILEAGE,D.S_ID_BK,D.SER_ID FROM SERVICE D,CUSTOMER C,STAFF B,VEHICLE A WHERE A.C_NO=C.C_NO AND D.V_ID=A.V_ID AND D.S_ID_BK=B.S_ID;
  • 15. 4.6. List by branch the number of cars booked in for service at each branch order by date. SELECT C.B_ID,B.NAME,C.V_ID,A.V_MAKE,C.SER_ID,C.S_DATE,COUNT(*)AS"NUMBER OF VEHICLES" FROM SERVICE C,BRANCH B,VEHICLE A WHERE C.B_ID=B.B_ID AND C.V_ID=A.V_ID GROUP BY C.B_ID,B.NAME,C.V_ID,A.V_MAKE,C.SER_ID,C.S_DATE ORDER BY S_DATE; 4.7. Can you list all the sub-parts that go into the Vauxhall Astra’s engine and their individual prices? SELECT DISTINCT P_NAME,P_PRICE,V_MODEL,V_MAKE FROM PARTS, VEHICLE, SUBPARTS, VEHICLE_PARTS WHERE PARTS.P_ID=VEHICLE_PARTS.P_ID AND VEHICLE.V_ID=VEHICLE_PARTS.V_ID AND PARTS.P_ID=SUBPARTS.PART2 AND VEHICLE.V_MAKE='VAUXHALL' AND VEHICLE.V_MODEL='ASTRA';