0% found this document useful (0 votes)
20 views29 pages

Doc1 2

This lab report on Database Management System provides a comprehensive guide to using MySQL for database creation, modification, and data retrieval. It includes various SQL queries and syntax for tasks such as creating databases and tables, inserting and deleting records, and applying constraints. The document serves as a valuable resource for mastering MySQL queries and understanding database management concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views29 pages

Doc1 2

This lab report on Database Management System provides a comprehensive guide to using MySQL for database creation, modification, and data retrieval. It includes various SQL queries and syntax for tasks such as creating databases and tables, inserting and deleting records, and applying constraints. The document serves as a valuable resource for mastering MySQL queries and understanding database management concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Nagarjuna College Of Information Technology

Sankhamul, Lalitpur
Affiliated to Tribhuwan University

Lab Report on Database Management System


2024

Submitted by: Submitted to:

Dolma Thokar Mohan Singh Ayer

Roll no: 8 Lecturer of DBMS

BBM 6th semester

Keywords:DBMS,SQL,DDL,ER-Data Model,etc.
Abstract
This project aims to harness MySQL for creating ,modifying,and
retrieving data in databases,providing a comprehensive guide to
database management.It covers syntax,queries and outcomes,offering
clear solutions for various tasks.Additionally,it explores techniques like
altering,dropping,and truncating databases,as well as Iaddresses SQL
constraints such as NOT NULL,UNIQUE,PRIMARY KEY,FOREIGN
KEY,CHECK,and DEFAULT constraints.It demonstrates the creation of
virtual tables,data display using pattern matching with the LIKE clause,
and the use of joins like Natural join and various types of Outer
join.Furthermore,it showcases the application of operators like
IN,NOT,BETWEEN,and NOT BETWEEN,making it a valuable resource for
mastering MySQL queries.

Acknowledgement
I want to thank Mr.Mohan Singh Ayer,my teacher,for his amazing
guidance.His support and encouragement were crucial for me to finish
the assignment. I'm also grateful for his help in proofreading and
correcting my mistakes.I feel lucky to have had his consistent support.

I'm thankful to my friends for helping with the project.And to the college
staff for creating a supportive environment.Thanks to everyone who
assisted me along the way.
Table of contents
Abstract

Acknowledgement

Introduction

Background of the study

References

Introduction

Background of the study


SQL, the standard language for managing relational database,is crucial
for manipulating data through querying,inserting,updating,and
modifying.Widely supported by relational database systems like
MySQL,Oracle and SQL server,SQL is indispensible for database
administrators working across diverse platforms.MySQL,an open-source
RDBMS developed by Oracle,stands out as the top choice for web-based
software applications due to it's powerful capabilities and widerspread
adoption.
LAB 1.
Q.N.1. Create database as per your first_name
Solution:

Syntax: create database<database_name>

Query: Create database dolmadb;

Result:

Q.N.2. Create Table Student with primary key.


Solution:
Syntax: Create table <table_name>
Query; create table student
(
sid int(10) Primary key,
sname varchar(100),
Address varchar(100),
Email varchar(100)
);
Result:

Q.N.3. Create table department with foreign key sid.

Solution:

Syntax; create table department <table_name>

Query: create table department

(
Did int(10)primary key,

Dname varchar(100),

Sid int,

Foreign key(sid)references student(sid)

);

Result:

Q.N.4. Describe structure of table student.

Solution:

Syntax:Desc table_name;

Query: desc student;

Result;

Q.N.5. Rename table employee with new name employee_details.


Solution; alter table_name rename new table_name;

Query: alter table employee rename employee_detail;

Result;

LAB 2.
Q.N.6. Delete table employee from your database
Solution;

Syntax; drop table_name

Query: drop table employee_detail;

Result;

Q.N.7 Insert id, name, address, phone no in table employee.


Solution;

Syntax; insert into table_name(column 1, column 2,….)values(value1,value2,…..)

Query : insert into


employee(eid,ename,address,phone)values(101,'dolma','lalitpur',9745957159)

Insert into
employee(eid,ename,address,phone)values(102,'sarah','Nakhu',9843882346);

insert into
employee(eid,ename,address,phone)values(103,'alan','baudha',9867543568)

