0% found this document useful (0 votes)
10 views7 pages

Practical Component

The document outlines SQL commands for creating and managing a database with two tables: Product and Shopping. It includes constraints for product pricing, data insertion, and various queries to display customer purchases, unique customers, total bills, and product details. Additionally, it demonstrates how to update product prices and filter customers based on payment methods.

Uploaded by

Nivetha J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Practical Component

The document outlines SQL commands for creating and managing a database with two tables: Product and Shopping. It includes constraints for product pricing, data insertion, and various queries to display customer purchases, unique customers, total bills, and product details. Additionally, it demonstrates how to update product prices and filter customers based on payment methods.

Uploaded by

Nivetha J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.​ Create table Product and Shopping with appropriate data types and keys.

2.​ Create constraint on product table, such that price is greater than 0.

CREATE TABLE Product (


Pcode VARCHAR2(10) PRIMARY KEY,
Pname VARCHAR2(50),
Price INT CHECK (Price > 0) -- Ensures price is greater than 0
);

CREATE TABLE Shopping (


CustName VARCHAR2(50),
Units INT,
P_Mode VARCHAR2(10) CHECK (P_Mode IN (‘Card’, ‘Cash’)), -- Ensures
only ‘Card’ or ‘Cash’
Pcode VARCHAR2(10),
FOREIGN KEY (Pcode) REFERENCES Product(Pcode) – Foreign key
constraint
);

3.​ Insert values into the table Product and Shopping.

INSERT INTO Product VALUES

('P01', 'Soap', 25),('P02', 'Paste', 43),('P03', 'Oil', 130),('P04', 'Rice',


54),('P05', 'Sugar', 35);

INSERT INTO Shopping VALUES('RAMESH', 7, 'Card', 'P05'),('VIDYA', 1,


'Cash', 'P02'),('LAVANYA', 4, 'Cash', 'P03'),('VIDYA', 3, 'Card', 'P04'),('ANAND',
8, 'Cash', 'P05'),('PRIYA', 6, 'Cash', 'P02'),('ARUN', 4, 'Card', 'P01'),('ANAND',
1, 'Cash', 'P01'),('AVANTHI', 10, 'Card', 'P04');

4.​ Display the Custname, Product_name ,Price and Units Purchased for all the
customers Using joins.

SELECT S.CustName, P.Pname, P.Price, S.Units


FROM Shopping S
JOIN Product P ON S.Pcode = P.Pcode;
5.​ Display the number of unique customers in the shopping table

SELECT COUNT(DISTINCT CustName) AS Unique_Customers FROM


Shopping;

6.​ Display the bill for each customer (Total cost = Price × Units)

SELECT S.CustName, SUM(P.Price * S.Units) AS Total_Bill


FROM Shopping S
JOIN Product P ON S.Pcode = P.Pcode
GROUP BY S.CustName;
7.​ Display the Product code and the total number of units purchased

SELECT Pcode, SUM(Units) AS Total_Units_Purchased


FROM Shopping
GROUP BY Pcode;

8.​ Update the price of ‘Paste’ to 60 and display the product table

UPDATE Product

SET Price = 60
WHERE Pname = ‘Paste’;
.
SELECT * FROM Product; -- To display updated table
9.​ Display the products from highest to lowest price

SELECT * FROM Product ORDER BY Price DESC;


10.​Display the customers who paid using ‘Cash’

SELECT CustName FROM Shopping WHERE P_Mode = ‘Cash’;

You might also like