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

Lab Manual Content

Uploaded by

god889870
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)
7 views

Lab Manual Content

Uploaded by

god889870
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/ 61

Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -1
OBJECTIVE:
Draw E-R diagram and convert entities and relationships to relation table for a given scenario.
Two assignments shall be carried out i.e. consider two different scenarios (eg. bank, college)
1. COLLEGE DATABSE

STUDENT(USN,SName,Address,Phone,Gender)

SEMSEC(SSID,Sem,Sec)

CLASS(USN,SSID)

SUBJECT(Subcode,Title,Sem,CREDITS)

IAMARKS (USN,Subcode,SSID,Test1,Test2,Test3,FinallA)
PROCEDURE:
1. Create: Open EDRAW software , draw the diagram after that save the diagram .
OUT PUT

Database Management Systems lab 1


Acharya Institute of Graduate Studies Department of Computer Application

Mapping Entities and relationships to relation table (Schema Diagram)


1. COMPANY DATABASE

EMPLOYEE(SSN,Name,Address,Sex, Salary, SuperSSN, DNo)

DEPARTMENT(DNo,DName, MgrSSN, MgrStartDate)

DLOCATION(DNo, DLoc)

PROJECT(PNo, PName , PLocation ,

Database Management Systems lab 2


Acharya Institute of Graduate Studies Department of Computer Application

DNo)WORKS_ON(SSN,PNo, Hours)

Company Database: E-R Diagram

Database Management Systems lab 3


Acharya Institute of Graduate Studies Department of Computer Application

Company Database: Schema Diagram

Database Management Systems lab 4


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -2
OBJECTIVE:
Viewing all databases, creating a database, viewing all tables in a Database, creating tables (With
and Without constraints), Inserting/Updating/Deleting Records in a Table, Saving (Commit) and
Undoing(rollback)

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
Consider the Inventory Database with the following

Product(PID:number;Name:Text;Price:Number)

Purchase(PO:Number;PID:Number;Qty:Number)

Steps:

1. Create Database Inventory

2. Create tables Product and Purchase with and without constraint

3. View all tables in inventory database

4. Insertfive tuples into each relation

5. Display all the tuples in the Product and Purchase Table

6. Update the product name for the PID = 4 as CAMERA

7. Delete information about the product whose PID = 5

8. Perform saving and Undoing

Database Management Systems lab 5


Acharya Institute of Graduate Studies Department of Computer Application

Step 1 : Create Database inventory(Creating a database means creating a user or schema)

mysql> create database

inventory001;mysql> use

inventory001;

Step 2: Create tables Product and Purchase with and without constraint

mysql> create table product (pidint(10) primary key, name varchar(15) not null, price int(8));

mysql>desc product;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| pid | int | NO | PRI | NULL | |
| name | varchar(15) | NO | | NULL | |
| price | int | YES | | NULL | |
+ + + + + + -+3 rows in set (0.07 sec)
mysql> create table purchase (po int(10) primary key,prod_id int(10) references product(pid),

-> qty int(5));

mysql>desc

purchase;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| po | int | NO | PRI | NULL | |

Database Management Systems lab 6


Acharya Institute of Graduate Studies Department of Computer Application

| prod_id | int | YES | | NULL | |


| qty | int | YES | | NULL | |
+ + + + + + +3 rows in set (0.00 sec)

Step 3: View all tables in inventory database

mysql> show tables;

+ +
| Tables_in_inventory001 |
+ +
| product |
| purchase |
+ +
Step 4: Insert five tuples into each relation

mysql> insert into product values

(10,'printer',20000); mysql> insert into product values

(20,'keyboard',20000);mysql> insert into product

values (30,'monitor',15000); mysql> insert into

product values (40,'table',25000); mysql> insert into

product values (50,'scanner',14000);

mysql> select * from product;


+ + + +
| pid | name | price |
+ + + +
| 10 | printer | 20000 |
| 20 | keyboard | 20000 |
| 30 | monitor | 15000 |
| 40 | table | 25000 |
| 50 | scanner | 14000 |

Database Management Systems lab 7


Acharya Institute of Graduate Studies Department of Computer Application

+ + + +5 rows in set (0.00 sec)


mysql> insert into purchase values

(101,10,25); mysql> insert into purchase

values (102,40,20); mysql> insert into

purchase values (107,30,40); mysql> insert

into purchase values (104,40,50); mysql>

insert into purchase values (105,40,10);

mysql> select * from purchase;

+ + + +
| po |prod_id | qty |
+ + + +
| 101 | 10 | 25 |
| 102 | 40 | 20 |
| 104 | 40 | 50 |
| 105 | 40 | 10 |
| 107 | 30 | 40 |
+ + + + 5 rows in set (0.00 sec)
Step 6: Update the product name for the PID = 40 as CAMERA

mysql> update product set name='camera' where

pid=40;mysql> select * from product;

+ + + +
| pid | name | price |
+ + + +
| 10 | printer | 20000 |
| 20 | keyboard | 20000 |
| 30 | monitor | 15000 |
| 40 | camera | 25000 |

Database Management Systems lab 8


Acharya Institute of Graduate Studies Department of Computer Application

| 50 | scanner | 14000 |
+ + + +5 rows in set (0.00 sec)

Step 7: Delete information about the product whose PID = 50

mysql> delete from product where

pid=50;mysql> select * from product;

+ + + +
| pid | name | price |
+ + + +
| 10 | printer | 20000 |
| 20 | keyboard | 20000 |
| 30 | monitor | 15000 |
| 40 | camera | 25000 |
+ + + +4 rows in set (0.00 sec)
Step 8: Perform saving and undoing
mysql> insert into product values

(50,'mobile',35000); mysql> insert into product

values (60,'laptop',70000); mysql> commit;

mysql> select * from product;

+ + + +
| pid | name | price |
+ + + +
| 10 | printer | 20000 |
| 20 | keyboard | 20000 |
| 30 | monitor | 15000 |
| 40 | camera | 25000 |
| 50 | mobile | 35000 |
| 60 | laptop | 70000 |
+ + + +6 rows in set (0.00 sec)

Database Management Systems lab 9


Acharya Institute of Graduate Studies Department of Computer Application

mysql> commit;
mysql> start

transaction;

mysql>savepoint s1;

mysql> insert into product values

(70,'table',50000);mysql> insert into product

values (80,'chair',25000);

mysql> select * from product;

+ + + +
| pid | name | price |
+ + + +
| 10 | printer | 20000 |
| 20 | keyboard | 20000 |
| 30 | monitor | 15000 |
| 40 | camera | 25000 |
| 50 | mobile | 35000 |
| 60 | laptop | 70000 |
| 70 | table | 50000 |
| 80 | chair | 25000 |
+ + + +8 rows in set (0.00 sec)

mysql> rollback to savepoint;

s1;mysql> select * from

product;

Database Management Systems lab 10


Acharya Institute of Graduate Studies Department of Computer Application

+ + + +
| pid | name | price |
+ + + +
| 10 | printer | 20000 |
| 20 | keyboard | 20000 |
| 30 | monitor | 15000 |
| 40 | camera | 25000 |
| 50 | mobile | 35000 |
| 60 | laptop | 70000 |
+ + + +6 rows in set (0.00 sec)

Database Management Systems lab 11


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -3
OBJECTIVE:
Altering a Table, Dropping/Truncating / Renaming a table, backing up/ Restoring aDatabase

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
Consider the library database with the following data and execute the queries
LIB(BID,TITLE,AUTHOR,PUBLICATION,YEAR OF PUBLICATION)

STEPS

1. Create LIB table by properly specifying the constraint

2. Rename the LIB as LIBRARY

