0% found this document useful (0 votes)
3 views

Database Management Assignment 2

The document outlines the creation of various database tables including OUTLET, PRODUCT, SUPPLIER, CATEGORY, BATCH, STOCK, CUSTOMER, PURCHASE_HISTORY, PURCHASE_ORDERS, DISCOUNT, and LOYALTY, along with their respective attributes and constraints. It also includes SQL queries for retrieving and manipulating data, such as finding outlet details, product sales, and implementing views, as well as managing user privileges and data redaction policies. Additionally, it highlights the limitations of updating and deleting records in views due to key preservation constraints.

Uploaded by

rcj5831
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Database Management Assignment 2

The document outlines the creation of various database tables including OUTLET, PRODUCT, SUPPLIER, CATEGORY, BATCH, STOCK, CUSTOMER, PURCHASE_HISTORY, PURCHASE_ORDERS, DISCOUNT, and LOYALTY, along with their respective attributes and constraints. It also includes SQL queries for retrieving and manipulating data, such as finding outlet details, product sales, and implementing views, as well as managing user privileges and data redaction policies. Additionally, it highlights the limitations of updating and deleting records in views due to key preservation constraints.

Uploaded by

rcj5831
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

2)OUTLET TABLE

OUTLET
PK_Outlet_ID
Province
District
City
Address
Telephone_No

CREATE TABLE OUTLET (pk_Outlet_Id INT GENERATED BY DEFAULT ON NULL AS IDENTITY, Province
VARCHAR2(15), District VARCHAR2(15), City VARCHAR2(15), Address VARCHAR2(50),
Telephone_No INT,
CONSTRAINT Outlet_Id_Length CHECK (pk_Outlet_Id >= 0000 and pk_Outlet_Id <= 9999 ),
CONSTRAINT Telephone_No_Length CHECK (Telephone_No >= 0000000000 and Telephone_No <=
9999999999 ),
CONSTRAINT pk_outlet PRIMARY KEY (pk_Outlet_Id) );
PRODUCT TABLE
PRODUCT
PK_Product_ID
PK_Outlet_ID
Product_Name
FK_Supplier_ID
FK_Category_ID
Min_Stock_Level

CREATE TABLE PRODUCT (PK_Product_Id INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


pk_Outlet_Id INT, Product_Name VARCHAR2(30), FK_Supplier_ID INT, FK_Category_ID INT,
Min_Stock_Level NUMBER (10,2),
CONSTRAINT pk_product PRIMARY KEY (PK_Product_ID, PK_Outlet_ID),
CONSTRAINT fk_Supplier FOREIGN KEY (FK_Supplier_ID) REFERENCES MANUFACTURER
(PK_Supplier_ID),
CONSTRAINT fk_Category FOREIGN KEY (FK_Category_ID) REFERENCES CATEGORY
(PK_Category_ID));
SUPPLIER TABLE
SUPPLIER
PK_Supplier_ID
Name
Address
Contact

CREATE TABLE SUPPLIER(PK_Supplier_ID INT GENERATED BY DEFAULT ON NULL AS


IDENTITY, Name VARCHAR2(30), Address VARCHAR2(50), Contact INT,
CONSTRAINT pk_supplier PRIMARY KEY (PK_Supplier_ID),
CONSTRAINT Contact_Length CHECK (Contact >= 0000000000 and Contact <= 9999999999
));
CATEGORY
CATEGORY
PK_Category_ID
Name

CREATE TABLE CATEGORY(pk_category_id INT GENERATED BY DEFAULT ON NULL AS IDENTITY, NAME


VARCHAR2(40), CONSTRAINT pk_category PRIMARY KEY (pk_category_id));
BATCH TABLE
BATCH
PK_Batch_Code
PK_Product_ID
Manufactured_Date
Expiration_Date
Retail_Price