insert into
employee(eid,ename,address,phone)values(104,'tika','bhaktapur',9849399039)

Insert into
employee(eid,ename,address,phone)values(105,'kiran','bhaktapur',9745957123);

Result:
Q.N.8. Write a query to delete all employee record who are from Bhaktapur.
Solution:
Syntax; delete from <table_name>where address=' ';
Query: delete from employee where address= 'Bhaktapur';
Result;

Q.N.9. Write a SQL statement to make table empty.


Solution;
Syntax;truncate<table><table_name>
Query; truncate table student;
Result;

Q.N.10. Update table student set new address kathmandu whose id is 2.

Solution:
Syntax; update table<table_name>set<new_name>where column_name=column id;
Query: update student set address='kathmandu' where sid=2;
Q.N.11. Delete student's name from table student whose id is 1.
Syntax;delete from <table_name>where column_name=id;
Query: delete from student where sid=1;

Result:

Q.N.12. Display all records of students from student table.


Solution;
Syntax ; Select * from <table _name>;
Query: select * from student;
Result;

Q.N.13. Print or display employee's name from employee table who are working in IT
department.
Solution;
Syntax; select column name from table 1 join table 2 on
table1.column_name=2.column_name;

Query; select ename from employee join department on employee.eid=department.fid


where department.dname='IT'

Result;
Q.N.14. Write a query to create table department where fid is working as a foreign
key.

Solution;

Syntax; create <table_name>

<column _name><data type>,

Foreign
key<column1_name>references<primary_key_table_name(primary_key_column_na
me)

);

Query : create table department

-> (

-> did int(10) primary key,

-> fid int,

-> foreign key(fid)references employee(eid)

-> );

Result:

Q.N.15. Write a query which will increase the salary of each department by Rs 1000
Solution:

Syntax;update<table_name>set<column_name>=column+<value>;
Query : update employee set salary=salary + 1000;
Result:

LAB 3.

Q.N.16. Write a query to increase salary of employees by 5% whose salary is more


than 9000.
Syntax: update<table_name>set<column_data>=<column_data>+
(column_data*percentage)where column_data>amount;

Query: update employee set salary=salary+(salary*0.05)where salary>9000;

Result;

Q.N.17. Write a query to change the department id of employees whose old


department id is 201. The new department id should be 501.
Solution;

Syntax; update <table_name>set 'new_name' where column id=column_id;

Query; update department set did=501 where did=201;


Result:
Q.N.18. Write a query to select name, address and salary of all employees who are
from Kathmandu.
Solution:

Syntax;select<column1,column2,…….>from <table_name>where column_name


IN(column_data);

Query: select ename,address,salary from employee where address IN('kathmandu')

Result:

Q.N.19. Write a query to select name, address and salary of all employees whose
department id is 2002
Solution;

Syntax: select column1,column 2….from table1 join table 2 on


table1.column_name=table 2 column_name where table 2 column1_name;

Query: select ename,address,salary from employee join department on


employee.eid=department.fid where did=200;

Result:

Q.N.20. Create table student where all fields should be not null.

Solution:

Syntax;Create table<table_name>
(
<column1><datatype>not NULL,
<column2><datatype>not NULL,
);

Query: create table student


-> (
-> sid int(10) primary key,
-> sname varchar(100) NOT NULL,
-> address varchar(100) NOT NULL
-> );
Result:

Q.N.21. Create table exam marks and sname should be unique.


Solution;

Syntax;Create table<table_name>
(
<column1><datatype>UNIQUE,
<column2><datatype>UNIQUE,
);

Query; create table exam


-> (
-> sid int(10)primary key,
-> sname varchar(100) UNIQUE,
-> marks int(10) UNIQUE
-> );
Result:

Q.N.22. Create table college where cid should be PRIMARY KEY.


Solution:
Syntax:Create table<table_name>
(
<column1><datatype>primary key,
<column2><datatype>
);

Query: create table college


-> (
-> cid int(10) primary key auto_increment,
-> cname varchar(100),
-> location varchar(100)
-> );
Result:

Q.N.23. Create table customer and Orders and orders table should have foreign key.
Solution;

Syntax: Create table<table_name>