3. Add a new column price with not null constraints to the existing table library

4. All the constraints and views that reference the column are dropped automatically along
withthe column.

5. Rename the BID to BOOKID in the LIBRARY table.

6. Insert Data into LIBRARY table

7. Truncate table to delete the record.

8. Drop Table.

Database Management Systems lab 12


Acharya Institute of Graduate Studies Department of Computer Application

Create Database Library:

mysql> create database

library001;mysql> use

library001;

Step 1.Create LIB table by properly specifying the constraint

mysql> create table lib (bid varchar(15) primary key, title varchar(20) not null, author
varchar(20), publication varchar(20),year_of_publication int(4));

mysql>desc lib;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| bid | varchar(15) | NO | PRI | NULL | |
| title | varchar(20) | NO | | NULL | |
| author | varchar(20) | YES | | NULL | |
| publication | varchar(20) | YES | | NULL | |
| year_of_publication | int | YES | | NULL | |
+ + + + + + +5 rows in set (0.00 sec)
Step 2.Rename the LIB as

LIBRARY mysql> alter table lib

rename to library1;mysql>desc

library1;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| bid | varchar(15) | NO | PRI | NULL | |
| title | varchar(20) | NO | | NULL | |

Database Management Systems lab 13


Acharya Institute of Graduate Studies Department of Computer Application

| author | varchar(20) | YES | | NULL | |


| publication | varchar(20) | YES | | NULL | |
| year_of_publication | int | YES | | NULL | |
+ + + + + + +5 rows in set (0.00 sec)
Step 3: Add a new column price with not null constraints to the existing table library

mysql> alter table library1 add price float(8,2) not

null;mysql>desc library1;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| bid | varchar(15) | NO | PRI | NULL | |
| title | varchar(20) | NO | | NULL | |
| author | varchar(20) | YES | | NULL | |
| publication | varchar(20) | YES | | NULL | |
| year_of_publication | int | YES | | NULL | |
| price | float(8,2) | NO | | NULL | |
+ + + + + + +6 rows in set (0.00 sec)

Step 4: All the constraints and views that reference the column are dropped automatically
along with the column.

mysql> alter table library1 drop column author;


mysql>desc library1;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| bid | varchar(15) | NO | PRI | NULL | |
| title | varchar(20) | NO | | NULL | |
| publication | varchar(20) | YES | | NULL | |
| year_of_publication | int | YES | | NULL | |
| price | float(8,2) | NO | | NULL | |
+ + + + + + +5 rows in set (0.00 sec)

Database Management Systems lab 14


Acharya Institute of Graduate Studies Department of Computer Application

Step 5: Rename the BID to BOOKID in the LIBRARY

table.mysql> alter table library1 rename column bid to

bookid; mysql>desc library1;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| bookid | varchar(15) | NO | PRI | NULL | |
| title | varchar(20) | NO | | NULL | |
| publication | varchar(20) | YES | | NULL | |
| year_of_publication | int | YES | | NULL | |
| price | float(8,2) | NO | | NULL | |
+ + + + + + +5 rows in set (0.00 sec)
Step 6: Insert Data into LIBRARY table

mysql> insert into library1 values

('sp001','dbms','skyward_publishers','2022',300);mysql> insert into library1

values ('sp002','java','oxford_publishers','2021',400); mysql> select * from

library1;

+ + + + + +
| bookid | title | publication | year_of_publication | price |
+ + + + + +
| sp001 | dbms | skyward_publishers | 2022 | 300.00 |
| sp002 | java | oxford_publishers | 2021 | 400.00 |
+ + + + + +2 rows in set (0.00 sec)
Step 7: Truncate table to

delete the record. mysql>

truncate table library1;

mysql> select * from

Database Management Systems lab 15


Acharya Institute of Graduate Studies Department of Computer Application

library1; Empty set (0.00

sec)

Step 8: Drop Table.

mysql> drop table

library1;mysql>desc

library1;

ERROR 1146 (42S02): Table 'library001.library1' doesn't exist

Database Management Systems lab 16


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -4
OBJECTIVE:
For a given set of relation schemes, create tables and perform the following simple Queries,
Simple Queries with aggregate functions, Queries with aggregate functions (group by and
having clause)

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
STEPS

1. Create table Salary

2. Enter five tuples into the table

3. Display employee number and their salary

4. Find the sum of salaries of all the employees

5. Find the sum and average salaries of employees of a particular department

6. Find the number of employees working for each department.

7. Dispaly employee information in ascending and descending order of their date of joining

8. Find the highest salary that an employee draws

9. Find the leat salary that an employee draws

10. Display the details of employee whose name is Rushank and salary is greater than 50000

Database Management Systems lab 17


Acharya Institute of Graduate Studies Department of Computer Application

Create Database salarybd:

mysql> create database

salarydb;mysql> use salarydb;

Step 1: Create table Salary

mysql> create table salary (enovarchar(10) primary key, name varchar(20) not null, dept
varchar(10), doj date, salary float(10,2));