CREATE TABLE BATCH (PK_Batch_Code INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


PK_Product_ID INT, Manufactured_Date DATE, Expiration_Date DATE,Retail_Price NUMBER
(12,2),
CONSTRAINT pk_Batch PRIMARY KEY (PK_Batch_Code, PK_Product_ID));
STOCK TABLE
STOCK
PK_SKU_Code
Quantity
Retail_Price

CREATE TABLE STOCK ( PK_Outlet_ID INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


PK_Product_ID INT GENERATED BY DEFAULT ON NULL AS IDENTITY, PK_Batch_Code INT GENERATED
BY DEFAULT ON NULL AS IDENTITY, PK_SKU_Code VARCHAR2(36), Quantity NUMBER(10,2),
Retail_Price NUMBER(12,2),
CONSTRAINT PK_STOCK PRIMARY KEY (PK_SKU_Code));
CUSTOMER
CUSTOMER
PK_Customer_ID
First_Name
Last_Name
DOB
NIC
Gender
Mobile_Number
FK_Loyalty_Card_ID

CREATE TABLE CUSTOMER(PK_Customer_ID INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


First_Name VARCHAR2(20), Last_Name VARCHAR2(20), DOB DATE, NIC VARCHAR(20), Gender
VARCHAR(10), Mobile_Number INT, FK_Loyalty_Card_ID INT,
CONSTRAINT pk_Customer PRIMARY KEY (PK_Customer_ID),
CONSTRAINT fk_Loyality FOREIGN KEY (FK_Loyalty_Card_ID) REFERENCES LOYALTY
(PK_Loyalty_Card_ID),
CONSTRAINT Mobile_No_Length CHECK (Mobile_Number >= 0000000000 and Mobile_Number <=
9999999999 ) );
PURCHASE_HISTORY
PURCHASE_HISTORY
PK_Bill_No
Billing_Date
FK_SKU_Code
FK_Customer_ID
Loyalty_Card_ID
Purchase Qty
Retail Price
Discount Value
Bill Value

CREATE TABLE PURCHASE_HISTORY (PK_Bill_No INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


Billing_Date DATE,FK_SKU_Code VARCHAR2(36),FK_Customer_ID INT, Loyalty_Card_ID INT,
Purchase_Qty NUMBER(10,2), Retail_Price NUMBER10,2), Discount_Value NUMBER(10,2),
Bill_Value NUMBER(12,2),
CONSTRAINT pk_purchase PRIMARY KEY (PK_Bill_No, Billing_Date, FK_SKU_Code),
CONSTRAINT fk_SKU FOREIGN KEY (FK_SKU_Code) REFERENCES STOCK(PK_SKU_Code));
PURCHASE_ORDER
PURCHASE_ORDERS
PK_Order_ID
FK_Supplier_ID
FK_Outlet_ID
FK_Product_ID
Quantity

CREATE TABLE PURCHASE_ORDERS ( PK_Order_ID INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


FK_Supplier_ID INT, FK_Product_ID INT, FK_Outlet_ID INT, Quantity NUMBER(10,2),
CONSTRAINT pk_Order PRIMARY KEY (PK_Order_ID),
CONSTRAINT Fk_Order_Supplier FOREIGN KEY (FK_Supplier_ID) REFERENCES SUPPLIER
(PK_Supplier_ID),
CONSTRAINT Fk_Order_Product FOREIGN KEY (FK_Product_ID, FK_Outlet_ID) REFERENCES PRODUCT
(PK_Product_ID, PK_Outlet_ID));
DISCOUNT_TABLE
DISCOUNT
PK_Discount_ID
FK_Loyalty_Card_ID
FK_Product_ID
FK_Outlet_ID
Start_Date
End_Date
Discount_Pct

CREATE TABLE DISCOUNT ( PK_Discount_ID INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


FK_Loyalty_Card_ID INT, FK_Product_ID INT, FK_Outlet_ID INT, Start_Date DATE, End_Date
DATE, Discount_Pct NUMBER(5,2),
CONSTRAINT pk_Discount PRIMARY KEY (PK_Discount_ID), CONSTRAINT Fk_Dis_Loyalty FOREIGN
KEY (FK_Loyalty_Card_ID) REFERENCES LOYALTY (PK_Loyalty_Card_ID), CONSTRAINT
Fk_Dis_Product FOREIGN KEY (FK_Product_ID, FK_Outlet_ID) REFERENCES PRODUCT
(PK_Product_ID, PK_Outlet_ID));
LOYALTY
LOYALTY
PK_Loyalty_Card_ID
Issued_Date
Expiry_Date
Joining_Fee

