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

Database Management System

Uploaded by

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

Database Management System

Uploaded by

Samir Thapa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

DATABASE MANAGEMENT SYSTEM

(A LAB ASSIGNMENT ON DATABSE)

BY

Sleshma Thapa

Symbol no. 36283/22

T.U.Reg No.7-2-361-56-2022

Submitted to

Hem Sagar Pokhrel

Department of Computer Science

in the partial fulfillment of the requirements for the degree of the

Bachelor of Business Administration

Universal College

Maitidevi, Kathmandu
Certificate

This is to certify that Sleshma Thapa, student of BBA(2nd semester) of symbol number 36283/22 has successfully completed her lab
assignment on Database Management System in the practical examination. It is further certified that this project is the individual work of
the candidate.

Date: 2080/

Internal Examiner External Examiner

Signature: Signature:
Acknowledgement

This report has been prepared as partial requirement for the internal evaluation of Bachelor of Business Administration (BBA). It is great
opportunity for me to be part of this project.

I would like to thank my teacher Mr. Hem Sagar Pokhrel for helping me on this project. He allowed me to work on this project . Along with
that, I would also like to thank my college whole heartedly. I would also want to thank my friends who helped me in finalizing this project
within a limited time frame.
CONTENTS

Lab assignment #1 .....................................................................................................................

Lab assignment #2 .....................................................................................................................

Task 1 .....................................................................................................................................

Task 2 .....................................................................................................................................

Task 3 .....................................................................................................................................

Lab Assignment #3 ..................................................................................................................


LAB ASSIGNEMENT 1
Schema :

Student ( sid, sname, level, age, sex)

Sid : primary key

Age : Not NULL

1. Create the above table


- CREATE table student ( sid int PRIMARY KEY, sname varchar(20), level varchar (30), age int NOT NULL, sex varchar (8));

Output:
2. Insert record in it
- INSERT INTO student VALUES(05,'sleshma','Graduate',19,'female');

Output:
3. Display all records
- SELECT * FROM student ;

Output:
4. Add new column contact no

- Alter table student add contactno integer;

Output:

- SELECT * from student ;

Output:
5. Remove column level

- ALTER table student DROP COLUMN level ;

- SELECT* FROM student ;

Output:
6. Add new column level

- Alter table student add level varchar (20) ;

- SELECT * FROM student ;

Output:

7. Change the data type varchar to char in sname

- Alter table student MODIFY sname char (20);

- DESC student ;

Output:
8. Put the values in contact no [ due to limited byte real values cannot be display so modify the datatype integer into varchar (15) ]

- ALTER table student MODIFY contactno varchar (15);

- UPDATE student

- SET contactno = 9848886471 ;

- SELECT* FROM student ;

- UPDATE student

- SET contactno = 9806478941

- WHERE sid= 01 ;

- SELECT * from student ;

Output:
9. Insert four more records and delete the record of Thor who left the college recently

- INSERT INTO student VALUES

