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

A) Create The Tables With The Appropriate Integrity Constraints

The document describes creating tables - Customer, Item, Sale - and inserting sample data to model a customer-sale database. It then provides examples of SQL queries on this data: listing bills for today with customer names; total bill details with price, quantity and amount; customers who bought items over $200; count of items bought by each customer; and items bought by a specific customer or sold today.

Uploaded by

vishal v nair
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)
74 views

A) Create The Tables With The Appropriate Integrity Constraints

The document describes creating tables - Customer, Item, Sale - and inserting sample data to model a customer-sale database. It then provides examples of SQL queries on this data: listing bills for today with customer names; total bill details with price, quantity and amount; customers who bought items over $200; count of items bought by each customer; and items bought by a specific customer or sold today.

Uploaded by

vishal v nair
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/ 5

CUSTOMER-SALE DATABASE

Aim :To create tables and perform queries in a customer-sale scenario


Database Schema for a customer-sale scenario

Customer(cust id : integer, cust_name: string)


Item(item_id: integer, item_name: string, price: integer)
Sale(bill_no: integer, bill_data: date, cust_id: integer, item_id: integer,
qty_sold: integer)

Answer:

a) Create the tables with the appropriate integrity


constraints.

• CREATE TABLE CUSTOMER ( CUST_ID INTEGER(5) PRIMARY KEY, CUST_NAME


VARCHAR(15) );

• CREATE TABLE ITEM ( ITEM_ID INTEGER(4) PRIMARY KEY, ITEM_NAME


VARCHAR(15), PRICE DECIMAL(6,2) );

• CREATE TABLE SALE ( BILL_NO INTEGER(5) PRIMARY KEY, BILL_DATE DATE,


CUST_ID INTEGER(5),ITEM_ID INTEGER(4), QTY_SOLD INTEGER(4),
FOREIGN KEY(CUST_ID) REFERENCES CUSTOMER(CUST_ID), FOREIGN
KEY(ITEM_ID) REFERENCES ITEM(ITEM_ID) );

• desc sale;
+-----------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------+------+-----+---------+-------+
| BILL_NO | int | NO | PRI | NULL | |
| BILL_DATE | date | YES | | NULL | |
| CUST_ID | int | YES | MUL | NULL | |
| ITEM_ID | int | YES | MUL | NULL | |
| QTY_SOLD | int | YES | | NULL | |
+-----------+------+------+-----+---------+-------+

• desc customer;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| CUST_ID | int | NO | PRI | NULL | |
| CUST_NAME | varchar(15) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
• desc item;

+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ITEM_ID | int | NO | PRI | NULL | |
| ITEM_NAME | varchar(15) | YES | | NULL | |
| PRICE | decimal(6,2) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+

b)Insert around 5 records in each of the tables

• insert into customer values (1,'arun');

• insert into customer values (2,'Varun');

• insert into customer values (3,'JHON');

• insert into customer values (4,'JOY');

• insert into customer values (5,'nithya');

• insert into item values(20,'keyboard',1500.00);

• insert into item values(21,'mouse',1500.00);

• insert into item values(22,'graphic_pro',3500.00);

• insert into item values(23,'ROM',2500.00);

• insert into item values(24,'USB',225.00);

• insert into sale values(10,'2022-10-15',1,20,2);

• insert into sale values(11,'2022-10-14',2,21,1);

• insert into sale values(12,'2022-09-14',3,22,3);

• insert into sale values(13,'2022-09-30',4,23,4);

• insert into sale values(14,'2022-10-07',5,24,2);

• select * from customer;


+---------+-----------+
| CUST_ID | CUST_NAME |
+---------+-----------+
| 1 | arun |
| 2 | Varun |
| 3 | JHON |
| 4 | JOY |
| 5 | nithya |
+---------+-----------+

• select * from sale;

+---------+------------+---------+---------+----------+
| BILL_NO | BILL_DATE | CUST_ID | ITEM_ID | QTY_SOLD |
+---------+------------+---------+---------+----------+
| 10 | 2022-10-15 | 1 | 20 | 2 |
| 11 | 2022-10-14 | 2 | 21 | 1 |
| 12 | 2022-09-14 | 3 | 22 | 3 |
| 13 | 2022-09-30 | 4 | 23 | 4 |
| 14 | 2022-10-07 | 5 | 24 | 2 |
+---------+------------+---------+---------+----------+

• select * from item;

+---------+-------------+---------+
| ITEM_ID | ITEM_NAME | PRICE |
+---------+-------------+---------+
| 20 | keyboard | 1500.00 |
| 21 | mouse | 1500.00 |
| 22 | graphic_pro | 3500.00 |
| 23 | ROM | 2500.00 |
| 24 | USB | 225.00 |
+---------+-------------+---------+

c)List all the bills for the current date with the customer
names and item numbers

• SELECT C.CUST_NAME, I.ITEM_ID, S.BILL_NO FROM CUSTOMER C, ITEM I, SALE


S WHERE C.CUST_ID=S.CUST_ID AND S.ITEM_ID=I.ITEM_ID AND
S.BILL_DATE=CURDATE();

+-----------+---------+---------+
| CUST_NAME | ITEM_ID | BILL_NO |
+-----------+---------+---------+
| arun | 20 | 10 |
+-----------+---------+---------+
d)List the total Bill details with the quantity sold, price of
the item and the final amount

• SELECT I.PRICE, S.QTY_SOLD,(I.PRICE*S.QTY_SOLD) TOTAL FROM ITEM I,


SALE S WHERE I.ITEM_ID=S.ITEM_ID;

+---------+----------+----------+
| PRICE | QTY_SOLD | TOTAL |
+---------+----------+----------+
| 1500.00 | 2 | 3000.00 |
| 1500.00 | 1 | 1500.00 |
| 3500.00 | 3 | 10500.00 |
| 2500.00 | 4 | 10000.00 |
| 225.00 | 2 | 450.00 |
+---------+----------+----------+

e) List the details of the customer who have bought a product


which has a price >200

• SELECT C.CUST_ID, C.CUST_NAME


FROM CUSTOMER C, SALE S, ITEM I
WHERI.PRICE>200 AND
C.CUST_ID=S.CUST_ID AND
I.ITEM_ID=S.ITEM_ID;

+---------+-----------+
| CUST_ID | CUST_NAME |
+---------+-----------+
| 1 | arun |
| 2 | Varun |
| 3 | JHON |
| 4 | JOY |
| 5 | nithya |
+---------+-----------+

f) Give a count of how many products have been bought by each


customer

• SELECT CUST_ID, COUNT(ITEM_ID) FROM SALE GROUP BY CUST_ID;

+---------+----------------+
| CUST_ID | COUNT(ITEM_ID) |
+---------+----------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
+---------+----------------+

g) Give a list of products bought by a customer having cust_id


as 5

• SELECT I.ITEM_NAME FROM ITEM I, SALE S WHERE S.CUST_ID=5 AND


I.ITEM_ID=S.ITEM_ID;

+-----------+
| ITEM_NAME |
+-----------+
| USB |
+-----------+

h) List the item details which are sold as of today

• SELECT I.ITEM_ID, I.ITEM_NAME FROM ITEM I, SALE S WHERE


I.ITEM_ID=S.ITEM_ID AND S.BILL_DATE=CURDATE();

+---------+-----------+
| ITEM_ID | ITEM_NAME |
+---------+-----------+
| 20 | keyboard |
+---------+-----------+

You might also like