CREATE TABLE LOYALTY(PK_Loyalty_Card_ID INT GENERATED BY DEFAULT ON NULL AS IDENTITY,


Issued_Date DATE, Expiry_Date DATE, Joining_Fee NUMBER(10,2),
CONSTRAINT pk_Loyalty PRIMARY KEY (PK_Loyalty_Card_ID) );
4.a) Find details of all outlets in the district of Colombo

SELECT * FROM OUTLET WHERE District = 'Colombo';

4.b) Fetch the product details of only the products those products that
have been deployed across all outlets in the district of Colombo.

SELECT *
FROM PRODUCT
INNER JOIN OUTLET ON PRODUCT.PK_OUTLET_ID = OUTLET.PK_OUTLET_ID
WHERE OUTLET.DISTRICT = 'Colombo' ;
4.c) Find the total value of sales per store per month.

SELECT SUM(Bill_Value) AS Total_Value


FROM PURCHASE_HISTORY
WHERE Billing_Date BETWEEN TO_DATE('01052023', 'DDMMYYYY') AND
TO_DATE('31052023', 'DDMMYYYY');
4.d) Given a customer ID, find the top 3 products that he/she has
purchased throughout his/her purchase history

SELECT FK_SKU_Code, SUM(Purchase_Qty) AS Total_Quantity


FROM PURCHASE_HISTORY
WHERE FK_Customer_ID = '2000002733'
GROUP BY FK_SKU_Code
ORDER BY Total_Quantity DESC
FETCH FIRST 3 ROWS ONLY;
4.e) Write a query that shows the total number of a times a product has
been sold over a year, add a column that compares it to the average number
of times a product has been sold.

SELECT FK_SKU_Code, COUNT(*) AS Total_Sales, AVG(CountPerProduct) AS


Average_Sales
FROM (
SELECT FK_SKU_Code, COUNT(*) AS CountPerProduct
FROM PURCHASE_HISTORY
WHERE Billing_Date BETWEEN TO_DATE('01072022', 'DDMMYYYY') AND
TO_DATE('30062023', 'DDMMYYYY')
GROUP BY FK_SKU_Code
) Subquery
GROUP BY FK_SKU_Code;
4.f) Get the total loyalty discount on a bill if applicable

SELECT PK_Bill_No, SUM(Discount_Value) AS Total_Loyalty_Discount


FROM PURCHASE_HISTORY
WHERE Loyalty_Card_ID IS NOT NULL
GROUP BY PK_Bill_No;
5. A. Develop a view on a join of only two tables, that shows SKUs along
with the relevant outlet details.

CREATE VIEW SKU_OUTLET_VIEW AS


SELECT SUBSTR(STOCK.PK_SKU_Code, 10, 4) AS SKU_Key, PK_SKU_Code,
STOCK.Quantity, STOCK.Retail_Price,
OUTLET.PK_Outlet_ID, OUTLET.Province, OUTLET.District, OUTLET.City,
OUTLET.Address, OUTLET.Telephone_No
FROM STOCK
JOIN OUTLET ON SUBSTR(STOCK.PK_SKU_Code, 10, 4) = OUTLET.PK_Outlet_ID;
B) 1. * Update the columns in the view
-- Update the Quantity column
UPDATE SKU_OUTLET_VIEW
SET Quantity = 10
WHERE SKU_Key = '0001';

-- Update the Retail_Price column


UPDATE SKU_OUTLET_VIEW
SET Retail_Price = 19.99
WHERE SKU_Key = '0001';

-- Update the Address column


