Solution of Assignmnet 1 SQL
Solution of Assignmnet 1 SQL
ER
Diagram – Student Database
Step 2: ER Model in to a Relational Model
Relational
Model – Student Database
Step 3: Implement the Relations (or Tables)
Create
Table Branch – Student Database
ALTER TABLE BRANCH ADD PRIMARY KEY (BCODE);
ALTER TABLE BRANCH ADD FOREIGN KEY (DNO) REFERENCES DEPARTMENT
(DNO);
CREATE TABLE BRANCH_COURSE (BCODE NUMBER(3),CCODE
NUMBER(4),SEMESTER NUMBER(2));
ALTER TABLE BRANCH_COURSE ADD PRIMARY KEY (BCODE, CCODE);
ALTER TABLE BRANCH_COURSE ADD FOREIGN KEY (BCODE) REFERENCES BRANCH
(BCODE);
ALTER TABLE BRANCH_COURSE ADD FOREIGN KEY (CCODE) REFERENCES COURSE
(CCODE);
CREATE TABLE STUDENT (ROLLNO NUMBER (5),
NAME VARCHAR2 (20),
DOB DATE, GENDER CHAR(2),
DOA DATE, BCODE NUMBER(3));
Create
Student Table – Student Database
ALTER TABLE STUDENT ADD PRIMARY KEY (ROLLNO);
ALTER TABLE STUDENT ADD FOREIGN KEY (BCODE) REFERENCES BRANCH
(BCODE);
ALTER TABLE ADD CONSTRAINT CHK CHECK (GENDER IN ('M','F'));
ALTER TABLE ADD CONSTRAINT CHK2 CHECK (DOA < TO_DATE('31-4-
2016,'DD-MM-YYYY');
CREATE TABLE COURSE (CCODE NUMBER (4), CNAME VARCHAR2 (25), CREDITS
NUMBER (2), DNO NUMBER (2));
Create
Table Course – Student Database
ALTER TABLE COURSE ADD PRIMARY KEY (CCODE);
ALTER TABLE COURSE ADD FOREIGN KEY (DNO) REFERENCES DEPARTMENT
(DNO));
CREATE TABLE ENROLLS (ROLLNO NUMBER (5), CCODE NUMBER (4), SESS
VARCHAR2 (15), GRADE CHAR (2));
Create
Table Enrolls – Student Database
ALTER TABLE ENROLLS ADD PRIMARY KEY (ROLLNO, CCODE, SESS);
ALTER TABLE ENROLLS ADD FOREIGN KEY ROLLNO) REFERENCES STUDENT
(ROLLNO);
ALTER TABLE ENROLLS ADD FOREIGN KEY (CCODE) REFERENCES COURSE
(CCODE);
Step 4: Insert data into the Tables
Use example SQL commands for inserting data into Department table.
Now, insert values to Course table using the following command. Make sure that
the Course ID from Branch_Course match with Course ID of Course Table.
Insert
Data Into Enrolls Table – Student Database
Answers to Questions
Solution (A)
SELECT * FROM DEPARTMENT D WHERE D.DNO IN (SELECT B.DNO FROM BRANCH
B GROUP BY B.DNO HAVING COUNT (B.DNO) > 3);
Query (A)
Solution (B)
SELECT * FROM DEPARTMENT D WHERE D.DNO IN (SELECT C.DNO FROM COURSE
C GROUP BY C.DNO HAVING COUNT (C.CCODE) > 6);
Query (B)
Solution (C)
SELECT * FROM COURSE C WHERE C.CCODE IN (SELECT B.CCODE FROM
BRANCH_COURSE B GROUP BY B.CCODE HAVING COUNT (B.BCODE) > 3);
Query (C)
Solution (D)
SELECT * FROM STUDENT S WHERE S.ROLLNO IN (SELECT E.ROLLNO FROM
ENROLLS E WHERE E.GRADE = 'S' GROUP BY E.ROLLNO HAVING COUNT
(E.GRADE) > 2);
Query (D)
Solution (E)
CREATE VIEW STUDATA AS SELECT E.ROLLNO, S.NAME, COUNT (E.CCODE) AS
CC FROM STUDENT S, ENROLLS E WHERE E.ROLLNO = S.ROLLNO AND
E.GRADE ! = 'U' GROUP BY E.ROLLNO, S.NAME;
Query-E
ER
Diagram – Customer Order Processing Database
Step 2: ER Model into a Relational Model
Relational
Model – Customer Order Processing Database
Queries
Solution (A)
Create all relations and enforced primary key, foreign key and check constraints
according to relational model.
Create
Table Customer Order – Customer Order Processing Database
CREATE TABLE ITEM (ITEMNO VARCHAR2 (5), ITEM_NAME VARCHAR2 (30),
UNIT_PRICE NUMBER (5));
ALTER TABLE ITEM ADD PRIMARY KEY (ITEMNO);
ALTER TABLE ITEM ADD CONSTRAINT CK4 CHECK (ITEMNO LIKE ‘I %’);
Create
Table Item – Customer Order Processing Database
CREATE TABLE ORDER_ITEM (ORDERNO VARCHAR2 (5), ITEMNO VARCHAR2 (5),
QTY NUMBER (3));
ALTER TABLE ORDER_ITEM ADD PRIMARY KEY (ORDERNO, ITEMNO);
Create
Table Order Item – Customer Order Processing Database
SOLUTION (B)
Now, we need to insert data to the relations created after following above steps.
Insert data into the Customer table using insert command as follows.
INSERT INTO CUSTOMER VALUES (‘C0001’,’ANIL KUMAR’);
Insert
Data Into Customer Table
Insert Data to Customer Table – Customer Order-Processing Database
Inserting data into the customer order table is bit different, assume that
customer placed an order on particular date then we cannot compute the total
amount unless we know the total quantity of purchase make by the customer
and for each purchase calculate the total amount as follows.
Insert
Data Into Customer Order
Now we insert data into the order_item table so that we know the total quantity
and unit price of the item customer has purchased.
Instance
of Order Item Table
Now we update the customer order table with order amount as follows
Update
Command Customer Order Table
The result should look like the following.
Updated
Cust_Order Table
SOLUTION (C)
SELECT * FROM CUSTOMER C WHERE C.CUSTOMERNO IN (SELECT O.CUSTOMERNO
FROM CUST_ORDER O GROUP BY O.CUSTOMERNO HAVING COUNT (O.ORDERNO) >
2);
Solution
(C)
SOLUTION (D)
SELECT * FROM ITEM I WHERE I.UNIT_PRICE < (SELECT AVG
(UNIT_PRICE) FROM ITEM);
Solution
(D)
SOLUTION (E)
SELECT ORDERNO, SUM (OTY) FROM ORDER_ITEM GROUP BY ORDERNO;
Solution
(E): Total Quantity of Items
SOLUTION (F)
SELECT * FROM ITEM WHERE ITEMNO IN (SELECT ITEMNO FROM ORDER_ITEM
GROUP BY ITEMNO HAVING COUNT (ITEMNO) >= (SELECT (COUNT (*)/4) FROM
CUST_ORDER));
Solution
(F): Item Count that is present in 25% of Orders
SOLUTION (G)
UPDATE CUST_ORDER SET ORD_AMT = (SELECT SUM (O.QTY * I.UNIT_PRICE)
FROM ORDER_ITEM O, ITEM I WHERE CUST_ORDER.ORDERNO = O.ORDERNO AND
O.ITEMNO = I.ITEMNO);
Solution
(G): Update Customer Order Table
Create a view the customer order table after update and its
called Customer_Details.
View For
Customer Order Table
QUERIES
Solution(A)
ALTER TABLE DEPTS ADD CONSTRAINT CPK CHECK (DEPTNO LIKE 'd%');
Solution (D)
Staff with less than AVG basic pay of all Staff – Project Database
Solution (E)
Staff
Count – Project Database
Solution (H)
Staff
Count Greater than 5 – Project Database
Solution (I)
ER
Diagram – Bank Database
Step : Relational Model
Relational
Model – Bank Database
Step : Implement Relations
ALTER TABLE BRANCH ADD CONSTRAINT CPK CHECK ( BCODE LIKE 'B%');
3. Create the ACCOUNT table and add constraints – primary key, foreign key and
check constraints for account type.
W – WITHDRAWAL
D – DEPOSIT
CREATE TABLE TRANSACTION ( TID VARCHAR2(5), ANO VARCHAR2(5), TTYPE
CHAR(2), TDATE DATE, TAMOUNT NUMBER(7));
ALTER TABLE TRANSACTION ADD PRIMARY KEY(TID); ALTER TABLE
TRANSACTION ADD FOREIGN KEY(ANO) REFERENCES ACCOUNT(ANO); ALTER
TABLE TRANSACTION ADD CONSTRAINT CPK3 CHECK (TTYPE IN ('W','D');
ALTER TABLE TRANSACTION ADD CONSTRAINT CPK4 CHECK (TID LIKE 'T%');
Other Solutions
Solution (B)
You can insert data into the tables now.
1. Customer Relation look like below after inserting data.
Customer
Relation – Bank Database
2. Insert data into the branch table as follows.
Branch
Table – Bank Database
3. Insert the data into account table.
Account
Table – Bank Database
4. Insert data into the last table – Transaction.
Transactio
n Table – Bank Database
Solution (C)
Solution
(C) – Bank Database
Solution (D)
Solution
(D) – Bank Database
Solution (E)
Solution
(E) – Bank Database
Solution (F)
Solution
(F) – Bank Database