mysql>desc salary;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| eno | varchar(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| dept | varchar(10) | YES | | NULL | |
| doj | date | YES | | NULL | |
| salary | float(10,2) | YES | | NULL | |
+ + + + + + +

Step 2: Enter five tuples into the table

mysql> insert into salary values ('sc1010','ahana','hr','15-02-10',60000);

mysql> insert into salary values ('sc1011','ramesh','finance','10-03-

12',45000); mysql> insert into salary values ('sc1013','naveen','marketing','8-

01-09',55000);mysql> insert into salary values ('sc1014','anagha','hr','14-02-

12',35000); mysql> insert into salary values ('sc1015','rushank','admin','16-

05-11',55000); mysql> insert into salary values

Database Management Systems lab 18


Acharya Institute of Graduate Studies Department of Computer Application

('sc1016','rushank','finance','08-06-08',25000); mysql> select * from salary;

+ + + + + +
| eno | name | dept | doj | salary |
+ + + + + +
| sc1010 | ahana | hr | 2015-02-10 | 60000.00 |
| sc1011 | ramesh | finance | 2010-03-12 | 45000.00 |
| sc1013 | naveen | marketing | 0008-01-09 | 55000.00 |
| sc1014 | anagha | hr | 2014-02-12 | 35000.00 |
| sc1015 | rushank | admin | 2016-05-11 | 55000.00 |
| sc1016 | rushank | finance | 2008-06-08 | 25000.00 |
+ + + + + +6 rows in set (0.00 sec)

Step 3: Display employee number and their salary

mysql> select eno,salary from salary;


+ -+ +
| eno | salary |
+ + +
| sc1010 | 60000.00 |
| sc1011 | 45000.00 |
| sc1013 | 55000.00 |
| sc1014 | 35000.00 |
| sc1015 | 55000.00 |
| sc1016 | 25000.00 |
+ + +6 rows in set (0.00 sec
Step 4: Find the sum of salaries of all the employees
mysql> select sum(salary) as "total_salary" from salary;

+ +
| total_salary |
+ +
| 275000.00 |
+ +
1 row in set (0.04 sec)

Database Management Systems lab 19


Acharya Institute of Graduate Studies Department of Computer Application

Step 5: Find the sum and average salaries of employees of a particular department

mysql> select dept,sum(salary) as "total_salary",avg(salary) as "average_salary" from


salarygroup by dept;

+ + + +
| dept | total_salary | average_salary |
+ + + +
| hr | 95000.00 | 47500.000000 |
| finance | 70000.00 | 35000.000000 |
| marketing | 55000.00 | 55000.000000 |
| admin | 55000.00 | 55000.000000 |
+ + + +4 rows in set (0.06 sec)
Step 6: Find the number of employees working for each department.

mysql> select dept,count(*) as "Number_of_Employees" from salary group by dept;

+ + +
| dept | Number_of_Employees |
+ + +
| hr | 2|
| finance | 2|
| marketing | 1|
| admin | 1|
+ + +4 rows in set (0.00 sec)
Step 7: Display employee information in ascending and descending order of their date of
joining

mysql> select * from salary order by doj asc;

+ + + + + +
| eno | name | dept | doj | salary |
+ + + + + +
| sc1013 | naveen | marketing | 0008-01-09 | 55000.00 |

Database Management Systems lab 20


Acharya Institute of Graduate Studies Department of Computer Application

| sc1016 | rushank | finance | 2008-06-08 | 25000.00 |


| sc1011 | ramesh | finance | 2010-03-12 | 45000.00 |
| sc1014 | anagha | hr | 2014-02-12 | 35000.00 |
| sc1010 | ahana | hr | 2015-02-10 | 60000.00 |
| sc1015 | rushank | admin | 2016-05-11 | 55000.00 |
+ + + + + +6 rows in set (0.00 sec)

mysql> select * from salary order by dojdesc;


+ + + + + +
| eno | name | dept | doj | salary |
+ + + + + +
| sc1015 | rushank | admin | 2016-05-11 | 55000.00 |
| sc1010 | ahana | hr | 2015-02-10 | 60000.00 |
| sc1014 | anagha | hr | 2014-02-12 | 35000.00 |
| sc1011 | ramesh | finance | 2010-03-12 | 45000.00 |
| sc1016 | rushank | finance | 2008-06-08 | 25000.00 |
| sc1013 | naveen | marketing | 0008-01-09 | 55000.00 |
+ + + + +
+6 rows in set (0.00 sec)
Step 8: Find the highest salary that an employee draws

mysql> select max(salary) as "Highest Salary" from salary;

+ +
| Highest Salary |
+ +
| 60000.00 |
+ +
1 row in set (0.10 sec)
Step 9: Find the least salary that an employee draws

mysql> select min(salary) as "Least Salary" from salary;

Database Management Systems lab 21


Acharya Institute of Graduate Studies Department of Computer Application

+ +
| Least Salary |
+ +
| 25000.00 |
+ +
1 row in set (0.00 sec)
Step 10: Display the details of employee whose name is Rushank and salary is greater
than50000
mysql> select * from salary where name="rushank" and salary>50000;

+ + + + + +
| eno | name | dept |doj | salary |
+ + + + + +
| sc1015 | rushank | admin | 2016-05-11 | 55000.00 |
+ + + + +
+1 row in set (0.00 sec)

Database Management Systems lab 22


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -5
OBJECTIVE :
Execute the fallowing queries

a. How the resulting salaries if every employee working on the ‘Research’ Departments is given
a 10% raise.

b. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:

Create Database company001:

mysql> create database

company001;mysql> use

company001;

Execute the following queries

Create tables by properly specifying primary key and foreign keys

mysql> create table dept ( dno int(4) primary key, dname varchar(20) not null, dlocation
varchar(20));

mysql> create table employee ( eno int(6) primary key, ename varchar(20) not null, edob date,
address varchar(20), gender varchar(6), salary int(10) not null, deptno int(4) references dept);

Database Management Systems lab 23


Acharya Institute of Graduate Studies Department of Computer Application

mysql> create table project (pno int(10) primary key, pname varchar(20) not null, dnum int(4)
references dept);

mysql> create table works_on ( eno int(6) references employee, pnum int(10) references
project,hours int(3), primary key (eno,pnum));

mysql>desc dept;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| dno | int | NO | PRI | NULL | |
| dname | varchar(20) | NO | | NULL | |
| dlocation | varchar(20) | YES | | NULL | |
+ + + + + + +3 rows in set (1.04 sec)
mysql>desc employee;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| eno | int | NO | PRI | NULL | |
| ename | varchar(20) | NO | | NULL | |
| edob | date | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| gender | varchar(6) | YES | | NULL | |
| salary | int | NO | | NULL | |
| deptno | int | YES | | NULL | |
+ + + + + + +7 rows in set (0.06 sec)

mysql>desc project;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| pno | int | NO | PRI | NULL | |
| pname | varchar(20) | NO | | NULL | |
| dnum | int | YES | | NULL | |

Database Management Systems lab 24


Acharya Institute of Graduate Studies Department of Computer Application

+ + + + + + +3 rows in set (0.00 sec)

mysql>descworks_on;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| eno | int | NO | PRI | NULL | |
| pnum | int | NO | PRI | NULL | |
| hours | int | YES | | NULL | |
+ + + + + + +3 rows in set (0.00 sec)
Insert the values into the tables:

mysql> insert into dept values

(2,'accounts','jayanagar');mysql> insert into dept

values (4,'research','kengeri'); mysql> insert into dept

values (5,'admin','southed'); mysql> select * from

dept;

+ + + +
| dno | dname | dlocation |
+ + + +
| 2 | accounts | jayanagar |
| 4 | research | kengeri |
| 5 | admin | southed |
+ + + +3 rows in set (0.00 sec)
mysql> insert into employee values (1001,'anirudh','1990-01-14','bangalore','male',45000,4);

mysql> insert into employee values (1004,'lakshmi','1998-03-04','mysore','female',5500,4);

mysql> insert into employee values (1002,'sinchana','1990-12-

22','mangalore','female',5000,2);mysql> insert into employee values (1007,'prashant','1989-

Database Management Systems lab 25


Acharya Institute of Graduate Studies Department of Computer Application

01-26','dharwad','male',20000,4);

mysql> insert into employee values (1003,'vinay','1990-11-26','hubli','male',3000,2);

mysql> insert into employee values (1005,'vidya','1978-11-26','hubli','female',35000,4);

mysql> insert into employee values (1006,'prajwal','1974-02-

02','bangalore','male',65000,5);mysql> insert into employee values (1008,'rajesh','2010-

02-10','bangalore','male',25000,2); mysql> select * from employee;

+ + + + + + + +
| eno | ename | edob | address | gender | salary | deptno |
+ + + + + + + -+
| 1001 | anirudh | 1990-01-14 | bangalore | male | 45000 | 4|
| 1002 | sinchana | 1990-12-22 | mangalore | female | 5000 | 2|
| 1003 | vinay | 1990-11-26 | hubli | male | 3000 | 2 |
| 1004 | lakshmi | 1998-03-04 | mysore | female | 5500 | 4|
| 1005 | vidya | 1978-11-26 | hubli | female | 35000 | 4|
| 1006 | prajwal | 1974-02-02 | bangalore | male | 65000 | 5|
| 1007 | prashant | 1989-01-26 | dharwad | male | 20000 | 4|
| 1008 | rajesh | 2010-02-10 | bangalore | male | 25000 | 2|
+ + + + + + + +8 rows in set (0.05 sec)
mysql> insert into project values (10,'erp',5);
mysql> insert into project values
(20,'banking',2);
mysql> insert into project values
(30,'connect_tech',4);mysql> insert into project
values (40,'smart_seek',5); mysql> insert into
project values (50,'finance',2); mysql> insert into
project values (60,'analysis',4);
mysql> insert into project values
(70,'market_research',4);mysql> insert into project
values (80,'smart_search',4); mysql> select * from
project;

Database Management Systems lab 26


Acharya Institute of Graduate Studies Department of Computer Application

+ + + +
| pno | pname | dnum |
+ + + +
| 10 | erp | 5|
| 20 | banking | 2|
| 30 | connect_tech | 4 |
| 40 | smart_seek | 5 |
| 50 | finance | 2|
| 60 | analysis | 4|
| 70 | market_research | 4 |
| 80 | smart_search | 4 |
+ + + +8 rows in set (0.00 sec)
mysql> insert into works_on values
(1001,10,4); mysql> insert into works_on
values (1002,10,6); mysql> insert into
works_on values (1003,10,4);

mysql> insert into works_on values

(1004,20,4); mysql> insert into works_on

values (1005,20,8); mysql> insert into

works_on values (1006,40,8); mysql> insert

into works_on values (1007,50,8); mysql>

insert into works_on values (1008,60,5);

mysql> select * from works_on;

+ + + +
| eno | pnum | hours |
+ + + +
| 1001 | 10 | 4 |

Database Management Systems lab 27


Acharya Institute of Graduate Studies Department of Computer Application

| 1002 | 10 | 6 |
| 1003 | 10 | 4 |
| 1004 | 20 | 4 |
| 1005 | 20 | 8 |
| 1006 | 40 | 8 |
| 1007 | 50 | 8 |
| 1008 | 60 | 5 |
+ + +
+ 8 rows in
set (0.00 sec)

How the resulting salaries , if every employee working on the ‘research ’ departments is
given a 10% raise

mysql> select e.eno,e.ename,d.dname,1.1 * e.salary as 'Inc_Salary' from employee e, dept d


-> where e.deptno=d.dno and d.dname='research';
+ + + + +
| eno | ename | dname | Inc_Salary |
+ + + + +
| 1001 | anirudh | research | 49500.0 |
| 1004 | lakshmi | research | 6050.0 |
| 1005 | vidya | research | 38500.0 |
| 1007 | prashant | research | 22000.0 |
+ + + + +4 rows in set (0.08 sec
(a) Find the sum of the salaries of all employees of the ‘Accounts’ department , as well as
the maximum salary, the minimum salary, and the average salary in this department.

mysql> select max(e.salary),min(e.salary),sum(e.salary),avg(e.salary) from employee e, dept d


where e.deptno=d.dno and d.dname='accounts';

+ + + + +
| max(e.salary) | min(e.salary) | sum(e.salary) | avg(e.salary) |
+ + + + +
| 25000 | 3000 | 33000 | 11000.0000 |
+ + + + +1 row in set (0.05 sec)

Database Management Systems lab 28


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -6
OBJECTIVE :
Execute the fallowing queries
a. Retrieve the name of each employee Controlled by Department number 5 (use EXISTS
operator).
b. Retrieve the name of each dept and number of employees working in each Department which
has at least 2 employees
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
mysql> create database

company001;mysql> use

company001;

Execute the following queries

(a) Retrieve the name of each employee controlled by Department number 5 (use
EXISTSoperator )

mysql> select e.ename from employee e where exists (select d.dno from dept d where
e.deptno =d.dno and e.deptno='5');

+ +
| ename |
+ +
| prajwal |
+ +
(a) Retrieve the name of each dept and number of employees working in each
departmentwhich has at least 2 employees.

Database Management Systems lab 29


Acharya Institute of Graduate Studies Department of Computer Application

mysql> select d.dname, count(*) from employee e, dept d where e.deptno = d.dno group by
d.dname having count(*) >=2;

+ + +
| dname | count(*) |
+ + +
| research | 4|
| accounts | 3|
+ + +
2 rows in set (0.30 sec)

Database Management Systems lab 30


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -7
OBJECTIVE :
Execute the fallowing queries
a. For each project, retrieve the project number, the project name, and the number of employee
who work on that project.(use GROUP BY)
b. Retrieve the name of employees who born in the year 1990’s

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
mysql> create database

company001;mysql> use

company001;

Execute the following queries

(a) For each project retrieve the project number, the project name and the number
ofemployees who work on that project. (use GROUPBY)

mysql> select p.pno, p.pname, count(*) as "No_of_EMPLOYEE" from project p, works_on w


where p.pno=w.pnum group by p.pno,p.pname;

+ + + +

| pno | pname | No_of_EMPLOYEE |

+ + + -+

| 10 | erp | 3|

Database Management Systems lab 31


Acharya Institute of Graduate Studies Department of Computer Application

| 20 | banking | 2|

| 40 | smart_seek | 1|

| 50 | finance | 1|

| 60 | analysis | 1|

+ + + +

5 rows in set (0.08 sec)


(a) Retrieve the name of employees who born in the year 1990’s

mysql> select ename, edob from employee where edob like '1990-%%-%%';

+ + +
| ename | edob |
+ + +
| anirudh | 1990-01-14 |
| sinchana | 1990-12-22 |
| vinay | 1990-11-26 |
+ + +3 rows in set (0.04 sec)

Database Management Systems lab 32


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -8
OBJECTIVE .
For each Department that has more than five employees, retrieve the department number and
number of employees who are making salary more than 40000.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
mysql> create database

company001;mysql> use

company001;

Execute the following queries

1. For each department that has more than five employees , retrieve the
departmentnumber and number of employees who are making salary more
than 40000.

mysql> select d.dname, d.dno, count(*) from employee e, dept d where


e.deptno=d.dnoand salary>40000 and d.dno in ( select deptno from employee group
by deptno having count(*)>=4) group by d.dno,d.dname;

+ + + +
| dname | dno | count(*) |
+ + + +
| research | 4 | 1 |
+ + + +1 row in set (0.00 sec)

2. For each department that has more than two employees , retrieve the
departmentnumber and number of employees who are making salary more than
40000.

Database Management Systems lab 33


Acharya Institute of Graduate Studies Department of Computer Application

mysql> select d.dname, d.dno, count(*) from employee e, dept d where e.deptno=d.dno and
salary>40000 and d.dno in ( select deptno from employee group by deptno having
count(*)>=2)group by d.dno,d.dname;

+ + + +
| dname | dno | count(*) |
+ + + +
| research | 4 | 1 |
| admin | 5 | 2 |
+ + + +2 rows in set (2.35 sec)

Database Management Systems lab 34


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -9
OBJECTIVE . For each project on which more than two employees work, retrieve the project
number, project name and the number of employees who work on that project.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:

mysql> select p.pno,p.pname,count(*) as "No_of_EMP_Working" from project p, works_on w


where p.pno=w.pnum group by p.pno,p.pname having count(*)>2;

+ + + +
| pno | pname | No_of_EMP_Working |
+ + + -+
| 10 | erp | 3|
+ + + +1 row in set (0.28 sec)

Database Management Systems lab 35


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -10
OBJECTIVE :
For a given set of relation tables perform the following . Creating views (with and without
check option). Dropping views, Selecting from a view.

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE
1. Without Check Option

Step 1 : Create View


mysql> create view emp_dept as (select e.eno, e.ename, e.salary, e.deptno, d.dname from
employee e,dept d where e.deptno=d.dno);
Step 2: Display all the rows of a view.

mysql> select * from emp_dept;


+ + + + + +
| eno |ename | salary | deptno | dname |
+ + + + + +
| 1001 | anirudh | 45000 | 4 | research |
| 1002 | sinchana | 5000 | 2 | accounts |
| 1003 | vinay | 3000 | 2 | accounts |
| 1004 | lakshmi | 5500 | 4 | research |
| 1005 | vidya | 35000 | 4 | research |
| 1006 | prajwal | 65000 | 5 | admin |
| 1007 | prashant | 20000 | 4 | research |
| 1008 | rajesh | 25000 | 2 | accounts |
+ + + + + +8 rows in set (0.14 sec)

Database Management Systems lab 36


Acharya Institute of Graduate Studies Department of Computer Application

Step3 : Insert records into a view

mysql> insert into emp_dept( eno, ename, salary, deptno) values (1009,'srikanth',90000,5);

Step 4: Display all the rows of a view

mysql> select * from emp_dept;


+ + + + + +
| eno |ename | salary | deptno | dname |
+ + + + + +
| 1001 | anirudh | 45000 | 4 | research |
| 1002 | sinchana | 5000 | 2 | accounts |
| 1003 | vinay | 3000 | 2 | accounts |
| 1004 | lakshmi | 5500 | 4 | research |
| 1005 | vidya | 35000 | 4 | research |
| 1006 | prajwal | 65000 | 5 | admin |
| 1007 | prashant| 20000 | 4 | research |
| 1008 | rajesh | 25000 | 2 | accounts |
| 1009 | srikanth| 90000 | 5 | admin |
+ + + + + +9 rows in set (0.05 sec)
Step 5: Drop view

mysql> drop view emp_dept;

2. With check option

Step 1: Let us create simple view on EMP table with check option of salary less than
50000in where condition

mysql> create view emp_view as (select eno,ename,salary from employee where salary
<=50000) with check option;

Step 2: Display all the rows of a view

mysql> select * from emp_view;


+ + + +

Database Management Systems lab 37


Acharya Institute of Graduate Studies Department of Computer Application

| eno |ename | salary |


+ + + +
| 1001 | anirudh | 45000 |
| 1002 | sinchana | 5000 |
| 1003 | vinay | 3000 |
| 1004 | lakshmi | 5500 |
| 1005 | vidya | 35000 |
| 1007 | prashant| 20000 |
| 1008 | rajesh | 25000 |
+ + + +7 rows in set (0.07 sec)

Step 3. Insert a row where employee salary is less than 50000

mysql> insert into emp_view values(1011,'snigdha',39000);

Step 4: Display all the rows of a view

mysql> select * from emp_view;


+ + + +
| eno |ename | salary |
+ + + +
| 1001 | anirudh | 45000 |
| 1002 | sinchana | 5000 |
| 1003 | vinay | 3000 |
| 1004 | lakshmi | 5500 |
| 1005 | vidya | 35000 |
| 1007 | prashant| 20000 |
| 1008 | rajesh | 25000 |
| 1011 | snigdha | 39000 |
+ + + +8 rows in set (0.00 sec)
Step 5 : Insert a row where employee salary is greater than 50000. This will give an error

mysql> insert into emp_view values (1012,'Smayan'99999);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

Database Management Systems lab 38


Acharya Institute of Graduate Studies Department of Computer Application

corresponds to your MySQL server version for the right syntax to use near '99999)' at line 1

Step 6: Drop View

mysql> drop view emp_view;

Database Management Systems lab 39


Acharya Institute of Graduate Studies Department of Computer Application

PART B

PROGRAM NO -1
OBJECTIVE : 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)

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
mysql> create table Branch (BRID INT PRIMARY KEY,BNAME VARCHAR(15)NOT
NULL,HOD VARCHAR(10));

Query OK, 0 rows affected (0.34 sec)

mysql> DESC BRANCH;

-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| BRID | int(11) | NO | PRI | NULL | |

| BNAME | varchar(15) | NO | | NULL | |

| HOD | varchar(10) | YES | | NULL | |

Database Management Systems lab 40


Acharya Institute of Graduate Studies Department of Computer Application

+-------+-------------+------+-----+---------+-------+

• mysql> CREATE TABLE STUDENT(USN VARCHAR(15)PRIMARY


KEY,NAME VARCHAR(20),ADDRESS VARCHAR(15),BRID INT REFERENCES
BRANCH,SEM VARCHAR(5));

Query OK, 0 rows affected (0.39 sec)

mysql> DESC STUDENT;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| USN | varchar(15) | NO | PRI | NULL | |

| NAME | varchar(20) | YES | | NULL | |

| ADDRESS | varchar(15) | YES | | NULL | |

| BRID | int(11) | YES | | NULL | |

| SEM | varchar(5) | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

• mysql> CREATE TABLE AUTHOR(AID INT PRIMARY KEY,ANAME


VARCHAR(15),COUNTRY VARCHAR(15),AGE INT);

Query OK, 0 rows affected (0.41 sec)

mysql> DESC AUTHOR;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |

Database Management Systems lab 41


Acharya Institute of Graduate Studies Department of Computer Application

+---------+-------------+------+-----+---------+-------+
| AID | int(11) | NO | PRI | NULL | |
| ANAME | varchar(15) | YES | | NULL | |
| COUNTRY | varchar(15) | YES | | NULL | |
| AGE | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
• mysql> CREATE TABLE BOOK(BKID VARCHAR(10)PRIMARY
KEY,BNAME VARCHAR(15),AID VARCHAR(10)REFERENCES
AUTHOR,PUBLISHER VARCHAR(20),BRID INT REFERENCES BRANCH);

Query OK, 0 rows affected (0.43 sec)

mysql> DESC BOOK;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| BKID | varchar(10) | NO | PRI | NULL | |
| BNAME | varchar(15) | YES | | NULL | |
| AID | varchar(10) | YES | | NULL | |
| PUBLISHER | varchar(20) | YES | | NULL | |
| BRID | int(11) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> CREATE TABLE BORROW(USN
VARCHAR(15)REFERENCES STUDENT,BKID VARCHAR(10) REFERENCES
BOOK,BORROW_DATE DATE);
Query OK, 0 rows affected (0.39 sec)

• mysql> DESC BORROW;

+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| USN | varchar(15) | YES | | NULL | |
| BKID | varchar(10) | YES | | NULL | |
| BORROW_DATE | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Database Management Systems lab 42


Acharya Institute of Graduate Studies Department of Computer Application

1. Perform the following:


a. Viewing all databases,Creating a Database,Viewing all Tables in a Database,Creating Tables
(With and Without Constraints),Inserting/Updating/Deleting Records in a Table, Saving
(Commit) and Undoing (rollback)
• Viewing all Tables in a Database

mysql> SHOW TABLES;


+-------------------+
| Tables_in_aswathy |
+-------------------+
| author |
| book |
| borrow |
| branch |
| student |
+-------------------+
5 rows in set (0.04 sec)
• Inserting records in a table

mysql> insert INTO


BRANCH(BRID,BNAME,HOD)VALUES(10,'BCA','SANTHOSH');
Query OK, 1 row affected (0.12 sec)

mysql> insert INTO BRANCH(BRID,BNAME,HOD)VALUES(20,'BBA','RASHMI');


Query OK, 1 row affected (0.18 sec)

mysql> insert INTO


BRANCH(BRID,BNAME,HOD)VALUES(30,'BCOM','RAMESH');
Query OK, 1 row affected (0.10 sec)

mysql> insert INTO BRANCH(BRID,BNAME,HOD)VALUES(40,'BSC','ADITYA');


Query OK, 1 row affected (0.14 sec)

mysql> insert INTO BRANCH(BRID,BNAME,HOD)VALUES(50,'BA','ASHA');


Query OK, 1 row affected (0.21 sec)

Database Management Systems lab 43


Acharya Institute of Graduate Studies Department of Computer Application

mysql> SELECT *FROM BRANCH;


+------+-------+----------+
| BRID | BNAME | HOD |
+------+-------+----------+
| 10 | BCA | SANTHOSH |
| 20 | BBA | RASHMI |
| 30 | BCOM | RAMESH |
| 40 | BSC | ADITYA |
| 50 | BA | ASHA |
+------+-------+----------+
5 rows in set (0.00 sec)
mysql> INSERT INTO
STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202201','ANURADH
A','JAYANAGAR',10,'IISEM');
Query OK, 1 row affected (0.14 sec)

mysql> INSERT INTO


STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202202','MANJULA',
'BASAVANGUDI',10,'IISEM');
Query OK, 1 row affected (0.18 sec)

mysql> INSERT INTO


STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202203','LAKSHMI','
BASAVANGUDI',10,'IVSEM');
Query OK, 1 row affected (0.16 sec)

mysql> INSERT INTO


STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202204','RENUKA','
HANUMANTANAGAR',20,'IVSEM');
Query OK, 1 row affected (0.14 sec)

mysql> INSERT INTO


STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202205','ARUN','JPN
AGAR',30,'IISEM');
Query OK, 1 row affected (0.36 sec)

mysql> INSERT INTO


STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202206','ABHI','GIRI
NAGAR',40,'IISEM');

Database Management Systems lab 44


Acharya Institute of Graduate Studies Department of Computer Application

Query OK, 1 row affected (0.15 sec)

mysql> INSERT INTO


STUDENT(USN,NAME,ADDRESS,BRID,SEM)VALUES('SCAS202207','DEEPTHI','
GIRINAGAR',50,'IISEM');
Query OK, 1 row affected (0.11 sec)
mysql> SELECT *FROM STUDENT;
+------------+----------+----------------+------+-------+
| USN | NAME | ADDRESS | BRID | SEM |
+------------+----------+----------------+------+-------+
| SCAS202201 | ANURADHA | JAYANAGAR | 10 | IISEM |
| SCAS202202 | MANJULA | BASAVANGUDI | 10 | IISEM |
| SCAS202203 | LAKSHMI | BASAVANGUDI | 10 | IVSEM |
| SCAS202204 | RENUKA | HANUMANTANAGAR | 20 | IVSEM |
| SCAS202205 | ARUN | JPNAGAR | 30 | IISEM |
| SCAS202206 | ABHI | GIRINAGAR | 40 | IISEM |
| SCAS202207 | DEEPTHI | GIRINAGAR | 50 | IISEM |
+------------+----------+----------------+------+-------+
7 rows in set (0.00 sec)
mysql> insert into
AUTHOR(AID,ANAME,COUNTRY,AGE)VALUES('123','ARUNA','INDIA',36);
Query OK, 1 row affected (0.22 sec)

mysql> insert into


AUTHOR(AID,ANAME,COUNTRY,AGE)VALUES('143','SUMA','INDIA',38);
Query OK, 1 row affected (0.20 sec)
mysql> insert into
AUTHOR(AID,ANAME,COUNTRY,AGE)VALUES('144','SANGEETHA','INDIA',42);
Query OK, 1 row affected (0.14 sec)
mysql> insert into
AUTHOR(AID,ANAME,COUNTRY,AGE)VALUES('145','DILEEP','INDIA',39);
Query OK, 1 row affected (0.14 sec)
mysql> insert into
AUTHOR(AID,ANAME,COUNTRY,AGE)VALUES('155','SKHEKARP','INDIA',44);
Query OK, 1 row affected (0.18 sec)

mysql> SELECT *FROM AUTHOR;

Database Management Systems lab 45


Acharya Institute of Graduate Studies Department of Computer Application

+-----+-----------+---------+------+
| AID | ANAME | COUNTRY | AGE |
+-----+-----------+---------+------+
| 123 | ARUNA | INDIA | 36 |
| 143 | SUMA | INDIA | 38 |
| 144 | SANGEETHA | INDIA | 42 |
| 145 | DILEEP | INDIA | 39 |
| 155 | SKHEKARP | INDIA | 44 |
+-----+-----------+---------+------+
5 rows in set (0.00 sec)
mysql> INSERT INTO
BOOK(BKID,BNAME,AID,PUBLISHER,BRID)VALUES('NEPDBMS','DBMS',123,'S
KYWARD',10);
Query OK, 1 row affected (0.14 sec)

mysql> INSERT INTO


BOOK(BKID,BNAME,AID,PUBLISHER,BRID)VALUES('NEPSE','SE',143,'SKYWA
RD',10);
Query OK, 1 row affected (0.14 sec)
mysql> INSERT INTO
BOOK(BKID,BNAME,AID,PUBLISHER,BRID)VALUES('NEPJAVA','JAVA',144,'O
XFORD',20);
Query OK, 1 row affected (0.09 sec)

mysql> INSERT INTO


BOOK(BKID,BNAME,AID,PUBLISHER,BRID)VALUES('NEPMATHS','MATHS',14
5,'OXFORD',20);
Query OK, 1 row affected (0.17 sec)

mysql> SELECT *FROM BOOK;


+----------+-------+------+-----------+------+
| BKID | BNAME | AID | PUBLISHER | BRID |
+----------+-------+------+-----------+------+
| NEPDBMS | DBMS | 123 | SKYWARD | 10 |
| NEPJAVA | JAVA | 144 | OXFORD | 20 |
| NEPMATHS | MATHS | 145 | OXFORD | 20 |
| NEPSE | SE | 143 | SKYWARD | 10 |
+----------+-------+------+-----------+------+
4 rows in set (0.00 sec)

Database Management Systems lab 46


Acharya Institute of Graduate Studies Department of Computer Application

mysql> INSERT INTO


BORROW(USN,BKID,BORROW_DATE)VALUES('SCA2021','NEPDBMS','2008-11-
11');
Query OK, 1 row affected (0.13 sec)

mysql> INSERT INTO


BORROW(USN,BKID,BORROW_DATE)VALUES('SCAS202201','NEPSE','2009-10-
11');
Query OK, 1 row affected (0.14 sec)

mysql> INSERT INTO


BORROW(USN,BKID,BORROW_DATE)VALUES('SCAS202204','NEPMATHS','200
9-10-11');
Query OK, 1 row affected (0.20 sec)

mysql> SELECT *FROM BORROW;


+------------+----------+-------------+
| USN | BKID | BORROW_DATE |
+------------+----------+-------------+
| SCA2021 | NEPDBMS | 2008-11-11 |
| SCAS202201 | NEPSE | 2009-10-11 |
| SCAS202204 | NEPMATHS | 2009-10-11 |
+------------+----------+-------------+
3 rows in set (0.00 sec)
3.uPDATING RECORDS IN A TABLE
mysql> UPDATE BOOK SET PUBLISHER='SKYWARD' WHERE
BKID='NEPJAVA';
Query OK, 1 row affected (0.14 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT *FROM BOOK;


+----------+-------+------+-----------+------+
| BKID | BNAME | AID | PUBLISHER | BRID |
+----------+-------+------+-----------+------+
| NEPDBMS | DBMS | 123 | SKYWARD | 10 |
| NEPJAVA | JAVA | 144 | SKYWARD | 20 |
| NEPMATHS | MATHS | 145 | OXFORD | 20 |
| NEPSE | SE | 143 | SKYWARD | 10 |
+----------+-------+------+-----------+------+

Database Management Systems lab 47


Acharya Institute of Graduate Studies Department of Computer Application

4 rows in set (0.00 sec)4

4.DELETING RECORDS FROM A TABLE


mysql> DELETE FROM BOOK WHERE BKID='NEPSE';
Query OK, 1 row affected (0.39 sec)

mysql> SELECT *FROM BOOK;


+----------+-------+------+-----------+------+
| BKID | BNAME | AID | PUBLISHER | BRID |
+----------+-------+------+-----------+------+
| NEPDBMS | DBMS | 123 | SKYWARD | 10 |
| NEPJAVA | JAVA | 144 | SKYWARD | 20 |
| NEPMATHS | MATHS | 145 | OXFORD | 20 |
+----------+-------+------+-----------+------+
3 rows in set (0.00 sec)
5.PERFORM SAVING
mysql> INSERT INTO
BRANCH(BRID,BNAME,HOD)VALUES(60,'MCA','NARAYAN');
Query OK, 1 row affected (0.23 sec)

mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT *FROM BRANCH;


+------+-------+----------+
| BRID | BNAME | HOD |
+------+-------+----------+
| 10 | BCA | SANTHOSH |
| 20 | BBA | RASHMI |
| 30 | BCOM | RAMESH |
| 40 | BSC | ADITYA |
| 50 | BA | ASHA |
| 60 | MCA | NARAYAN |
+------+-------+----------+
6 rows in set (0.00 sec)
6.PERFORM UNDOING
SAVEPOINT S1;
mysql> SAVEPOINT S1;
Query OK, 0 rows affected (0.00 sec)

Database Management Systems lab 48


Acharya Institute of Graduate Studies Department of Computer Application

mysql> INSERT INTO


BRANCH(BRID,BNAME,HOD)VALUES(70,'ME','PRABHU');
Query OK, 1 row affected (0.16 sec)

mysql> INSERT INTO


BRANCH(BRID,BNAME,HOD)VALUES(80,'BE','AKASH');
Query OK, 1 row affected (0.16 sec)

mysql> ROLL BACK S1;


mysql> SELECT *FROM BRANCH;
+------+-------+----------+
| BRID | BNAME | HOD |
+------+-------+----------+
| 10 | BCA | SANTHOSH |
| 20 | BBA | RASHMI |
| 30 | BCOM | RAMESH |
| 40 | BSC | ADITYA |
| 50 | BA | ASHA |
| 60 | MCA | NARAYAN |
+------+-------+----------+

Database Management Systems lab 49


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -2
OBJECTIVE :
a. List the details of Students who are all studying in 2nd sem BCA.
b. List the students who are not borrowed any books.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
a. List the details of Students who are all studying in 2nd sem BCA.
mysql> SELECT *FROM STUDENT S,BRANCH BR WHERE
S.BRID=BR.BRID AND S.SEM='IISEM' AND BR.BNAME='BCA';

+------------+----------+-------------+------+-------+------+-------+----------+
| USN | NAME | ADDRESS | BRID | SEM | BRID | BNAME | HOD
|
+------------+----------+-------------+------+-------+------+-------+----------+
| SCAS202201 | ANURADHA | JAYANAGAR | 10 | IISEM | 10 | BCA |
SANTHOSH |
| SCAS202202 | MANJULA | BASAVANGUDI | 10 | IISEM | 10 | BCA |
SANTHOSH |
+------------+----------+-------------+------+-------+------+-------+----------+
3 rows in set (0.04 sec)

b. List the students who are not borrowed any books.


mysql> SELECT *FROM STUDENT S WHERE S.USN NOT IN(SELECT
B.USN FROM BORROW B);
+------------+---------+-------------+------+-------+
| USN | NAME | ADDRESS | BRID | SEM |
+------------+---------+-------------+------+-------+
| SCAS202202 | MANJULA | BASAVANGUDI | 10 | IISEM |
| SCAS202203 | LAKSHMI | BASAVANGUDI | 10 | IVSEM |

Database Management Systems lab 50


Acharya Institute of Graduate Studies Department of Computer Application

| SCAS202205 | ARUN | JPNAGAR | 30 | IISEM |


| SCAS202206 | ABHI | GIRINAGAR | 40 | IISEM |
| SCAS202207 | DEEPTHI | GIRINAGAR | 50 | IISEM |
+------------+---------+-------------+------+-------+
4 rows in set (0.08 sec)

Database Management Systems lab 51


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -3
OBJECTIVE :
a. Display the USN, Student name, Branch_name, Book_name, Author_name,
Books_Borrowed_ Date of 2nd sem BCA Students who borrowed books.
b. Display the number of books written by each Author.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
mysql> SELECT
S.USN,S.NAME,S.SEM,BR.BNAME,BK.BKNAME,A.ANANE,B.BORROW_DATE FROM
STUDENT S,BRANCH BR,BOOK BK,AUTHOR A,BORROW B,WHERE S.BRID=BR.BRID
AND S.BRID=BK.BRID AND A.AID=BK.AID AND B.USN=S.USN AND
BK.BKID=B.BKID AND S.SEM='IISEM' AND BR.BNAME='BCA';
b. Display the number of books written by each Author
mysql> SELECT A.ANAME,COUNT(DISTINCT BK.BKID)AS "NO OF
BOOKS" FROM AUTHOR A,BOOK BK WHERE A.AID=BK.AID GROUP BY A.ANAME;

+-----------+-------------+
| ANAME | NO OF BOOKS |
+-----------+-------------+
| ARUNA | 1|
| DILEEP | 1|
| SANGEETHA | 1|
+-----------+-------------+
3 rows in set (0.05 sec)

Database Management Systems lab 52


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -4
OBJECTIVE :
a. Display the student details who borrowed more than two books.
b.Display the student details who borrowed books of more than one Author.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
mysql> SELECT S.NAME FROM STUDENT S,BORROW B WHERE S.USN=B.USN
GROUP BY S.NAME HAVING COUNT(DISTINCT B.BKID)>2;
Empty set (0.00 sec)

b.Display the student details who borrowed books of more than oneAutho
mysql> SELECT S.NAME,COUNT(DISTINCT BK.AID)FROM STUDENT S,BOOK
BK,BORROW B WHERE S.USN=B.USN AND B.BKID=BK.BKID GROUP BY S.NAME
HAVING COUNT(DISTINCT BK.AID)>1;
Empty set (0.00 sec)

Database Management Systems lab 53


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -5
OBJECTIVE :
a. Display the Book names in descending order of their names.
b. List the details of students who borrowed the books which are all published by the same
publisher.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
a. Display the Book names in descending order of their names.
mysql> SELECT *FROM BOOK ORDER BY BNAME DESC;
+----------+-------+------+-----------+------+
| BKID | BNAME | AID | PUBLISHER | BRID |
+----------+-------+------+-----------+------+
| NEPMATHS | MATHS | 145 | OXFORD | 20 |
| NEPJAVA | JAVA | 144 | SKYWARD | 20 |
| NEPDBMS | DBMS | 123 | SKYWARD | 10 |
+----------+-------+------+-----------+------+
3 rows in set (0.00 sec)

b. List the details of students who borrowed the books which are all published by the same
publisher. Consider the following schema: STUDENT (USN, name, date_of_birth, branch,
mark1, mark2, mark3, total, GPA

mysql> SELECT S.NAME,COUNT(BK.PUBLISHER)FROM STUDENT S, BOOK


BK,BORROW B WHERE S.USN=B.USN AND B.BKID=BK.BKID GROUP BY S.NAME
HAVING COUNT(BK.PUBLISHER)>1;

Empty set (0.00 sec)

Database Management Systems lab 54


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -6
OBJECTIVE :
Consider the following schema:
STUDENT (USN, name, date_of_birth, branch, mark1, mark2, mark3, total, GPA)
Perform the following: a. Creating Tables (With and Without Constraints),
Inserting/Updating/Deleting Records in a Table, Saving (Commit) and Undoing (rollback)
.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
a. Creating Tables (With and Without Constraints),Inserting/Updating/Deleting Records in a
Table, Saving (Commit) and Undoing (rollback)

• Creating Tables (With and Without Constraints),

mysql> create table students(usn varchar(10)primary key,name varchar(20)not


null,dob date,branch varchar(20)not null,marki int,mark2 int,mark3 int,total int,gpa
int);
Query OK, 0 rows affected (0.50 sec)

mysql> desc students;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| usn | varchar(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| dob | date | YES | | NULL | |
| branch | varchar(20) | NO | | NULL | |
| marki | int(11) | YES | | NULL | |
| mark2 | int(11) | YES | | NULL | |
| mark3 | int(11) | YES | | NULL | |
| total | int(11) | YES | | NULL | |
| gpa | int(11) | YES | | NULL | |

Database Management Systems lab 55


Acharya Institute of Graduate Studies Department of Computer Application

+--------+-------------+------+-----+---------+-------+
9 rows in set (0.07 sec)
• Inserting records into the table

mysql> insert into


students(usn,name,dob,branch,marki,mark2,mark3,total,gpa)values('sca202201','sanj
ana','2022-04-2','bca',85,96,97,NULL,NULL);
Query OK, 1 row affected (0.19 sec)

mysql> insert into


students(usn,name,dob,branch,marki,mark2,mark3,total,gpa)values('sca202202','ANI
L','2022-04-4','bcOM',85,96,97,NULL,NULL);
Query OK, 1 row affected (0.15 sec)

mysql> insert into


students(usn,name,dob,branch,marki,mark2,mark3,total,gpa)values('sca202203','AK
ASH','2022-04-5','bSC',85,96,97,NULL,NULL);
Query OK, 1 row affected (0.10 sec)
mysql> SELECT *FROM STUDENTS;
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| usn | name | dob | branch | marki | mark2 | mark3 | total |
gpa |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| sca202201 | sanjana | 2022-04-02 | bca | 85 | 96 | 97 | NULL |
NULL |
| sca202202 | ANIL | 2022-04-04 | bcOM | 85 | 96 | 97 |
NULL | NULL |
| sca202203 | AKASH | 2022-04-05 | bSC | 85 | 96 | 97 |
NULL | NULL |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
3 rows in set (0.00 sec)
• INSERTING Records into a table and saving

mysql> insert into


students(usn,name,dob,branch,marki,mark2,mark3,total,gpa)values('sca202204','bhav
ana','2022-04-6','ba',85,96,97,NULL,NULL);

Database Management Systems lab 56


Acharya Institute of Graduate Studies Department of Computer Application

Query OK, 1 row affected (0.17 sec)

mysql> insert into


students(usn,name,dob,branch,marki,mark2,mark3,total,gpa)values('sca202205','ANI
JA','2022-04-7','ba',85,96,97,NULL,NULL);
Query OK, 1 row affected (0.18 sec)

mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
• Deleting records from table and saving

mysql> delete from students where usn='sca202201';


Query OK, 1 row affected (0.18 sec)

mysql> savepoint s1;


Query OK, 0 rows affected (0.00 sec)

mysql> insert into


students(usn,name,dob,branch,marki,mark2,mark3,total,gpa)values('sca202206','ANn
A','2022-04-7','ba',85,96,97,NULL,NULL);
Query OK, 1 row affected (0.16 sec)
mysql> select *from students;
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| usn | name | dob | branch | marki | mark2 | mark3 | total |
gpa |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| sca202202 | ANIL | 2022-04-04 | bcOM | 85 | 96 | 97 |
NULL | NULL |
| sca202203 | AKASH | 2022-04-05 | bSC | 85 | 96 | 97 |
NULL | NULL |
| sca202204 | bhavana | 2022-04-06 | ba | 85 | 96 | 97 | NULL |
NULL |
| sca202205 | ANIJA | 2022-04-07 | ba | 85 | 96 | 97 | NULL |
NULL |
| sca202206 | ANnA | 2022-04-07 | ba | 85 | 96 | 97 | NULL |
NULL |

Database Management Systems lab 57


Acharya Institute of Graduate Studies Department of Computer Application

+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
5 rows in set (0.00 sec)
Updating Records in atable
mysql> update students set total=marki+mark2+mark3;
Query OK, 5 rows affected (0.12 sec)
Rows matched: 5 Changed: 5 Warnings: 0

mysql> select *from students;


+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| usn | name | dob | branch | marki | mark2 | mark3 | total |
gpa |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| sca202202 | ANIL | 2022-04-04 | bcOM | 85 | 96 | 97 | 278 |
NULL |
| sca202203 | AKASH | 2022-04-05 | bSC | 85 | 96 | 97 | 278
| NULL |
| sca202204 | bhavana | 2022-04-06 | ba | 85 | 96 | 97 | 278 |
NULL |
| sca202205 | ANIJA | 2022-04-07 | ba | 85 | 96 | 97 | 278 |
NULL |
| sca202206 | ANnA | 2022-04-07 | ba | 85 | 96 | 97 | 278 |
NULL |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
5 rows in set (0.00 sec)

Database Management Systems lab 58


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -7
OBJECTIVE :
Execute the following queries:
a. Find the GPA score of all the students.
b. Find the students who born on a particular year of birth from the date_of_birth column.
PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:
a. Find the GPA score of all the students.
b. Find the students who born on a particular year of birth from the date_of_birth column.

Mysql> UPDATE STUDENTS SET gpa=((100*TOTAL)/300)/10;


Query OK, 5 rows affected (0.17 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select *from students;
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| usn | name | dob | branch | marki | mark2 | mark3 | total |
gpa |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| sca202202 | ANIL | 2022-04-04 | bcOM | 85 | 96 | 97 | 278 |
9|
| sca202203 | AKASH | 2022-04-05 | bSC | 85 | 96 | 97 | 278
| 9|
| sca202204 | bhavana | 2022-04-06 | ba | 85 | 96 | 97 | 278 |
9|
| sca202205 | ANIJA | 2022-04-07 | ba | 85 | 96 | 97 | 278 |
9|
| sca202206 | ANnA | 2022-04-07 | ba | 85 | 96 | 97 | 278 |
9|
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
5 rows in set (0.00 sec)

Database Management Systems lab 59


Acharya Institute of Graduate Studies Department of Computer Application

• mysql> SELECT USN,NAME,BRANCH,BOB,FROM STUDENTS WHERE DOB


LIKE '%_%_06';

+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| usn | name | dob | branch | marki | mark2 | mark3 | total |
gpa |
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
| sca202204 | bhavana | 2022-04-06 | ba | 85 | 96 | 97 | 278 |
9|
+-----------+---------+------------+--------+-------+-------+-------+-------+-
-----+
1 row in set (0.00 sec)

Database Management Systems lab 60


Acharya Institute of Graduate Studies Department of Computer Application

PROGRAM NO -8
OBJECTIVE :
a. List the students who are studying in a particular branch of study.
b. Find the maximum GPA score of the student branch-wise.

PROCEDURE:
1. Create: Open MYAQL, write the query.
2. Execute: Press Enter.

SOURCE CODE:

List the students who are studying in a particular branch of study.

mysql> SELECT USN,NAME,BRANCH,dOB FROM STUDENTS WHERE BRANCH='ba';


+-----------+---------+--------+------------+
| USN | NAME | BRANCH | dOB |
+-----------+---------+--------+------------+
| sca202204 | bhavana | ba | 2022-04-06 |
| sca202205 | ANIJA | ba | 2022-04-07 |
| sca202206 | ANnA | ba | 2022-04-07 |
+-----------+---------+--------+------------+
3 rows in set (0.00 sec)
Find the maximum GPA score of the student branch-wise

mysql> select branch,max(gpa)from students group by branch;


+--------+----------+
| branch | max(gpa) |
+--------+----------+
| bcOM | 9|
| bSC | 9|
| ba | 9|
+--------+----------+
3 rows in set (0.04 sec)

Database Management Systems lab 61

You might also like