UPDATE SKU_OUTLET_VIEW
SET Address = 'No 100, Deans Road, Colombo 10 '
WHERE SKU_Key = '0002';
ERROR : Cannot modify a column which maps to a non key preserved table.
Views are virtual representation of data and may not always support for
direct updates.
In my view definition, the join condition
(STOCK.PK_SKU_Code, 10, 4) = OUTLET.PK_Outlet_ID does not guarantee key
preservation because the SKU_Key derived from (STOCK.PK_SKU_Code, 10, 4)
may not uniquely identify a row in the OUTLET table. When we try to update
the quantity column in the view, Oracle determines that the underlying
join does not preserve the uniqueness of the OUTLET tables primary key.
Therefore, it considers the view as non-key-preserved, and cannot modify a
column which maps to non-key preserved table.
2. * Insert new rows in the view
INSERT INTO SKU_OUTLET_VIEW (SKU_Key, PK_SKU_Code, Quantity, Retail_Price,
PK_Outlet_ID, Province, District, City, Address, Telephone_No)
VALUES ('0002', '100000002', 20.00, 29.99, '0002', 'Western', 'Gampaha',
'Negombo', 'No 30, Main Street, Negombo', '0312265478');

ERROR : Virtual column not allowed here .


In this error specially related to the “SKU_Key” column, which is derived
using SUBSTR function in the view.
Since the “SKU_Key” column is not directly mapped to a column in the
underlying tables, you cannot insert values into it. Virtual columns which
are derived or computed columns, cannot be directly inserted or updated.
3. * Delete of rows from the view

DELETE FROM SKU_OUTLET_VIEW


WHERE SKU_Key = '0002';
ERROR : cannot delete from view without exactly one key-preserved table.
“Cannot delete record from view without exactly one key-preserved table”
indicate that the view SKU_OUTLET_VIEW cannot be directly used for delete
operations because it is not based on a single key preserved table.
In order to delete rows from the view, you need to identify the primary
key columns from the tables and that the view is based on and use the
primary key columns in the delete statement.
C)
CREATE USER CBO12267_S IDENTIFIED BY cab3213;
*GRANT BASIC ACCESS TO USER CBO12267_S.
GRANT CONNECT, RESOURCE TO CBO12267_S;

1)Enable read only access to the view for the secondary account and show
evidence of your grant by querying the Oracle data dictionary for
privileges granted by you.
*GRANT SELECT PREVILEGE TO VIEW SKU_OUTLET_VIEW.
GRANT SELECT ON SKU_OUTLET_VIEW TO CBO12267_S;
2) Change the Privilege for the secondary account to permit updates to
the view. Test the privilege using your ‘2’ account and while logged
in as ‘_2’ use the data dictionary to show the privilege that the
‘_2’ account now has.

GRANT UPDATE ON SKU_OUTLET_VIEW TO CBO12267_S;

3) Prohibit the secondary account from accessing the view

REVOKE SELECT ON SKU_OUTLET_VIEW FROM CBO12267_S;


6) SYSTEM USER DOES NOT HAVE SUFFICIENT AUTHORIZATION TO ACTIVATE
DBMS_REDIRECT TO USER CBO12267.
GRANT EXECUTE ON DBMS_REDACT TO CBO12267;

1. Enable the Oracle Data Redaction Feature.


EXECUTE DBMS_REDACT.ENABLE;
2. Create Redaction Policy using ‘DBMS_REDACT.ADD_POLICY’ Procedure.
BEGIN
DBMS_REDACT.ADD_POLICY(
object_schema => 'CBO12267',
object_name => 'CUSTOMER',
column_name => 'NIC',
policy_name => 'redact_nic',
function_type => DBMS_REDACT.PARTIAL,
function_parameters => DBMS_REDACT.REDACT_US_SSN,
expression => '1:4',
policy_description => 'Redact first four digits of NIC');
END;

3. Once the Redaction Policy is created, the first four digits of the
NIC column in the CUSTOMER table will be redacted for users who do
not have the necessary privileges to view the original data .

You might also like