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

DBMS_Program 1-2

The document outlines the creation of several database tables including BRANCH, STUDENT, BOOK, AUTHOR, and BORROW, along with their relationships through primary and foreign keys. It also includes a series of SQL queries to retrieve specific information from these tables, such as student details, book borrowing statistics, and author information. Additionally, a second program focuses on a STUDENT table with queries for updating total scores, calculating GPA, and filtering students based on various criteria.

Uploaded by

snehalesha30
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)
5 views

DBMS_Program 1-2

The document outlines the creation of several database tables including BRANCH, STUDENT, BOOK, AUTHOR, and BORROW, along with their relationships through primary and foreign keys. It also includes a series of SQL queries to retrieve specific information from these tables, such as student details, book borrowing statistics, and author information. Additionally, a second program focuses on a STUDENT table with queries for updating total scores, calculating GPA, and filtering students based on various criteria.

Uploaded by

snehalesha30
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/ 15

DBMS _ PROGRAM 1

Create the following tables with properly specifying Primary keys, Foreign keys
and solve the following queries.

BRANCH(Branchid,Branchname,HOD)
STUDENT(USN,Name,Address,Branchid,sem)
BOOK(Bookid,Bookname,Authorid,Publisher,Branchid)
AUTHOR(Authorid,Authorname,Country,age)
BORROW(USN,Bookid,Borrowed_Date)

QUERIES:

1) List the details of Students who are all Studying in 2nd sem MCA.
2) List the students who are not borrowed any books.
3) Display the USN,Student
name,Branch_name,Book_name,Books_Borrowed_Date of 2nd sem,Author
name MCA Students who borrowed books.
4) Display the number of books written by each Author.
5) Display the student details who borrowed more than two books.
6) Display the student details who borrowed books of more than one Author.
7) Display the Book names in descending order of their names.
8) List the details of students who borrowed the books which are all published
by the same Publisher.
✓ SCHEMA:

BRANCH BRID BRNAME HOD

USN STNAME STADDRES BRID SEM


STUDENT

AUTHOR AID ANAME COUNTRY AGE

BOOKID BOOKNAME AUTHORID PUBLISHER BRANCHID


BOOK

STUSN BOOKID BORROWDATE


BORROW
✓ ER DIAGRAM:

BRANCH
CREATE TABLE BRANCH(BRID VARCHAR(20) PRIMARY KEY, BRNAME VARCHAR(20), HOD
VARCHAR(20));
INSERT INTO BRANCH VALUES('MCA123','MCA','MANJULA');
INSERT INTO BRANCH VALUES('MBA123','MBA','ABHISHEK');
INSERT INTO BRANCH VALUES('CIVIL123','CIVIL','ARUN');
SELECT *FROM BRANCH;
STUDENT
CREATE TABLE STUDENT(USN VARCHAR(20) PRIMARY KEY, STNAME
VARCHAR(20),STADDRESS VARCHAR(20),BRID VARCHAR(20),SEM NUMBER(3),FOREIGN
KEY(BRID) REFERENCES BRANCH(BRID));
INSERT INTO STUDENT VALUES('21MCA09','HESH','RANJAN','MCA123',2);
INSERT INTO STUDENT VALUES('21MCA08','SRI','HEBBAL','MCA123',2);
INSERT INTO STUDENT VALUES('21MCA10','JAGESH','SIRMVIT','MCA123',2);
INSERT INTO STUDENT VALUES('21MCA11','AKANSH','YELAHANKA','MCA123',2);
INSERT INTO STUDENT VALUES('21MBA01','HARISH','HEBBAL','MBA123',4);
INSERT INTO STUDENT VALUES('21CIVIL','RAJ','KOTE','CIVIL123',4);
SELECT *FROM STUDENT;
AUTHOR
CREATE TABLE AUTHOR (AID VARCHAR(20) PRIMARY KEY, ANAME
VARCHAR(20),COUNTRY VARCHAR(20), AGE NUMBER(3));
INSERT INTO AUTHOR VALUES('A101','SATHAM','INDIA',54);
INSERT INTO AUTHOR VALUES('A102','RAKESH GUPTA','US',34);
INSERT INTO AUTHOR VALUES('A103','JIRAN','UK',40);
SELECT *FROM AUTHOR;