- ( 01,'silu’, 18 , 'female' , 9808589758 , 'graduate'),

- ( 03 ,'abhishek ' , 19, ‘male' ,9818567898 ,'undergraduade'),

- (02,' arbin ' , 20, 'male' , 9856528909 ,'undergraduate'),

- ( 04,'seela' ,22 ,' male', 9741314910, 'graduate' );

Output:

COMMAND TO DELETE A RECORD

- DELETE FROM student where sname = 'abhishek ' ;

- SELECT * from student;

Output:
10. Delete all records

- Delete from student ;

- Select * from student ;

Output:

11. Remove the student table

- DROP TABLE student ;

- SELECT * from student;


Output:

12. create the table Student ( sid , sname , level, age , sex )

Sid : primary key

Age : Not Null

Level : Default “ Graduate “

Age <= 20

- Create table student

- ( sid int Primary Key ,

- sname varchar (15) ,

- level varchar (20) DEFAULT ' graduate' ,

- age int not null ,


- sex varchar (8) ,

- check (age <= 20 ) );

Output:
Lab Assignment 2
Task 1

Create the following tables under database called universitydb:

- CREATE DATABASE universitydb ;

Student (sid,sname,age,sex)

- CREATE TABLE student (sid integer PRIMARY KEY, sname varchar(15),

- level varchar (15), age integer , sex varchar (8));

Output:

Instructor( iid, Iname, age , sex)

Course (cid, cname,credits hours)

Enrollby ( sid,cid )

Taghtby ( iid,cid )

- CREATE table instructor ( iid integer PRIMARY KEY, iname varchar (15) , age integer,sex varchar(15));

- CREATE table course (cid varchar(6) PRIMARY KEY ,cname varchar (15), credithours integer);

- CREATE TABLE enrollby (sid integer, cid varchar(6), FOREIGN KEY(sid) REFERENCES student(sid), FOREIGN KEY(cid) REFERENCES
course(cid));

- CREATE table taughtby (iid integer , cid varchar(6) , FOREIGN KEY (iid) REFERENCES instructor (iid), FOREIGN KEY (cid) REFERENCES
course(cid));
Output:
Task 2

Insert at least 5 records in every table.

- INSERTINTOStudent VALUES(101,’sleshma’,’graduate’,20,’female’),(106,’silu’,’graduate’,20,’female’),
(107,’anamika’,’graduate’,20,’female’);

Output:

INSERT into instructor


values(201,'A',21,'male'),(202,'B',16,'female'),(203,'C',26,'male'),(204,'D',27,'female'),(205,'E',31,'
male');
- SELECT * FROM instructor;
Output:

- INSERT into course values (301,'BBA',2),(302,'BBS',5),(303,'IT',6),(304,'BBA',2),(305,'BIT',6);

- SELECT * FROM COURSE ;


- insert INTO taughtby values (201,301),(202,302),(203,303),(204,304),(205,305);

- Select * from taughtby ;

Output:
- INSERT into enrollby VALUES (101,301),(102,302),(103,303),(104,305),(105,304);

- Select * from enrollby;

Output:
Task 3

Write the SQL Statements for the following Queries

1. Print or Display or Find all the records of the student.

- SELECT* FROM student;

Output:
2. Print or Display or Find all the records of the instructor.

- SELECT* FROM instructor;

Output:
3. Print or Display or Find all the records of the course.

- SELECT* FROM course;

Output:
4. Find the name and level of the female student over the age 22.

- SELECT sname, LEVEL FROM student

- WHERE age > 22 AND sex ='female';

Output:
5. Find the name of instructor whose age is less than 28.

- SELECT iname FROM instructor

- WHERE age < 28;

Output:
6. Find id, name, level and age of student by increasing age by 1.

- SELECT sid , sname, level, age +1 as newage

- FROM student;

Output:
7. List the name of the instructors whose age is greater than 30 .

- SELECT iname FROM instructor

- WHERE age > 30;

Output:
8. Find the name of student who study SAD.

- SELECT sname FROM student NATURAL JOIN course NATURAL JOIN enrollby

- where cname = 'SAD';

Output:
9. Find the name and age of those students whose age is greater than 25 and sex is male.

- SELECT sname , age FROM student

- WHERE age>25 AND sex ='male';

Output:

10. Increase 50% credit_hours to all courses.

- UPDATE course

- SET credithours= credithours*1.5;

- Select* from course;

Output:
11. Display the name of those students whose level is unknown.

- SELECT sname FROM student

- WHERE level = 'unknown';

Output:
12. List the students who are not enrolled in any courses.(hint: Select* from student natural join enrolled by natural join course where cid is
NULL).

- SELECT * FROM student NATURAL JOIN enrollby NATURAL JOIN course where cid is null ;

Output:

13. List all the course name of 3 credit hours.

- SELECT cname from course WHERE credithours =3;

Output:
14. Update the level of student Sita with postgraduate.

- UPDATE student SET level='postgraduate' WHERE sname ='sita';

Output:

15. Delete the instructor Sweta from the instructor relation.

- DELETE FROM instructor WHERE iid = 'B';

Output:
16. Find iname of all instructors whose name contains ‘an’ somewhere.

- SELECT iid, iname FROM instructor WHERE iname LIKE ’%an%’;

Output:

17. Find name and level of students whose age is between 20 and 25.

- SELECT sname, level FROM student WHERE age>20 AND age <25;

Output:
18. How many students are taking course of 3 credit hours .

- SELECT COUNT(sid)as numberofstudents FROM student NATURAL

JOIN enrollby WHERE credithours=15;

Output:

19. Find id and name of all instructors in the order of sex and then in descending order of age.

- SELECT iid, iname FROM instructor ORDER BY sex , age DESC ;


Output:

20. Find id and name of all undergraduate students in ascending order of name .

- SELECT sid, sname FROM student WHERE LEVEL = 'undergraduate' ORDER BY sname ASC;

Output:
21. Find record of all courses taught by instructor 302.

- SELECT * FROM course NATURAL JOIN taughtby WHERE iid =302;

Output:

22. Find id and name of students who enrolled in course BBA.

- SELECT sid, sname FROM student NATURAL JOIN course NATURAL JOIN enrollby WHERE cname='BBA';

Output:
23. Find id,name , level of all students whose age is greater than at least one student.

- SELECT sid,sname,level

- FROM student

- WHERE age>(SELECT MIN (age) FROM student);

Output:

24. Find average age of all undergraduate students.

- SELECT AVG (age) from student WHERE level ='undergraduate' ;


Output:

25. Find the average age for all levels of students.

- SELECT AVG (age) FROM student ;

Output:
26. Display the name of instructor with maximum age.

- SELECT iname

- FROM instructor WHERE age=(SELECT MAX(age) FROM instructor);

Output:
27. Find name of courses taught by each male instructors.

- SELECT iname,sex,COUNT(cid)AS num_of_course FROM course NATURAL JOIN taughtby

- NATURAL JOIN instructor WHERE sex='male'GROUP BY iid;

Output:
28. Find average age for all levels of student that have average age more than 20.

- SELECT LEVEL , AVG (age) AS average_age FROM student GROUP BY LEVEL HAVING AVG (age)>20;

Output:
29. Add new column ‘addresses’ to student table.

- ALTER TABLE student ADD (addresses VARCHAR (30) );

- SELECT * from student;

Output:
30. Remove column ‘addresses’ from student table.

- ALTER TABLE student DROP addresses;

Output:
31. Modify student relation by changing data type of sname to varchar(30).

- ALTER TABLE student MODIFY sname varchar (30) ;

Output:

32. Create view that contains id, name, level of those students whose age is greater than 24.

- CREATE VIEW student .view AS SELECT sid, sname , level FROM student WHERE age > 24;

Output:
33. Remove the view table just created.

- DROP view student .view;

Output:
Lab Assignment 3

1. Find ID and name of all females who are instructor or student. Also identify the different between

union and union all .

- SELECT sid AS id , sname AS name FROM student WHERE sex ='female' UNION ALL

- SELECT iid AS id, iname as name FROM instructor WHERE sex ='female'

Output:
2. Find ID , name and age of all persons who are instructors as well as student .

- SELECT DISTINCT student.sid AS id , student.sname AS

- name , student.age AS age FROM student INNER JOIN instructor ON

- student.age = instructor.age = INNER JOIN enrollby ON

- student.sid = enrollby.sid INNER JOIN taughtby ON

- instructor. iid = taughtby. iid ;

3. Find ID , name and age of all persons who are instructors but not student.

- SELECT instructor.iid AS id , instructor.iname as Name , instructor.age as age FROM instructor

LEFT JOIN student ON instructor.age = student.age WHERE student.sid IS null;

Output:
4. Find the average age for all levels of students .

- SELECT LEVEL, AVG (age) AS average_age FROM student GROUP BY LEVEL;

Output:
5. Find average age for all levels of students that have average age more than 22.

- SELECT level , AVG (age) AS average_age FROM student GROUP BY level HAVING AVG (age)>22;

Output:
6. Select the name of instructor who teaches computer science , and whose names are neither raja or ram.

- SELECT iname FROM instructor NATURAL JOIN taughtby NATURAL JOIN course WHERE cname ='computer science' AND iname NOT IN
('raja','ram');

Output:

7. Find ID , name and age of all instructor whose age is greater than age of all students.

- SELECT iid,iname,age FROM instructor WHERE age>(SELECT MAX(AGE)FROM student);

Output:
8. Increase credit hour of courses by 5 whose credit hour is more than 3 otherwise increase credit hour by 1.

- UPDATE course SET credithours= CASE WHEN credithours >3 THEN credithours+5 ELSE credithours +1 END ;

Output:

You might also like