Professional Documents
Culture Documents
Database Management System (DBMS) Lab Report
Database Management System (DBMS) Lab Report
MANAGEMENT SYSTEM
(DBMS)
LAB REPORT
FACULTY: BIM
ROLL NO:
Anurag Bhattarai
INDEX
2 Lab 2 2021/03/18
10 Joins 2021/03/18
Anurag Bhattarai
13 Creating views 2021/03/18
Anurag Bhattarai
a. Login to mysql server using root
For Database
For Table
Syntax: Create table table_name(
Attribute 1,Attribute 2);
Description: It creates table.
Query: Create table student_r12(
Rollno int,
FullName varchar(200),
Faculty varchar(40),
ContactNo varchar(100)
);
Anurag Bhattarai
c. List all database in mysql
e. Create table (employee, department with necessary foreign key and assume your
own attributes along with data type)
Anurag Bhattarai
Query: CREATE TABLE employee_r12(
salary int
);
Dname varchar(50),
Location varchar(50)
);
D_ID int ,
Emp_id int
);
Anurag Bhattarai
Alter table department_emp_r12 add constraint dept_emp_fk2 foreign
key(Emp_id) references employee_r12 (id);
f. Listing tables
Anurag Bhattarai
h. Inserting record in a table (at least 10 entries in each table)
Syntax: Insert into table_name (column1,column2) values(“ ”,” ”),(“ ”,” ”);
Description: Data are inserted into a table. Query: Insert into
student_r12(Rollno,FullName,Faculty,ContactNo)values(1,”Ram”,”bi
m”,9987899997),(2,”Angel”,”bim”,9828906754),….(3,”shyam”,”
bim”,9841000034).
Anurag Bhattarai
j. Using where clause
k. Write a query to retrieve all records of employee having salary greater than 50000
l. Write a query to select name, address and salary of all employees who are from
Kathmandu
Page 9
m. Write a query to select name address and salary of all employees whose department
id is 2002.
n. Write a query to list all record of the employees who are from Imadol and whose salary
is less than 91000
o. Write sql to list name of the employee who are from Kathmandu and whose salary is
greater than 50000
Page 10
Syntax: select * from table1 where address=”value” and salary<value.
Description: It displays those record who are from kathmandu and having salary >50000.
p. Write sql to list name of the employee who are form Kathmandu or whose dept id is
2001
(a.address=”kathmandu” or D_ID=2001)
q. Write a query to retrieve name, department id and salary of all employees whose
department id is 2002.
Page 11
Syntax: select name, D_ID,salary from table1,table2 where D_ID=value and
table2.attribute(FK)=table1.attribute(PK).
r. Write a query to select all records of employees where salary is between 2000 to
18000.
Syntax: select name,D_ID,salary from table1,table2 where salary between 2000 and
18000 and table2.attribute(FK)=table1.attribute(PK).
s. Write a query to select all records of employees where salary is not between 2000 to
18000.
Syntax: select name,D_ID,salary from table1,table2 where salary not between 2000 and
18000 and table2.attribute(FK)=table1.attribute(PK).
u. Write a query to retrieve records of employee whose department id is not one of the
flowing (2006, 2004, 2034, 5001).
Page 13
Query: Select a.*, b.dname, b.d_id from employee_r12 as a,
Page 14
2. Lab 2:
c. Write a query which will increase the salary of each department by Rs 1000
Page 15
Syntax: update table set salary=salary+value.
Description: It increase the salary of the employee bt rs1000 Query:
update employee_r12 set salary=salary+1000;
d. Write a query to increase salary of employees by 5% whose salary is more than 9000.
Page 16
e. Write a query to change the department id of employees whose old department id is 2001.
The new department id should be 5001.
f. Write a query to delete all employee record who are from Bhaktapur.
Page 17
a. Write a query to display the records of department table with following table heads:
Department_ko_id, Department_ko_Naam. Department_ko_Location.
b. Write a query to list employee id, employee name, department locations from employee
and department tables.
Page 18
c. Write a query that display employee name and their yearly salary.
a. Write a SQL query to list all employee name, their address and salary in:
Page 20
iv. descending order of their name
Query: select name, salary from employee_r12 order by salary desc, name ;
Page 21
5. String Operation
a. Write a SQL query to list names of employee from employee table where
Page 22
iii. name starts with “M” and ends with “n”
i. Display department id and their location where department includes the substring
“par”
Page 23
c. Write a sql query to list id and names of employee from employee table where
the name starts with “M” and end with “n” and has exactly 5 characters
Description: It displays records of employee whose name start with m and ends with
n and have exactly 5 character.
d. Write a sql query to display employee name and address where name include a
substring “il” and address starts with “I”
Syntax : select column1, column2 from table where column1 like”value” and column2
like”value”.
Query: select name,address from employee_r12 where name like"%il%" and address
like"l%";
Page 24
a. Write a query to find
Page 26
Description: It displays sum of salary.
Query: select sum(salary) from employee_r12.
Description: It displays the record of employee from each department having average
salary
Page 27
ii. Minimum salary of employee of each employee address
iii. Minimum salary of employee of each employee having average salary greater than
3000
Page 28
Description: It displays the record of each employee having average salary greater
than 3000.
Query: select name, salary from employee_r12 as a, (select address, avg(salary) avg_sal
from employee_r12 group by address) as b where b.address=a.address and
b.avg_sal>3000;
OR
select name,min(salary) from employee_r12 group by name having avg(salary)>3000;
Page 29
Description: It displays the record of employee from each department .
Query: select a.Dname, count(b.emp_id) as "no of employees" from
department_r12 as a ,department_emp_r12 as b where a.d_id=b.d_id group by
a.d_id;
Description: It displays the record of employee’s total salary except from employee
of Kathmandu.
vi. Average salary of employee of each department who are from “lalitpur”,
Page 30
Syntax: select
table1.column1,table1.column2,table2.column1,avg(table2.column2) from table
where condition.
Description: It displays the record of employee of each department who are from
lalitpur, Kathmandu and imadol.
vii. Number of employee from each address where salary is greater than 8000
Page 31
8. NULL and NOT NULL clause
a. Write a query to:
Page 32
iii. Display name and address and salary of employee whose address is not null and salary
is greater than 8000
b. Delete the employee whose salary is less than 10000(if not present in
database first insert 5 employees with salary less than 10000 show the table
and delete the table)
Page 34
10. Joins
a. Create table
Query: create table students(std_id int not null primary key,name varchar(100),email
varchar(100),phone varchar(20),dep_id int,add_id int,foreign key(add_id)references
address(add_id));
Page 35
iii. Department(dept_id,dept_name) Assusme necessary data type
Query: create table department(SNO int, dept_id int not null,dept_name varchar(20)not
null,
b. Demonstrate the full join concept with sql query and table for the address,
students and department table
Description: It displays record of students with their name, address and department
with full join concept.
Page 36
c. Demonstrate the Inner join concept with sql query and table for the address,
students and department table
Description: It displays record of students with their name, address and department
with inner join concept.
d. Demonstrate the left join concept with sql query and table for the address,
students and department table
Page 37
Query: Select students.name, address.address,address.state,address.country,
department.* FROM address left JOIN students ON students.add_id =
address.add_id left JOin department ON department.SNO = students.add_id;
e. Demonstrate the right join concept with sql query and table for the address,
students and department table
Description: It displays record of students with their name, address and department
with right join concept.
Page 38
11. Working with referential integrity constraints
a. Creating a table with foreign key(assume your table with necessary table except
already created table)
Query: create table country_r12(id int not null auto_increment primary key, name
varchar(20));
create table citizen_r12(id int not null auto_increment primary key, name
varchar(20),email varchar(50),c_id int, foreign key(c_id) references country_r12(id));
(“Canada”);
Page 39
insert into citizen_r12(name, email, c_id) values
( "K","K@gmail.com",1),("A","A@gmail.com",2),("B","B@gmail.com",3),("C","
C@gmail.com",5),("D","D@gmail.com",4),("E","E@gmail.com",6),("F","F@gma
il.com",7),("G","G@gmail.com",8),("H","H@gmail.com",2),("I","I@gmail.com",5
);
Page 40
Syntax: insert into table(column1,column2,column3)values(),(),..
Description: It inser data into tables.
Query: insert into citizen_r12(name,email,c_id) values
(“Ramprasa”,”Ramprasad@gmail.com”,null);
Page 41
g. Update primary key value which is referenced in child table
Page 42
Syntax: mysql –u raunak –p
Description:It login into different users created in mysql.
Query : mysql -u raunak –p
Page 43
d. Revoke privileges
Page 44
13. Creating Views from table created in 10.a
Page 45
students.std_id,students.n
ame,department.dept_id,department.dept_name from
students,department where students.std_id=department.SNO;
Page 47
f. Delete view
Page 48