(
<column1><datatype>,
<column2><datatype>
);
:create table<table_name>
(
<column1><datatype>,
Foreign key(column1_name)references<primary key table_name>(primary key
column_name));

Query:
create table customers
-> (
-> cusid int(2) primary key
-> );
: create table orders
-> (
-> oid int(2) NOT NULL,
-> fooditem varchar(50),
-> foreign key(oid) references customers(cusid)
-> );
Result:

Q.N.24. Create table atm where atmid should be greater than 0.

Solution:

Syntax: Create table <table_name>

<column1><datatype>,

<column2><datatype>,

CHECK(column1>0)

);

Query: create table atm


-> (

-> atmid int(10) NOT NULL primary key,

-> name varchar(100),

-> address varchar(100),

-> CHECK(atmid>0)

-> );

Result

Q.N.25. Create table bank where default balance should be 1000.

Solution:

Syntax;Create table <table_name>

<column1><datatype>default value,

<column2><datatype>default value

);

Query: create table bank


-> (

-> bid int(10) primary key auto_increment,

-> bname varchar(100),

-> address varchar(100),

-> balance decimal(10,2) default 1000

-> );

Result:

LAB 4

Q.26. Write a SQL query to retrieve empno, ename, job,salary of all employees in
descending order of their salary.

Solution;

Syntax;create table <table_name>

Query: select ename,phone,job,salary from employee ORDER BY salary DESC;

Result:

Q.27. Write a SQL query to retrieve all information of employee that belongs to
department number 10 or 20.

Solution:
Syntax: Select all from <table1_name>join<table2_name>on
primary_key_table_name.column1_name>=<foreign_key_table_name.column1_nam
e>where table2 column2_name=value1 OR value2;

Query: select * from employee join department on employee.eid=department.fid


where depno=10 OR 20;

Result:

Q.28. Write a query in SQL to list the employee who does not belongs department no
10.

Sollution;

Syntax;select all from <table1_name>join<table2_name>on


primary_key_table_name.column1_name>=<foreign_key_table_name.column1_nam
e>where column2_name<>value;

Query;

select * from employee join department on employee.eid=department.fid where


depno<>10;

Result:

Q.29. Write a SQL query to retrieve employee information whose salary is greater
than average salary of all employee.

Solution;

Syntax;Select all from <table_name>where column_name>value;

Query; select * from employee where salary>24255;

Result:
Q.30Write a SQL query to display all employee who does not have any commission.

Solution;

Syntax; Select all from <table_name> where column_name is NULL;

Query:select * from employee where commission is NULL;

Result:

Q.31. Display empname and annual total salary of individual’s employee from
employee table.

Solution;

Syntax: Select column1_name,column2_name * 12 AS annual_total_salary


from<table_name>;

Query: select ename,salary*12 AS annual-total_salary from employee;

Result:

Q.32. Write a SQL query to display department number and total salary of each
department (use group by clause).

Solution;

Syntax;Select column 1_name,SUM(column2_name)AS total column2_name


from<table1_name>join
<table2_name>on<primary_key_table_name.column_name>=foreign_key_table_nam
e.column_name>GROUP BY column1_name;
Query:

select depno,SUM(salary)AS total_salary from employee join department on


employee.eid=department.fid GROUP BY depno;

Result:

Q.33. Write a SQL query to display information of employee whose name starts with
A.

Solution;

Syntax: Select all from <table_name> WHERE column1_name LIKE 'expression%';

Query: select * from employee WHERE ename LIKE 'A%';

Result:

Q.34. Write a SQL query to find out total number of department in the given
employee table

Solution:

Syntax:Select COUNT(DISTINCT column_name)AS total_table2_name


from<table1_name>join<table2_name>on<primary_key_table_name.column_name>
=<foreign_key_table_name.column_name>;

Query:

select COUNT(DISTINCT depno) AS total_department from employee join


department on employee.eid=department.fid;

Result:
Q.35. Display all the information of employee whose salary is between 3000 AND
5000.

Solution:

Syntax: select all from <table_name>WHERE column_name BETWEEN value1


AND value2;

Query: select * from employees where salary BETWEEN 3000 AND 5000;

Result:

LAB 5

Q.36. Display different job levels of employees in employee table.

Solution:

Syntax: Select column1,column2,….*12 AS annual_column_name


from<table_name> ORDER BY column name;

Query: select ename,job,salary * 12 AS annual_salary from employee ORDER BY


ename;

Result:

Q.37. Display empname, job, annual of employee using order by clause.

Solution:

Syntax: : Select column1,column2,….*12 AS annual_column_name


from<table_name> ORDER BY column name;
Query: select MAX(salary) AS max_salary from employee;

Result:

Q.38. Print the minimum salary of employee.

Solution:

Syntax: select MIN(column_name)from<table_name>;

Query: select MIN(salary) from employee;

Result:

Q.39. Print the total salary of all employee

Solution;

Syntax: Select SUM(column_name)from <table_name>;

Query: select SUM(salary) from employee;

Result:

Q.40. Display the average salary of employee.

Solution:

Syntax: Select avg(column_name)from<table_name>;

Query: select avg(salary)from employee;

Result:
Q.41. Display branch and total marks of individual department from student table
using group by clause.

Solution:

Syntax: Select column1,column2 from table_name GROUP BY column;

Query: select branch,marks from student group by sid;

Result:

Q.42. Display branch and total marks of IT branch only from student table.

Solution:

Syntax:select column1,sum(column2)from table_name where column1='value';

Query: select branch,sum(marks)from student where branch='IT';

Result:

Q.43. Create view name as viewIT on the basis of IT branch.

Solution:

Syntax:create view view_name as select * from table_name where column='value';

Query: create view viewIT as select * from student where branch='IT';


Result:

Q.44. Delete id no 101 from viewIT.

Solution:

Syntax:delete from<table_name>where column_name=value;

Query: delete from viewIT where sid=101;

Result

Q.45. Write a query to display employee in which city name starts with ‘ka’ ends with
‘ti’ and contains multiple character between ‘ka’ and ‘ti’.

Solution:

Syntax:select column1 from table_name where column2 LIKE 'start_letter


%end_letter';

Query:

Result:
LAB 6

Q.46. To find employee name containing exactly 5 characters use 5 instances of the
_pattern character.

Solution:

Syntax: Select column_name_from <table_name>where column_name LIKE'_____';

Query: select ename from employee where ename LIKE'_____';

Result:

Q.47. Display all the records from left table with matched records from right table.
There are two tables given to you as employee and department.

Solution:

Syntax:Select column_name from table1_name left join table2_name on


table1.column_name=table2.column_name;

Query: select * from employee left join department on


employee.eid=department.eid;Result:

Q.48. Display employee name and department name from employee and department
table using natural join.
Solution:

Syntax:Select column_name from table1 natural join table2;

Query: select ename,dname from employee natural join department;

Result:

Q.49. Replace the table employee with table emp.

Solution:

Syntax:alter table table_name rename new_name;

Query: : alter table employee rename emp;

Result:

Q.50. Write a query for adding empno in emp table at first row of the table

Solution:
Syntax:alter table<table_name>add column name<datatype>first;

Query: alter table emp add phone bigint(10)first;

Result:

Q.51. Write a query to display marks details in which marks starts with 4.

Solution:

Syntax:Select all from <table_name.where column_name LIKE 'expression';

Query: select * from student where marks LIKE '4%';

Result:
Q.52. Write a query to display employee details in which name contains letter ‘ik’ in
between.

Solution:

Syntax:Select all from <table_name>where column_name LIKE 'expression'

Query: select * from employee where ename LIKE'%ik%';

Result:

Q.53. Display all employees whose salary is either 3000,4000 or 10000.

Solution:

Syntax: Select all from<table_name>where column_name IN(values);

Query: select * from employee where salary IN(3000,4000,10000);

Result:

Q.54. Display all employees with the salary except between 4000 to 5000.

Solution:
Syntax:Select all from <Table_name>where column_name NOT BETWEEN value AND value;

Query: select * from employee where salary NOT BETWEEN 4000 and 5000;

Resullt:

Q.55. Select all records from employee table where job is not manager.

Solution:

Syntax:Select all from <table_name>where column_name'expression';

Query: select * from employee where job<>'manager';

Result:
References:

Tutorials Point.(2019).SQL Tutorial.tutorialspoint.com,15.

You might also like