BOOK
CREATE TABLE BOOK(BOOKID VARCHAR(20) PRIMARY KEY, BOOKNAME
VARCHAR(20),AUTHORID VARCHAR(20),PUBLISHER VARCHAR(20),BRANCHID
VARCHAR(20),FOREIGN KEY(BRANCHID) REFERENCES BRANCH(BRID),FOREIGN
KEY(AUTHORID) REFERENCES AUTHOR(AID));
INSERT INTO BOOK VALUES('B103','JAVA','A101','P01','MCA123');
INSERT INTO BOOK VALUES('B104','PYTHON','A102','P02','MCA123');
INSERT INTO BOOK VALUES('B105','DBMS','A103','P03','MBA123');
INSERT INTO BOOK VALUES('B106','ADA','A102','P04','CIVIL123');
SELECT *FROM BOOK;
BORROW
CREATE TABLE BORROW (STUSN VARCHAR(20),BOOKID VARCHAR(20),BORROWDATE
VARCHAR(15),FOREIGN KEY(BOOKID) REFERENCES BOOK(BOOKID),FOREIGN KEY(STUSN)
REFERENCES STUDENT(USN));
INSERT INTO BORROW VALUES('21MCA09','B103','10-10-1999');
INSERT INTO BORROW VALUES('21MCA09','B104','22-09-1998');
INSERT INTO BORROW VALUES ('21MCA09','B103','07-02-2012');
INSERT INTO BORROW VALUES ('21MCA08','B105','22-05-2016');
INSERT INTO BORROW VALUES ('21MCA11','B105','02-02-2012');
SELECT *FROM BORROW;

QUERY-1) List the details of Students who are all Studying in 2nd sem MCA.
SQL>
SELECT *FROM STUDENT WHERE SEM=2 AND BRID IN(SELECT BRID FROM BRANCH
WHERE BRNAME='MCA');
QUERY-2) List the students who are not borrowed any books.
SQL>
SELECT *FROM STUDENT WHERE USN NOT IN(SELECT STUSN FROM BORROW);

QUERY-3) Display the USN, Student name, Branch_name, Book_name, Author_name ,


Books_Borrowed_Date of 2nd sem MCA Students who borrowed books.
SQL>
SELECT DISTINCT S.USN,S.STNAME, BK.BOOKNAME, A.ANAME,BW.BORROWDATE,
BR.BRNAME FROM STUDENT S,BOOK BK, AUTHOR A, BORROW BW, BRANCH BR WHERE
S.USN=BW.STUSN AND S.BRID=BR.BRID AND BW.BOOKID=BK.BOOKID AND
BK.AUTHORID=A.AID AND S.SEM=2 AND BRNAME='MCA';
QUERY-4) Display the number of books written by each Author.
SQL>
SELECT AUTHORID, COUNT(*) AS NO_OF_BOOKS FROM BOOK GROUP BY AUTHORID;

QUERY-5) Display the student details who borrowed more than two books.
SQL>
SELECT *FROM STUDENT WHERE USN IN(SELECT STUSN FROM BORROW GROUP BY
STUSN HAVING COUNT(BORROW.STUSN)>2);

QUERY-6) Display the student details who borrowed books of more than one Author.
SQL>
SELECT *FROM STUDENT WHERE USN IN (SELECT STUSN FROM BORROW BW JOIN BOOK
B ON BW.BOOKID=B.BOOKID GROUP BY STUSN HAVING COUNT(B.AUTHORID)>1);

QUERY-7) Display the Book names in descending order of their names.


SQL>
SELECT BOOKNAME FROM BOOK ORDER BY BOOKNAME DESC;

QUERY-8) List the details of students who borrowed the books which are all published by
the same Publisher.
SQL>
SELECT *FROM STUDENT WHERE USN IN (SELECT BW.STUSN FROM BORROW BW JOIN
BOOK B ON BW.BOOKID=B.BOOKID GROUP BY BW.STUSN HAVING
COUNT(B.PUBLISHER)=1);
DBMS _ PROGRAM 2
consider the following schema:
STUDENT(USN,dob,branch,m1,m2,m3,total,GPA) execute the following queries.
QUERIES:

1) Update the column total by adding m1, m2, m3.


2) Find GPA score of all students.
3) Find the students who born on a particular year of birth from DOB column.
4) List the students who are studying in particular branch.
5) Find the maximum GPA score of the student branch wise.
6) Find the students whose name starts with alphabet “S”.
7) Find the students whose name ends with alphabet “AR”.
8) Delete the student details whose USN is given as 1001.

✓ SCHEMA:
USN Name Dob Branch M1 M2 M3 Total GPA
STUDENT
✓ ER DIAGRAM:

STUDENT3

CREATE TABLE STUDENT3(USN VARCHAR2(10) NOT NULL,NAME VARCHAR(20) NOT


NULL,DOB DATE NOT NULL,BRACH VARCHAR(20) NOT NULL,M1 NUMBER DEFAULT 0,M2
NUMBER DEFAULT 0,M3 NUMBER DEFAULT 0,TOTAL NUMBER,GPA NUMBER);

INSERT INTO STUDENT3 VALUES('1001','JAM','01-01-2001','MCA',50,55,75,0 ,0);


INSERT INTO STUDENT3 VALUES('1002','ARMIN','01-02-2000','MBA',55,43,33,0 ,0);
INSERT INTO STUDENT3 VALUES('1003','JAMIS','01-07-2002','MCA',56,35,67,0 ,0);
INSERT INTO STUDENT3 VALUES('1004','JASIMAR','04-08-2001','MBA',48,95,84,0 ,0);
INSERT INTO STUDENT3 VALUES('1005','AJAM','09-15-2002','MCA',73,85,90,0 ,0);
INSERT INTO STUDENT3 VALUES('1006','EJAM','05-23-2002','MBA',60,79,79,0 ,0);
INSERT INTO STUDENT3 VALUES('1007','UJAMI','01-13-1998','MCA',80,75,55,0 ,0);
INSERT INTO STUDENT3 VALUES('1008','OEJAMY','04-27-1995','MBA',80,35,75,0 ,0);
INSERT INTO STUDENT3 VALUES('1009','SEJAMOE','11-09-2000','MCA',69,55,65,0 ,0);
SELECT * FROM STUDENT3;
Query_1) update the column total by adding column m1,m2,m3.
SQL>
UPDATE STUDENT3 SET TOTAL=M1+M2+M3;
SELECT * FROM STUDENT3;

--------------------------------------------------------------------------------
Query_2) find GPA score of all students.s
SQL>
UPDATE STUDENT3 SET GPA=ROUND((TOTAL/3)/9.5,2);
SELECT *FROM STUDENT3;
--------------------------------------------------------------------------------
Query_3) Find the students who born on a particular year of birth from DOB column
SQL>
SELECT * FROM STUDENT3 WHERE DOB LIKE '%2001';
---------------------------------------------------------------------------
Query_4) List the students who are studying in particular branch.
SQL>
SELECT USN,NAME,BRACH FROM STUDENT3 WHERE BRACH='MCA';

--------------------------------------------------------------------------------
Query_5) Find the maximum GPA score of the student branch wise.
SQL>
SELECT MAX(GPA),BRACH FROM STUDENT3 GROUP BY BRACH;

--------------------------------------------------------------------------------
Query_6) Find the students whose name starts with alphabet “S”.
SQL>
SELECT USN,NAME,BRACH FROM STUDENT3 WHERE NAME LIKE 'S%';

--------------------------------------------------------------------------------
Query_7) Find the students whose name ends with alphabet “AR”.

SQL>
SELECT * FROM STUDENT3 WHERE NAME LIKE '%AR';
--------------------------------------------------------------------------------
Query_8) Delete the student details whose USN is given as 1001.

SQL>
DELETE FROM STUDENT3 WHERE USN='1001';
SELECT * FROM STUDENT3;

You might also like