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

DBMS Final Code

The document details the creation of MySQL database tables with employee data and performing CRUD operations on the tables. Multiple tables are created and data is inserted, selected, updated and deleted. Indexes are also created and views are used to select specific columns.

Uploaded by

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

DBMS Final Code

The document details the creation of MySQL database tables with employee data and performing CRUD operations on the tables. Multiple tables are created and data is inserted, selected, updated and deleted. Indexes are also created and views are used to select specific columns.

Uploaded by

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

Assignment 2A

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use AAA;


Database changed

mysql> create table Employee(emp_id int,Emp_name varchar(10),DOB date,Salary float,Adddress


varchar(20),dept varchar(10));
Query OK, 0 rows affected (0.15 sec)

mysql> insert into Employee values(01,’Ashwini’,10/06/1997,10000,’Otur’,’Computer’);


Query OK, 1 row affected (0.04 sec)

mysql> select *from Employee;

+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——– +———-+————+——–+———-+———-+
|1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
1 row in set (0.00 sec)

mysql> insert into Employee values(01,’Ashwini’,199/09/12,10000,’Otur’,’Computer’);


Query OK, 1 row affected, 1 warning (0.03 sec)

mysql> select *from Employee;


+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+———-+———-+
|1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
|1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
2 rows in set (0.00 sec)

mysql> create table Emp(emp_id int,Emp_name varchar(10),DOB date,Salary float,Adddress


varchar(20),dept varchar(10));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into Emp values(01,’Ashwini’,1997/02/04,10000,’Otur’,’Computer’);
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> select *from Employee;


+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+———-+———-+
|1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
|1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
2 rows in set (0.00 sec)

mysql> select *from Emp;


+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+———-+———-+
| 1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
1 row in set (0.00 sec)

mysql> create table Empl(emp_id int,Emp_name varchar(10),DOB date,Salary float,Adddress


varchar(20),dept varchar(10));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into Empl values(01,’Ashwini’,1997-02-04,10000,’Otur’,’Computer’);


Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> select *from Empl;


+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+———-+———-+
| 1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
1 row in set (0.00 sec)

mysql> create table Empp(emp_id int,Emp_name varchar(10),DOB date,Salary float,Adddress


varchar(20),dept varchar(10));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into Empp values(01,’Ashwini’,’1997-02-04’,10000,’Otur’,’Computer’);


Query OK, 1 row affected (0.05 sec)

mysql> select *from Empp;


+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+———-+———-+
| 1 | Ashwini | 1997-02-04 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
1 row in set (0.00 sec)

mysql> select *from Empp;


+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+———-+———-+
| 1 | Ashwini | 1997-02-04 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
1 row in set (0.00 sec)

mysql> insert into Empp values(02,’Anuja’,’1995-03-02’,12000,’Narayangoan’,’IT’);


Query OK, 1 row affected (0.05 sec)

mysql> insert into Empp values(03,’Rupali’,’1998-09-06’,8000,’Peth’,’Mechanical’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Empp values(04,’Madhuri’,’1998-03-01’,1000,’Pargoan’,’ETC’);


Query OK, 1 row affected (0.05 sec)

mysql> insert into Empp values(05,’Tejshri’,’1999-05-01’,11000,’Pargoan’,’Civil’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Empp values(06,’Pooja’,’1997-05-07’,12000,’Mumbai’,’IT’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Empp values(07,’Asmita’,’1996-05-01’,20000,’Pune’,’Computer’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Empp values(08,’Komal’,’1996-05-11’,16000,’Thane’,’Finance’);


Query OK, 1 row affected (0.07 sec)
mysql> insert into Empp values(09,’Ashwini’,’1996-01-11’,20000,’Delhi’,’Manager’);
Query OK, 1 row affected (0.04 sec)

mysql> insert into Empp values(10,’Arti’,’1995-01-11’,32000,’Nagpur’,’CEO’);


Query OK, 1 row affected (0.04 sec)

mysql> select *from Empp;


+——–+———-+————+——–+————-+————+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+————-+————+
| 1 | Ashwini | 1997-02-04 | 10000 | Otur | Computer |
| 2 | Anuja | 1995-03-02 | 12000 | Narayangoan | IT |
| 3 | Rupali | 1998-09-06 | 8000 | Peth | Mechanical|
| 4 | Madhuri | 1998-03-01 | 1000 | Pargoan | ETC |
| 5 | Tejshri | 1999-05-01 | 11000 | Pargoan | Civil |
| 6 | Pooja | 1997-05-07 | 12000 | Mumbai | IT |
| 7 | Asmita | 1996-05-01 | 20000 | Pune | Computer |
| 8 | Komal | 1996-05-11 | 16000 | Thane | Finance |
| 9 | Ashwini | 1996-01-11 | 20000 | Delhi | Manager |
| 10 | Arti | 1995-01-11 | 32000 | Nagpur | CEO |
+——–+———-+————+——–+————-+————+
10 rows in set (0.00 sec)

mysql> update Empp set dept=’finance’ where emp_id=1;


Query OK, 1 row affected (0.06 sec)

mysql> update Empp set dept=’sailing’ where emp_id=2;


Query OK, 1 row affected (0.05 sec)

mysql> update Empp set dept=’purch’ where emp_id=3;


Query OK, 1 row affected (0.04 sec)
mysql> update Empp set dept=’manufacture’ where emp_id=4;
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> update Empp set dept=’sailing’ where emp_id=5;


Query OK, 1 row affected (0.04 sec)

mysql> update Empp set dept=’purch’ where emp_id=6;


Query OK, 1 row affected (0.05 sec)

mysql> select *from Empp;


+——–+———-+————+——–+————-+————+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+————-+————+
| 1 | Ashwini | 1997-02-04 | 10000 | Otur | finance |
| 2 | Anuja | 1995-03-02 | 12000 | Narayangoan | sailing |
| 3 | Rupali | 1998-09-06 | 8000 | Peth | purch |
| 4 | Madhuri | 1998-03-01 | 1000 | Pargoan | manufactur |
| 5 | Tejshri | 1999-05-01 | 11000 | Pargoan | sailing |
| 6 | Pooja | 1997-05-07 | 12000 | Mumbai | purch |
| 7 | Asmita | 1996-05-01 | 20000 | Pune | Computer |
| 8 | Komal | 1996-05-11 | 16000 | Thane | Finance |
| 9 | Ashwini | 1996-01-11 | 20000 | Delhi | Manager |
| 10 | Arti | 1995-01-11 | 32000 | Nagpur | CEO |
+——–+———-+————+——–+————-+————+
10 rows in set (0.00 sec)

mysql> update Empp set dept=’manager’ where emp_id=7;


Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select *from Empp;


+——–+———-+————+——–+————-+————+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+————-+————+
| 1 | Ashwini | 1997-02-04 | 10000 | Otur | finance |
| 2 | Anuja | 1995-03-02 | 12000 | Narayangoan | sailing |
| 3 | Rupali | 1998-09-06 | 8000 | Peth | purch |
| 4 | Madhuri | 1998-03-01 | 1000 | Pargoan | manufactur |
| 5 | Tejshri | 1999-05-01 | 11000 | Pargoan | sailing |
| 6 | Pooja | 1997-05-07 | 12000 | Mumbai | purch |
| 7 | Asmita | 1996-05-01 | 20000 | Pune | manager |
| 8 | Komal | 1996-05-11 | 16000 | Thane | Finance |
| 9 | Ashwini | 1996-01-11 | 20000 | Delhi | Manager |
| 10 | Arti | 1995-01-11 | 32000 | Nagpur | CEO |
+——–+———-+————+——–+————-+————+
10 rows in set (0.00 sec)

mysql> create view view1 as select emp_id, Emp_name from Empp;


Query OK, 0 rows affected (0.04 sec)

mysql> select *from view1;


+——–+———-+
| emp_id | Emp_name |
+——–+———-+
| 1 | Ashwini |
| 2 | Anuja |
| 3 | Rupali |
| 4 | Madhuri |
| 5 | Tejshri |
| 6 | Pooja |
| 7 | Asmita |
| 8 | Komal |
| 9 | Ashwini |
| 10 | Arti |
+——–+———-+
10 rows in set (0.00 sec)

mysql> drop view view1;


Query OK, 0 rows affected (0.00 sec)

mysql> create index index1 ON Empp(emp_id,Emp_name);


Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show indexes from Empp from AAA;


+——-+————+———-+————–+————-+———–+————-+———-+——–
+——+————+———+—————+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality |
Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+——-+————+———-+————–+————-+———–+————-+———-+——–
+——+————+———+—————+
| Empp | 1 | index1 | 1 | emp_id | A | 10 | NULL | NULL | YES | BTREE | | |
| Empp | 1 | index1 | 2 | Emp_name | A | 10 | NULL | NULL | YES | BTREE | | |
+——-+————+———-+————–+————-+———–+————-+———-+——–
+——+————+———+—————+
2 rows in set (0.07 sec)

mysql> drop index index1 On Empp;;


Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show indexes from Empp from AAA;


Empty set (0.00 sec)

mysql> alter table Empp change emp_id emp_id int(11) primary key;
Query OK, 10 rows affected (0.24 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql> alter table Empp change emp_id emp_id int(11) auto_increment;


Query OK, 10 rows affected (0.19 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql> insert into Empp values(emp_id,’Ashwini’,’1996-01-11’,20000,’Delhi’,’Manager’);


Query OK, 1 row affected (0.08 sec)
mysql> select *from Empp;
+——–+———-+————+——–+————-+————+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——–+———-+————+——–+————-+————+
| 1 | Ashwini | 1997-02-04 | 10000 | Otur | finance |
| 2 | Anuja | 1995-03-02 | 12000 | Narayangoan | sailing |
| 3 | Rupali | 1998-09-06 | 8000 | Peth | purch |
| 4 | Madhuri | 1998-03-01 | 1000 | Pargoan | manufactur |
| 5 | Tejshri | 1999-05-01 | 11000 | Pargoan | sailing |
| 6 | Pooja | 1997-05-07 | 12000 | Mumbai | purch |
| 7 | Asmita | 1996-05-01 | 20000 | Pune | manager |
| 8 | Komal | 1996-05-11 | 16000 | Thane | Finance |
| 9 | Ashwini | 1996-01-11 | 20000 | Delhi | Manager |
| 10 | Arti | 1995-01-11 | 32000 | Nagpur | CEO |
| 11 | Ashwini | 1996-01-11 | 20000 | Delhi | Manager |
+——–+———-+————+——–+————-+————+
11 rows in set (0.00 sec)

mysql> truncate table Empp;


Query OK, 0 rows affected (0.05 sec)
mysql> select *from Empp;
Empty set (0.00 sec)

mysql> show tables;


+—————+
| Tables_in_AAA |
+—————+
| Emp |
| Empl |
| Employee |
| Empp |
+—————+
4 rows in set (0.00 sec)

mysql> drop table Empp;


Query OK, 0 rows affected (0.04 sec)

mysql> show tables;


+—————+
| Tables_in_AAA |
+—————+
| Emp |
| Empl |
| Employee |
+—————+
3 rows in set (0.00 sec)
Assignment 2B

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database AAA;


Query OK, 1 row affected (0.00 sec)

mysql> use AA
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> insert into emp values(1,’Anuja’,’testing’,12000,’1997-03-08’,’Mumbai’);


Query OK, 1 row affected (0.04 sec)
mysql> select *from emp;

+——–+———-+———-+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+———-+————+————+———+
| 2 | Anuja | testing | 12000 | 1997-03-08 | Mumbai |
+——–+———-+———-+————+————+———+
1 row in set (0.00 sec)

mysql> insert into emp values(1,’Ashwini’,’purch’,15000,’1997-06-10’,’Pune’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into emp values(3,’Pooja’,’manufacturing’,10000,’1997-07-1’,’Delhi’);


Query OK, 1 row affected, 1 warning (0.04 sec)

mysql> insert into emp values(4,’Madhuri’,’purch’,10000,’1992-07-1’,’kolkata’);


Query OK, 1 row affected (0.03 sec)
mysql> insert into emp values(5,’Rupali’,’sailing’,10000,’1992-05-2’,’nagpur’);
Query OK, 1 row affected (0.04 sec)

mysql> select *from emp;


+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
| 2 | Anuja | testing | 12000 | 1997-03-08 | Mumbai |
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
| 5 | Rupali | sailing | 10000 | 1992-05-02 | nagpur |
+——–+———-+————+————+————+———+
5 rows in set (0.00 sec)

mysql> select *from emp where emp_id AND emp_name;


Empty set, 5 warnings (0.00 sec)

mysql> select *from emp;


+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
| 2 | Anuja | testing | 12000 | 1997-03-08 | Mumbai |
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
| 5 | Rupali | sailing | 10000 | 1992-05-02 | nagpur |
+——–+———-+————+————+————+———+
5 rows in set (0.00 sec)
mysql> select *from emp where emp_id=1 AND emp_name=’Madhuri’;
Empty set (0.00 sec)

mysql> select *from emp where emp_id=1 AND emp_name=’Ashwini’;


+——–+———-+———-+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+———-+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
+——–+———-+———-+————+————+———+
1 row in set (0.00 sec)

mysql> select *from emp;


+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
| 2 | Anuja | testing | 12000 | 1997-03-08 | Mumbai |
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
| 5 | Rupali | sailing | 10000 | 1992-05-02 | nagpur |
+——–+———-+————+————+————+———+
5 rows in set (0.00 sec)

mysql> select *from emp where emp_id=1 OR emp_name=’Ashwini’;


+——–+———-+———-+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+———-+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
+——–+———-+———-+————+————+———+
1 row in set (0.00 sec)

mysql> select *from emp;


+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
| 2 | Anuja | testing | 12000 | 1997-03-08 | Mumbai |
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
| 5 | Rupali | sailing | 10000 | 1992-05-02 | nagpur |
+——–+———-+————+————+————+———+
5 rows in set (0.00 sec)

mysql> select *from emp where emp_add like ’p’;

+——–+———-+———-+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+———-+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
+——–+———-+———-+————+————+———+
1 row in set (0.00 sec)

mysql> select *from emp where emp_salary between 5000 AND 10000;
+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
| 5 | Rupali | sailing | 10000 | 1992-05-02 | nagpur |
+——–+———-+————+————+————+———+
3 rows in set (0.00 sec)

mysql> select *from emp where emp_id IN(1,3,4);


+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
+——–+———-+————+————+————+———+
3 rows in set (0.00 sec)

mysql> create table Student(name varchar(10),rollno int,adress varchar(10));


Query OK, 0 rows affected (0.06 sec)

mysql> create table Stud(name varchar(10),rollno int,adress varchar(10));


Query OK, 0 rows affected (0.06 sec)

mysql> create table Stud(name varchar(10),rollno int,adress varchar(10));


Query OK, 0 rows affected (0.06 sec)

mysql> insert into Stud values(’Ashwini’,1,’Pune’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Stud values(’Rupali’,2,’nagpur’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Stud values(’Anuja’,3,’mumbai’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into Stud values(’Pooja’,4,’delhi’);


Query OK, 1 row affected (0.04 sec)

mysql> select *from Stud;


+———+——–+——–+
| name | rollno | adress |
+———+——–+——–+
| Ashwini | 1 | Pune |
| Rupali | 2 | nagpur |
| Anuja | 3 | mumbai |
| Pooja | 4 | delhi |
+———+——–+——–+
4 rows in set (0.00 sec)

mysql> create table stud(name varchar(10),rollno int,adress varchar(10));


Query OK, 0 rows affected (0.06 sec)

mysql> insert into stud values(’Ashwini’,1,’Pune’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into stud values(’Rupali’,2,’nagpur’);


Query OK, 1 row affected (0.05 sec)

mysql> insert into stud values(’Anuja’,3,’mumbai’);


Query OK, 1 row affected (0.03 sec)

mysql> insert into stud values(’Pooja’,4,’delhi’);


Query OK, 1 row affected (0.04 sec)
mysql> select name from stud union select emp_name from emp;
+———+
| name |
+———+
| Ashwini |
| Rupali |
| Anuja |
| Pooja |
| Madhuri |
+———+
5 rows in set (0.00 sec)

mysql> select name from stud union all select emp_name from emp;
+———+
| name |
+———+
| Ashwini |
| Rupali |
| Anuja |
| Pooja |
| Ashwini |
| Anuja |
| Pooja |
| Madhuri |
| Rupali |
+———+
9 rows in set (0.00 sec)

mysql> select *from emp;


+——–+———-+————+————+————+———+
| emp_id | emp_name | emp_dept | emp_salary | emp_DOB | emp_add |
+——–+———-+————+————+————+———+
| 1 | Ashwini | purch | 15000 | 1997-06-10 | Pune |
| 2 | Anuja | testing | 12000 | 1997-03-08 | Mumbai |
| 3 | Pooja | manufactur | 10000 | 1997-07-01 | Delhi |
| 4 | Madhuri | purch | 10000 | 1992-07-01 | kolkata |
| 5 | Rupali | sailing | 10000 | 1992-05-02 | nagpur |
+——–+———-+————+————+————+———+
5 rows in set (0.00 sec)

mysql> select *from stud;


+———+——–+——–+
| name | rollno | adress |
+———+——–+——–+
| Ashwini | 1 | Pune |
| Rupali | 2 | nagpur |
| Anuja | 3 | mumbai |
| Pooja | 4 | delhi |
+———+——–+——–+
4 rows in set (0.00 sec)

mysql> select count(emp_id) from emp;


+—————+
| count(emp_id) |
+—————+
|5|
+—————+
1 row in set (0.00 sec)

mysql> select sum(emp_salary) from emp;


+—————–+
| sum(emp_salary) |
+—————–+
| 57000 |
+—————–+
1 row in set (0.01 sec)

mysql> select avg(emp_salary) from emp;


+—————–+
| avg(emp_salary) |
+—————–+
| 11400.0000 |
+—————–+
1 row in set (0.00 sec)

mysql> select min(emp_salary) from emp;


+—————–+
| min(emp_salary) |
+—————–+
| 10000 |
+—————–+
1 row in set (0.00 sec)

mysql> select max(emp_salary) from emp;


+—————–+
| max(emp_salary) |
+—————–+
| 15000 |
+—————–+
1 row in set (0.00 sec)
Assignment A3

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use DBMS


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
Mysql>create table product_details(Pro_id int,Pro_name varchar(20),Pro_pricr int);
Query OK, 1 row affected (0.04 sec)

Mysql>insert into product_details values(1,ABC,100);


Query OK, 1 row affected (0.04 sec)

Mysql>insert into product_details values(2,PQR,70);


Query OK, 1 row affected (0.04 sec)
Mysql>insert into product_details values(3,XYZ,80);
Query OK, 1 row affected (0.04 sec)

Mysql>insert into product_details values(4,MNO,90);


Query OK, 1 row affected (0.04 sec)

Mysql>insert into product_details values(5,UVW,110);


Query OK, 1 row affected (0.04 sec)

mysql> select * from product_details;


+——–+———-+———–+
| Pro_id | Pro_name | Pro_price |
+——–+———-+———–+
| 1 | ABC | 100 |
| 2 | PQR | 70 |
| 3 | XYZ | 80 |
| 4 | MNO | 90 |
| 5 | UVW | 110 |
+——–+———-+———–+
5 rows in set (0.00 sec)
mysql> create table sale_details(sale_id int,Pro_id1 int,quantity int,price int,customer_name
varchar(20));
Query OK, 1 row affected (0.04 sec)

Mysql>insert into sale_details values(11,1,100,101,Ashwini);


Query OK, 1 row affected (0.04 sec)

Mysql>insert into sale_details values(12,21,50,111,Anuja);


Query OK, 1 row affected (0.04 sec)

Mysql>insert into sale_details values(13,3,64,234,Madhuri);


Query OK, 1 row affected (0.04 sec)

Mysql>insert into sale_details values(14,21,64,533,Tejashri);


Query OK, 1 row affected (0.04 sec)

Mysql>insert into sale_details values(15,5,76,53,Arti);


Query OK, 1 row affected (0.04 sec)

mysql> select * from sale_details;


+———+———+———-+——-+—————+
| sale_id | Pro_id1 | quantity | price | customer_name |

+———+———+———-+——-+—————+
| 11 | 1 | 100 | 101 | Ashwini |
| 12 | 11 | 50 | 111 | Anuja |
| 13 | 3 | 64 | 234 | Madhuri |
| 14 | 21 | 64 | 533 | Tejashri |
| 15 | 5 | 76 | 53 | Arti |
+———+———+———-+——-+—————+
5 rows in set (0.00 sec)

mysql> select Pro_id,Pro_price from product_details inner join sale_details where product_-
details.pro_id=sale_details.Pro_id1;
+——–+———–+
| Pro_id | Pro_price |
+——–+———–+
| 1 | 100 |
| 3 | 80 |
| 5 | 110 |
+——–+———–+
3 rows in set (0.00 sec)

mysql> select Pro_id,Pro_price,sale_details.customer_name from product_details inner join


sale_details where product_details.Pro_id=sale_details.Pro_id1;;
+——–+———–+—————+
| Pro_id | Pro_price | customer_name |
+——–+———–+—————+
| 1 | 100 | Ashwini |
| 3 | 80 | Madhuri |
| 5 | 110 | Arti |
+——–+———–+—————+
3 rows in set (0.00 sec)

mysql> select Pro_id,Pro_price,sale_details.customer_name from product_details inner join


sale_details where product_details.Pro_id=sale_details.Pro_id1;
+——–+———–+—————+
| Pro_id | Pro_price | customer_name |
+——–+———–+—————+
| 1 | 100 | Ashwini |
| 3 | 80 | Madhuri |
| 5 | 110 | Arti |
+——–+———–+—————+
3 rows in set (0.00 sec)

mysql> select Pro_id,Pro_price,sale_details.customer_name from product_details right outer


join sale_details on product_details.Pro_id=sale_details.Pro_id1;
+——–+———–+—————+
| Pro_id | Pro_price | customer_name |
+——–+———–+—————+
| 1 | 100 | Ashwini |
| NULL | NULL | Anuja |
| 3 | 80 | Madhuri |
| NULL | NULL | Tejashri |
| 5 | 110 | Arti |
+——–+———–+—————+
5 rows in set (0.04 sec)

mysql> select Pro_id,Pro_price,sale_details.customer_name from product_details left outer


join sale_details on product_details.Pro_id=sale_details.Pro_id1;
+——–+———–+—————+
| Pro_id | Pro_price | customer_name |
+——–+———–+—————+
| 1 | 100 | Ashwini |
| 2 | 70 | NULL |
| 3 | 80 | Madhuri |
| 4 | 90 | NULL |
| 5 | 110 | Arti |
+——–+———–+—————+
5 rows in set (0.00 sec)

mysql> select Pro_id,Pro_price,sale_details.customer_name from product_details left outer


join sale_details on product_details.Pro_id=sale_details.Pro_id1 union select
Pro_id,Pro_price,
sale_details.customer_name from product_details left outer join sale_details on
product_details
.Pro_id=sale_details.Pro_id1;
+——–+———–+—————+
| Pro_id | Pro_price | customer_name |
+——–+———–+—————+
| 1 | 100 | Ashwini |
| 2 | 70 | NULL |
| 3 | 80 | Madhuri |
| 4 | 90 | NULL |
| 5 | 110 | Arti |
+——–+———–+—————+
5 rows in set (0.04 sec)

mysql> select * from product_details left outer join sale_details on product_details.Pro_-


id=sale_details.Pro_id1 union select * from product_details left outer join sale_details on
product_details.Pro_id=sale_details.Pro_id1;
+——–+———-+———–+———+———+———-+——-+—————+
| Pro_id | Pro_name | Pro_price | sale_id | Pro_id1 | quantity | price | customer_name |
+——–+———-+———–+———+———+———-+——-+—————+
| 1 | ABC | 100 | 11 | 1 | 100 | 101 | Ashwini |
| 2 | PQR | 70 | NULL | NULL| NULL | NULL | NULL |

| 3 | XYZ | 80 | 13 | 3 | 64 | 234 | Madhuri |


| 4 | MNO | 90 | NULL | NULL | NULL | NULL | NULL |
| 5 | UVW | 110 | 15 | 5 | 76 | 53 | Arti |
+——–+———-+———–+———+———+———-+——-+—————+
5 rows in set (0.00 sec)

mysql> select *from product_details Pro_id=(select Pro_id from product_details where Pro_-
name=’ABC’);

mysql> select *from product_details where Pro_id=(select Pro_id from product_details


where
Pro_name=’ABC’);
+——–+———-+———–+
| Pro_id | Pro_name | Pro_price |
+——–+———-+———–+
| 1 | ABC | 100 |
+——–+———-+———–+
1 row in set (0.02 sec)
Assignment A4

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use AAA


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> create table borrower(Roll_no int,Name varchar(10),dateofissue date,bookname


varchar(
20),status varchar(10));
Query OK, 0 rows affected (0.08 sec)
mysql> insert into borrower values(01,’Ashwini’,’2017-2-3’,’java’,’I’);
Query OK, 1 row affected (0.04 sec)

mysql> insert into borrower values(02,’Anuja’,’2017-3-23’,’DBMS’,’I’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into borrower values(03,’Rupali’,’2017-8-23’,’CN’,’I’);


Query OK, 1 row affected (0.03 sec)

mysql> insert into borrower values(04,’Komal’,’2017-7-2’,’ISEE’,’I’);


Query OK, 1 row affected (0.04 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
+———+———+————-+———-+——–+
4 rows in set (0.00 sec)

mysql> create table fine(Roll_no int,curdate date,fine int);


Query OK, 0 rows affected (0.10 sec)

mysql> delimiter
mysql> Create procedure AA2(In rno1 int(3),name1 varchar(30))
begin
Declare i_date date;
Declare diff int;
select dateofissue into i_date from borrower where Roll_no=rno1;
select datediff(curdate(),i_date)into diff;
select diff;
End;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call AA2(1,’java’);
+——+
| diff |
+——+
| 202 |
+——+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter
mysql> Create procedure SS2(In rno1 int(3),name1 varchar(30))
-> begin
-> Declare i_date date;
-> Declare diff int;
-> select dateofissue into i_date from borrower where Roll_no=rno1 and bookname=name1;
-> select datediff(curdate(), i_date) into diff;
-> select diff;
-> If diff>15 then
-> Update Borrower
-> set status=’R’
-> where Roll_no=rno1 and NameofBook=name1;
-> End if;
-> End;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call ss2(4,’ISEE’);
+——+
| diff |
+——+
| 53 |
+——+
1 row in set (0.01 sec)
mysql> delimiter
mysql> Create procedure RRR3(In rno1 int(3),name1 varchar(30))
-> begin
-> Declare i_date date;
-> Declare diff int;
-> Declare fine_amt int;
-> select dateofissue into i_date from borrower where Roll_no=rno1 and bookname=name1;
-> select datediff(curdate(),i_date)into diff;
-> select diff;
-> If (diff>=15 and diff<=30)then
-> SET fine_amt=diff*5;
-> insert into Fine values(rno1,CURDATE(),fine_amt);
-> Update Borrower set status=’R’ where Roll_no=rno1 and NameofBook=name1;
-> End if;
-> End;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call RRR3(1,’java’);

+——+
| diff |
+——+
| 202 |
+——+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter
mysql> Create procedure RR3(In rno1 int(3),name1 varchar(30))
-> begin
-> Declare i_date date;
-> Declare diff int;
-> Declare fine_amt int;
-> select dateofissue into i_date from borrower where Roll_no=rno1 and bookname=name1;
-> select datediff(curdate(),i_date)into diff;
-> select diff;
-> If (diff>=15 and diff<=30)then
-> SET fine_amt=diff*5;
-> insert into Fine values(rno1,CURDATE(),fine_amt);
-> Update Borrower set status=’R’ where Roll_no=rno1 and NameofBook=name1;
-> End if;
-> End;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call RR3(4,’ISEE’);
+——+
| diff |
+——+
| 53 |
+——+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
+———+———+————-+———-+——–+
4 rows in set (0.00 sec)

mysql> insert into borrower values(05,’Vishal’,’2017-08-26’,’C’,’I’);


Query OK, 1 row affected (0.04 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
| 5 | Vishal | 2017-08-26 | C | I |
+———+———+————-+———-+——–+
5 rows in set (0.00 sec)

mysql> call RR3(5,’C’);


+——+
| diff |
+——+
| -2 |
+——+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
| 5 | Vishal | 2017-08-26 | C | I |
+———+———+————-+———-+——–+
5 rows in set (0.00 sec)

mysql> insert into borrower values(06,’Pooja’,’2017-08-16’,’C++’,’I’);


Query OK, 1 row affected (0.02 sec)
mysql> call RR3(6,’C++’);
+——+
| diff |
+——+
|8|
+——+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select * from borrower;
+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
| 5 | Vishal | 2017-08-26 | C | I |
| 6 | Pooja | 2017-08-16 | C++ | I |
+———+———+————-+———-+——–+
6 rows in set (0.00 sec)

mysql> insert into borrower values(07,’Pooja’,’2017-08-08’,’C++’,’I’);


Query OK, 1 row affected (0.05 sec)

mysql> call RR3(7,’C++’);


+——+
| diff |
+——+
| 16 |
+——+
1 row in set (0.00 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
| 5 | Vishal | 2017-08-26 | C | I |
| 6 | Pooja | 2017-08-16 | C++ | I |
| 7 | Pooja | 2017-08-08 | C++ | I |
+———+———+————-+———-+——–+
7 rows in set (0.00 sec)
mysql> delimiter
mysql> Create procedure R3(In rno1 int(3),name1 varchar(30))
-> begin
-> Declare i_date date;
-> Declare diff int;
-> Declare fine_amt int;
-> select dateofissue into i_date from borrower where Roll_no=rno1 and bookname=name1;
-> select datediff(curdate(),i_date)into diff;
-> select diff;
-> If (diff>=15 and diff<=30)then
-> SET fine_amt=diff*5;
-> insert into fine values(rno1,curdate(),fine_amt);
-> Update borrower set status=’R’ where Roll_no=rno1 and bookname=name1;
-> End if;
-> End;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call R3(7,’C++’);
+——+
| diff |
+——+
| 16 |
+——+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.10 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | I |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
| 5 | Vishal | 2017-08-26 | C | I |
| 6 | Pooja | 2017-08-16 | C++ | I |
| 7 | Pooja | 2017-08-08 | C++ | R |
+———+———+————-+———-+——–+
7 rows in set (0.00 sec)

mysql> delimiter
mysql> Create procedure III3(In rno1 int(3),name1 varchar(30))
-> begin
-> Declare i_date date;
-> Declare diff int;
-> Declare fine_amt int;
-> select dateofissue into i_date from borrower where Roll_no=rno1 and bookname=name1;
-> select datediff(curdate(), i_date) into diff;
-> select diff;
-> If (diff>=15 and diff<=30)then
-> SET fine_amt=diff*5;
-> insert into fine values(rno1,curdate(),fine_amt);
-> elseif (diff>30) then
-> SET fine_amt=diff*50;
-> insert into fine values(rno1,curdate(),fine_amt);
-> End if;
-> Update borrower set status=’R’ where Roll_no=rno1 and bookname=name1;
-> End;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call III3(2,’DBMS’);
+——+
| diff |
+——+
| 154 |
+——+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.07 sec)

mysql> select * from borrower;


+———+———+————-+———-+——–+
| Roll_no | Name | dateofissue | bookname | status |
+———+———+————-+———-+——–+
| 1 | Ashwini | 2017-02-03 | java | I |
| 2 | Anuja | 2017-03-23 | DBMS | R |
| 3 | Rupali | 2017-08-23 | CN | I |
| 4 | Komal | 2017-07-02 | ISEE | I |
| 5 | Vishal | 2017-08-26 | C | I |
| 6 | Pooja | 2017-08-16 | C++ | I |
| 7 | Pooja | 2017-08-08 | C++ | R |
+———+———+————-+———-+——–+
7 rows in set (0.00 sec)

mysql> select * from fine;


+———+————+——+
| Roll_no | curdate | fine |
+———+————+——+
| 7 | 2017-08-24 | 80 |
| 2 | 2017-08-24 | 7700 |
+———+————+——+
2 rows in set (0.00 sec)
Assignment A5

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database qq;


Query OK, 1 row affected (0.16 sec)

mysql> use qq;


Database changed

mysql> create table stud_marks(roll_no int(3) primary key,name varchar(20),total_marks


int(10));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into stud_marks values (1,"ashu",1100);


Query OK, 0 rows affected (0.00 sec)

mysql> insert into stud_marks values (2,"ram",902);


Query OK, 0 rows affected (0.00 sec)

mysql> insert into stud_marks values (3,"Sham",826);


Query OK, 0 rows affected (0.00 sec)

mysql> create table result (rno int(3),name varchar(20),class varchar(30));


Query OK, 0 rows affected (0.00 sec)

mysql> delimiter //
mysql> Create procedure Fgrade32(rno1 int)
Begin
-> Declare grade varchar(20);
-> Declare mark1 int(10);
-> Declare name1 varchar(50);
-> Select total_marks,name into mark1,name1 from stud_marks where roll_no=rno1;
-> If (mark1>=990 and mark1<=1500)then
-> Set grade=’Distinction’;
-> insert into result values(rno1,name1,grade);
-> elseif (mark1>=900 and mark1<=989) then
-> Set grade=’First Class’;
-> insert into result values(rno1,name1,grade);
-> elseif (mark1>=825 and mark1<=899) then
-> Set grade=’Higher Second Class’;
-> insert into result values(rno1,name1,grade);
-> End if;
-> select grade;
-> End;
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call Fgrade(1);


-> //
+————-+
| grade |
+————-+
| Distinction |
+————-+
1 row in set (0.03 sec)
Query OK, 0 rows affected (0.03 sec)

mysql> select * from result;


-> //

+——+——+————-+
| rno | name | class |
+——+——+————-+
| 1 | ashu | Distinction |
+——+——+————-+
1 row in set (0.00 sec)

mysql> delimiter //
mysql> Create procedure Fgrade32(rno1 int)
-> Begin
-> Declare grade varchar(20);
-> Declare mark1 int(10);
-> Declare name1 varchar(50);
-> Declare Continue Handler for sqlwarning set grade=’NOT FOUND’;
-> Select total_marks,name into mark1,name1 from stud_marks where roll_no=rno1;
-> If (mark1>=990 and mark1<=1500)then
-> Set grade=’Distinction’;
-> insert into result values(rno1,name1,grade);
-> elseif (mark1>=900 and mark1<=989) then
-> Set grade=’First Class’;
-> insert into result values(rno1,name1,grade);
-> elseif (mark1>=825 and mark1<=899) then
-> Set grade=’Higher Second Class’;
-> insert into result values(rno1,name1,grade);
-> End if;
-> select grade;
-> End;
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call Fgrade32(3);


-> //
+————-+
| grade |
+————-+
| Distinction |
+————-+
1 row in set (0.05 sec)
Query OK, 0 rows affected (0.05 sec)

mysql> select * from result;


-> //
+——+——-+————-+
| rno | name | class |
+——+——-+————-+
| 1 | ashu | Distinction |
| 3 | Pooja | Distinction |
+——+——-+————-+
2 rows in set (0.00 sec)

mysql> call Fgrade32(4);


-> //
+———–+
| grade |
+———–+
| NOT FOUND |
+———–+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> call Fgrade32(2);


-> //
+————-+
| grade |
+————-+
| Distinction |
+————-+
1 row in set (0.05 sec)
Query OK, 0 rows affected (0.05 sec)

mysql> select * from result;


-> //
+——+——-+————-+
| rno | name | class |
+——+——-+————-+
| 1 | ashu | Distinction |
| 3 | Pooja | Distinction |
| 2 | ram | Distinction |
+——+——-+————-+
3 rows in set (0.00 sec)

mysql> call Fgrade32(10);


-> //
+———–+
| grade |
+———–+
| NOT FOUND |
+———–+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> delimiter //
mysql> Create function Fgrade4(rno1 int) returns varchar(20)
-> DETERMINISTIC
-> Begin
-> Declare grade varchar(20);
-> Declare mark1 int(10);
-> Declare name1 varchar(50);
-> Declare Continue Handler for sqlwarning set grade=’NOT FOUND’;
-> Select total_marks,name into mark1,name1 from stud_marks where roll_no=rno1;
-> If (mark1>=990 and mark1<=1500)then
-> Set grade=’Distinction’;
-> insert into result values(rno1,name1,grade);
-> elseif (mark1>=900 and mark1<=989) then
-> Set grade=’First Class’;
-> insert into result values(rno1,name1,grade);
-> elseif (mark1>=825 and mark1<=899) then
-> Set grade=’Higher Second Class’;
-> insert into result values(rno1,name1,grade);
-> End if;
-> Return grade;
-> End;
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> select Fgrade4(6);


-> //
+————+
| Fgrade4(6) |
+————+
| NOT FOUND |
+————+
1 row in set, 1 warning (0.00 sec)

mysql> select Fgrade4(1);


-> //
+————-+
| Fgrade4(1) |
+————-+
| Distinction |
+————-+
1 row in set (0.03 sec)
Assignment A6

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use AB;


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> create table O_rollcall (rno int(3) primary key, name varchar(20),addr varchar(30));
Query OK, 0 rows affected (0.18 sec)

mysql> insert into O_rollcall values(1,’Ashwini’,’Nashik’);


Query OK, 1 row affected (0.07 sec)

mysql> insert into O_rollcall values(2,’Anuja’,’Pune’);


Query OK, 1 row affected (0.04 sec)

mysql> insert into O_rollcall values(3,’Pooja’,’Manmad’);


Query OK, 1 row affected (0.04 sec)

mysql> select * from O_rollcall;


+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)
mysql> show tables;
-> //
+————–+
| Tables_in_AB |
+————–+
| N_rollcall |
| O_rollcall |
| borrower |
| fine |
| n_rollcall |
| stud |
+————–+
6 rows in set (0.00 sec)

mysql> insert into n_rollcall select * from O_rollcall;


-> //
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from n_rollcall;


-> //
+——+———+——–+
| rno | name | addr |
+——+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+——+———+——–+
3 rows in set (0.00 sec)

mysql> delete from n_rollcall;


-> //
Query OK, 3 rows affected (0.05 sec)
mysql> select * from n_rollcall;
-> //
Empty set (0.00 sec)

mysql> select * from O_rollcall;


-> //
+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)

mysql> delimiter //
mysql> CREATE PROCEDURE newcurr1(IN rno1 int(3))
-> begin
-> DECLARE rno2 int(3);
-> DECLARE exit_loop BOOLEAN;
-> DECLARE c1 CURSOR FOR SELECT rno FROM O_rollcall where rno>rno1;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
-> OPEN c1;
-> emp_loop: LOOP
-> FETCH c1 INTO rno2;
-> If not exists(select * from n_rollcall where rno=rno2) then
-> insert into n_rollcall select * from O_rollcall where rno=rno2;
-> End If;
-> IF exit_loop THEN
-> CLOSE c1;
-> LEAVE emp_loop;
-> END IF;
-> END LOOP emp_loop;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call newcurr1(1);


-> //
Query OK, 0 rows affected, 1 warning (0.07 sec)

mysql> select * from n_rollcall;


-> //

+——+——-+——–+
| rno | name | addr |
+——+——-+——–+
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+——+——-+——–+
2 rows in set (0.00 sec)

mysql> select * from O_rollcall;


-> //
+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)

mysql> delimiter //
mysql> CREATE PROCEDURE newcurr22()
-> begin
-> DECLARE rno1 int(3);
-> DECLARE exit_loop BOOLEAN;
-> DECLARE c1 CURSOR FOR SELECT rno FROM O_rollcall ;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
-> OPEN c1;
-> emp_loop: LOOP
-> FETCH c1 INTO rno1;
-> If not exists(select * from n_rollcall where rno=rno1) then
-> insert into n_rollcall select * from O_rollcall where rno=rno1;
-> End If;
-> IF exit_loop THEN
-> CLOSE c1;
-> LEAVE emp_loop;
-> END IF;
-> END LOOP emp_loop;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call newcurr22();


-> //
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> select * from n_rollcall;


-> //
+——+———+——–+
| rno | name | addr |
+——+———+——–+
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
| 1 | Ashwini | Nashik |
+——+———+——–+
3 rows in set (0.00 sec)

mysql> select * from O_rollcall;


-> //
+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)

mysql> create table n_rollcall(rno int(3),name varchar(20), addr varchar(30));//


Query OK, 0 rows affected (0.13 sec)

mysql> select * from n_rollcall;


-> //
Empty set (0.00 sec)
mysql> insert into n_rollcal select * from O_rollcall;
-> //

mysql> insert into n_rollcall select * from O_rollcall;


-> //
Query OK, 3 rows affected (0.03 sec)

mysql> select * from n_rollcall;


-> //
+——+———+——–+
| rno | name | addr |
+——+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+——+———+——–+
3 rows in set (0.00 sec)

mysql> delete from n_rollcall;


-> //
Query OK, 3 rows affected (0.05 sec)

mysql> select * from n_rollcall;


-> //
Empty set (0.00 sec)

mysql> select * from O_rollcall;


-> //
+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)

mysql> delimiter //
mysql> CREATE PROCEDURE newcurr1(IN rno1 int(3))
-> begin
-> DECLARE rno2 int(3);
-> DECLARE exit_loop BOOLEAN;
-> DECLARE c1 CURSOR FOR SELECT rno FROM O_rollcall where rno>rno1;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
-> OPEN c1;
-> emp_loop: LOOP
-> FETCH c1 INTO rno2;
-> If not exists(select * from n_rollcall where rno=rno2) then
-> insert into n_rollcall select * from O_rollcall where rno=rno2;
-> End If;
-> IF exit_loop THEN
-> CLOSE c1;
-> LEAVE emp_loop;
-> END IF;
-> END LOOP emp_loop;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call newcurr1(1);


-> //
Query OK, 0 rows affected, 1 warning (0.07 sec)

mysql> select * from n_rollcall;


-> //
+——+——-+——–+
| rno | name | addr |
+——+——-+——–+
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+——+——-+——–+
2 rows in set (0.00 sec)

mysql> select * from O_rollcall;


-> //
+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)

mysql> delimiter //
mysql> CREATE PROCEDURE newcurr22()
-> begin
-> DECLARE rno1 int(3);
-> DECLARE exit_loop BOOLEAN;
-> DECLARE c1 CURSOR FOR SELECT rno FROM O_rollcall ;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
-> OPEN c1;
-> emp_loop: LOOP
-> FETCH c1 INTO rno1;
-> If not exists(select * from n_rollcall where rno=rno1) then
-> insert into n_rollcall select * from O_rollcall where rno=rno1;
-> End If;
-> IF exit_loop THEN
-> CLOSE c1;
-> LEAVE emp_loop;
-> END IF;
-> END LOOP emp_loop;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call newcurr22();


-> //
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> select * from n_rollcall;


-> //
+——+———+——–+
| rno | name | addr |
+——+———+——–+
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
| 1 | Ashwini | Nashik |
+——+———+——–+
3 rows in set (0.00 sec)

mysql> select * from O_rollcall;


-> //
+—–+———+——–+
| rno | name | addr |
+—–+———+——–+
| 1 | Ashwini | Nashik |
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+—–+———+——–+
3 rows in set (0.00 sec)
Assignment A7

(base) student@student-OptiPlex-3010:~$ mysql -u root -p Enter password:

Welcome to the MySQL monitor. Commands end with; or \g. Your MySQL connection id is 6 Server
version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be
trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use AB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> create table lib(lno int(10),bname varchar(10),author varchar(20),allow_days int


(10));
Query OK, 0 rows affected (0.07 sec)

mysql> create table lib_audit(lno int(10),old_all_days int (10),new_all_days int(10));


Query OK, 0 rows affected (0.07 sec)

mysql> insert into lib values(1,"java","Ashwini",10);


Query OK, 1 row affected (0.04 sec)

mysql> insert into lib values(2,"VB"," Anuja",15);


Query OK, 1 row affected (0.04 sec)

mysql> insert into lib values(3,"DBMS","Prasad",12);


Query OK, 1 row affected (0.03 sec)

mysql> insert into lib values(4,"C++","vishal",11);


Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM lib;


+——+——-+———+————+
| lno | bname | author | allow_days |
+——+——-+———+————+
| 1 | java | Ashwini | 10 |
| 2 | VB | Anuja | 15 |
| 3 | DBMS | Prasad | 12 |
| 4 | C++ | vishal | 11 |
+——+——-+———+————+
4 rows in set (0.00 sec)

mysql> SELECT * FROM lib_audit;


Empty set (0.00 sec)

mysql> delimiter //
mysql> CREATE TRIGGER t22
-> AFTER UPDATE ON lib
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO lib_audit SET lno = OLD. lno,old_all_days=OLD. allow_days,new_all_-
days=NEW. allow_days;
-> END
-> //
Query OK, 0 rows affected (0.07 sec)

mysql> update lib set allow_days=20 where lno=1;


-> //
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM lib_audit;


-> //
+——+————–+————–+
| lno | old_all_days | new_all_days |
+——+————–+————–+
| 1 | 10 | 20 |
+——+————–+————–+
1 row in set (0.00 sec)

mysql> delimiter //
mysql> CREATE TRIGGER t12
-> BEFORE DELETE ON lib
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO lib_audit SET lno = OLD. lno,old_all_days=OLD. allow_days;
-> END
-> //
Query OK, 0 rows affected (0.06 sec)

mysql> delete from lib where lno=4;


-> //
Query OK, 1 row affected (0.02 sec)
mysql> select * from lib_audit;
-> //
+——+————–+————–+
| lno | old_all_days | new_all_days |
+——+————–+————–+
| 1 | 10 | 20 |
| 4 | 11 | NULL |
+——+————–+————–+
2 rows in set (0.00 sec)
Assignment A8

package dbmsl;
import java.sql.*;
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;
public class MySqlProcedure1
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
c=DriverManager.getConnection("jdbc:mysql://localhost:3306/Marz","root","");
Statement sm=c.createStatement();
int ch;
do
{
System.out.println("Enter Your Choice:\n1.Insert: \n2.Select: \
n3.Update: \n4.Delete: \n5.Exit!!!");
Scanner sc=new Scanner(System.in);
ch= sc.nextInt();
switch(ch)
{
case 1:

String sql="Insert into stud values('Ashwini',20)";


sm.executeUpdate(sql);
System.out.println("Record is inserted successfully!!!");
break;

case 2:

String sql1="Select Name ,Age from stud";


ResultSet rs=sm.executeQuery(sql1);
while(rs.next())
{
String name1=rs.getString("Name");
int age1=rs.getInt("Age");
System.out.println("Name"+name1);
System.out.println("Age"+age1);
}
break;

case 3:

String sql2="update stud set Name=’Pooja’where


Name=”Ashwini";
sm.executeUpdate(sql2);
System.out.println("Record is updated successfully!!!");
break;

case 4:

sql="delete from stud where Age=20 ";


sm.executeUpdate(sql);
System.out.println("Record is deleted successfully!!!");
break;
}
}while(ch<5);
}
catch(Exception e)
{
e.printStackTrace();
}
}

Output:
* Enter Your Choice:
1.Insert:
2.Select:
3.Update:
4.Delete:
5.Exit!!!
1
Record is inserted successfully!!!
Enter Your Choice:
1.Insert:
2.Select:
3.Update:
4.Delete:
5.Exit!!!
2
Name:Ashwini
Age:20
Enter Your Choice:
1.Insert:
2.Select:
3.Update:
4.Delete:
5.Exit!!!
3
Record is updated successfully!!!
Enter Your Choice:
1.Insert:
2.Select:
3.Update:
4.Delete:
5.Exit!!!
4
Record is deleted successfully!!!
Enter Your Choice:
1.Insert:
2.Select:
3.Update:
4.Delete:
5.Exit!!!
Assignment B1

[jcoe@localhost bin]$ su
Password:
[root@localhost bin]# ./mongo
MongoDB shell version: 2.6.12
connecting to: test
> use DBMS
switched to db DBMS

>db.stud.insert([{"Roll_no":01,"Name":"Ashwini","Dept":"Comp","Per":78,"Add":"Otur"},
{"Roll_no":02,"Name":"Anuja","Dept":"IT","Per":70,"Add":"Narayangoan"},
{"Roll_no":03,"Name":"Vishal","Dept":"Mech","Per":81,"Add":"alephata"},
{"Roll_no":04,"Name":"Pooja","Dept":"E&TC","Per":58,"Add":"Pune"},
{"Roll_no":05,"Name":"Nikita","Dept":"Civil","Per":63,"Add":"Mumbai"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 5,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> db.stud.find().pretty()
{
"_id" : ObjectId("596c46b45c2af306687b314e"),
"Roll_no" : 1,
"Name" : "Ashwini",
"Dept" : "Comp",
"Per" : 78,
"Add" : "Otur"
}
{
"_id" : ObjectId("596c46b45c2af306687b314f"),
"Roll_no" : 2,
"Name" : "Anuja",
"Dept" : "IT",
"Per" : 70,
"Add" : "Narayangoan"
}
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Mech",
"Per" : 81,
"Add" : "alephata"
}
{
"_id" : ObjectId("596c46b45c2af306687b3151"),
"Roll_no" : 4,
"Name" : "Pooja",
"Dept" : "E&TC",
"Per" : 58,
"Add" : "Pune"
}
{
"_id" : ObjectId("596c46b45c2af306687b3152"),
"Roll_no" : 5,
"Name" : "Nikita",
"Dept" : "Civil",
"Per" : 63,
"Add" : "Mumbai"
}

> db.stud.update({Roll_no:03},{$set:{Dept:"Comp"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.stud.find().pretty()
{
"_id" : ObjectId("596c46b45c2af306687b314e"),
"Roll_no" : 1,
"Name" : "Ashwini",
"Dept" : "Comp",
"Per" : 78,
"Add" : "Otur"
}
{
"_id" : ObjectId("596c46b45c2af306687b314f"),
"Roll_no" : 2,
"Name" : "Anuja",
"Dept" : "IT",
"Per" : 70,
"Add" : "Narayangoan"
}
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Comp",
"Per" : 81,
"Add" : "alephata"
}
{
"_id" : ObjectId("596c46b45c2af306687b3151"),
"Roll_no" : 4,
"Name" : "Pooja",
"Dept" : "E&TC",
"Per" : 58,
"Add" : "Pune"
}
{
"_id" : ObjectId("596c46b45c2af306687b3152"),
"Roll_no" : 5,
"Name" : "Nikita",
"Dept" : "Civil",
"Per" : 63,
"Add" : "Mumbai"
}

> db.stud.find().limit(4)
{ "_id" : ObjectId("596c46b45c2af306687b314e"), "Roll_no" : 1, "Name" : "Ashwini",
"Dept" : "Comp", "Per" : 78, "Add" : "Otur" }
{ "_id" : ObjectId("596c46b45c2af306687b314f"), "Roll_no" : 2, "Name" : "Anuja", "Dept" :
"IT", "Per" : 70, "Add" : "Narayangoan" }
{ "_id" : ObjectId("596c46b45c2af306687b3150"), "Roll_no" : 3, "Name" : "Vishal",
"Dept" : "Comp", "Per" : 81, "Add" : "alephata" }
{ "_id" : ObjectId("596c46b45c2af306687b3151"), "Roll_no" : 4, "Name" : "Pooja", "Dept" :
"E&TC", "Per" : 58, "Add" : "Pune" }

> db.stud.find().limit(4)pretty()
2017-07-17T10:48:08.738+0530 SyntaxError: Unexpected identifier

> db.stud.find().limit(4).pretty()
{
"_id" : ObjectId("596c46b45c2af306687b314e"),
"Roll_no" : 1,
"Name" : "Ashwini",
"Dept" : "Comp",
"Per" : 78,
"Add" : "Otur"
}
{
"_id" : ObjectId("596c46b45c2af306687b314f"),
"Roll_no" : 2,
"Name" : "Anuja",
"Dept" : "IT",
"Per" : 70,
"Add" : "Narayangoan"
}
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Comp",
"Per" : 81,
"Add" : "alephata"
}
{
"_id" : ObjectId("596c46b45c2af306687b3151"),
"Roll_no" : 4,
"Name" : "Pooja",
"Dept" : "E&TC",
"Per" : 58,
"Add" : "Pune"
}
> db.stud.find().skip(2).pretty()
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Comp",
"Per" : 81,
"Add" : "alephata"
}
{
"_id" : ObjectId("596c46b45c2af306687b3151"),
"Roll_no" : 4,
"Name" : "Pooja",
"Dept" : "E&TC",
"Per" : 58,
"Add" : "Pune"
}
{
"_id" : ObjectId("596c46b45c2af306687b3152"),
"Roll_no" : 5,
"Name" : "Nikita",
"Dept" : "Civil",
"Per" : 63,
"Add" : "Mumbai"
}

> db.stud.find({$and:[{Roll_no:{$gt:02}},{Roll_no:{$lt:05}}]})
{ "_id" : ObjectId("596c46b45c2af306687b3150"), "Roll_no" : 3, "Name" : "Vishal",
"Dept" : "Comp", "Per" : 81, "Add" : "alephata" }
{ "_id" : ObjectId("596c46b45c2af306687b3151"), "Roll_no" : 4, "Name" : "Pooja", "Dept" :
"E&TC", "Per" : 58, "Add" : "Pune" }

> db.stud.find({$and:[{Roll_no:{$gt:02}},{Roll_no:{$lt:05}}]}).pretty()
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Comp",
"Per" : 81,
"Add" : "alephata"
}
{
"_id" : ObjectId("596c46b45c2af306687b3151"),
"Roll_no" : 4,
"Name" : "Pooja",
"Dept" : "E&TC",
"Per" : 58,
"Add" : "Pune"
}

> db.stud.find({Name:{$in:['Vishal']}}).pretty()
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Comp",
"Per" : 81,
"Add" : "alephata"
}

> db.stud.remove({Roll_no:05})
WriteResult({ "nRemoved" : 1 })

> db.stud.find().pretty()
{
"_id" : ObjectId("596c46b45c2af306687b314e"),
"Roll_no" : 1,
"Name" : "Ashwini",
"Dept" : "Comp",
"Per" : 78,
"Add" : "Otur"
}
{
"_id" : ObjectId("596c46b45c2af306687b314f"),
"Roll_no" : 2,
"Name" : "Anuja",
"Dept" : "IT",
"Per" : 70,
"Add" : "Narayangoan"
}
{
"_id" : ObjectId("596c46b45c2af306687b3150"),
"Roll_no" : 3,
"Name" : "Vishal",
"Dept" : "Comp",
"Per" : 81,
"Add" : "alephata"
}
{
"_id" : ObjectId("596c46b45c2af306687b3151"),
"Roll_no" : 4,
"Name" : "Pooja",
"Dept" : "E&TC",
"Per" : 58,
"Add" : "Pune"
}
Assignment B2

[jcoe@localhost bin]$ su
Password:
[root@localhost bin]# ./mongo
MongoDB shell version: 2.6.12
connecting to: test
> use DBMS
switched to db DBMS

>db.emp1.insert([{"Emp_id":01,"Name":"Ashwini","Dept":"manufacturing","Salary":1000,"
Addres":"Pune"},
{"Emp_id":02,"Name":"Anuja","Dept":"purch","Salary":2000,"Addres":"Ngpur"},
{"Emp_id":03,"Name":"Madhuri","Dept":"manufacturing","Salary":3000,"Addres":"Mumbai
"},{"Emp_id":04,"Name":"Tejashri","Dept":"Account","Salary":4000,"Addres":"Delhi"},
{"Emp_id":05,"Name":"Pooja","Dept":"Testing","Salary":15000,"Addres":"Pune"},
{"Emp_id":06,"Name":"Vishal","Dept":"Sales","Salary":10000,"Addres":"Delhi"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> db.emp1.find()
{ "_id" : ObjectId("596c3b5f5c2af306687b3148"), "Emp_id" : 1, "Name" : "Ashwini",
"Dept" : "manufacturing", "Salary" : 1000, "Addres" : "Pune" }
{ "_id" : ObjectId("596c3b5f5c2af306687b3149"), "Emp_id" : 2, "Name" : "Anuja", "Dept" :
"purch", "Salary" : 2000, "Addres" : "Ngpur" }
{ "_id" : ObjectId("596c3b5f5c2af306687b314a"), "Emp_id" : 3, "Name" : "Madhuri",
"Dept" : "manufacturing", "Salary" : 3000, "Addres" : "Mumbai" }
{ "_id" : ObjectId("596c3b5f5c2af306687b314b"), "Emp_id" : 4, "Name" : "Tejashri", "Dept"
: "Account", "Salary" : 4000, "Addres" : "Delhi" }
{ "_id" : ObjectId("596c3b5f5c2af306687b314c"), "Emp_id" : 5, "Name" : "Pooja", "Dept" :
"Testing", "Salary" : 15000, "Addres" : "Pune" }
{ "_id" : ObjectId("596c3b5f5c2af306687b314d"), "Emp_id" : 6, "Name" : "Vishal", "Dept" :
"Sales", "Salary" : 10000, "Addres" : "Delhi" }

> db.emp1.ensureIndex({Emp_id:01})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

> db.emp1.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "DBMS.emp1"
},
{
"v" : 1,
"key" : {
"Emp_id" : 1
},
"name" : "Emp_id_1",
"ns" : "DBMS.emp1"
}
]
> db.emp1.ensureIndex({Emp_id:01,"Name":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}

> db.emp1.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "DBMS.emp1"
},
{
"v" : 1,
"key" : {
"Emp_id" : 1
},
"name" : "Emp_id_1",
"ns" : "DBMS.emp1"
},
{
"v" : 1,
"key" : {
"Emp_id" : 1,
"Name" : 1
},
"name" : "Emp_id_1_Name_1",
"ns" : "DBMS.emp1"
}

> db.emp1.aggregate([{$group:{_id:"$Dept","Avg_salary":{$max:"$Salary"}}}])
{ "_id" : "Sales", "Avg_salary" : 10000 }
{ "_id" : "Testing", "Avg_salary" : 15000 }
{ "_id" : "Account", "Avg_salary" : 4000 }
{ "_id" : "purch", "Avg_salary" : 2000 }
{ "_id" : "manufacturing", "Avg_salary" : 3000 }

> db.emp1.aggregate([{$group:{_id:"$Dept","sum":{$sum:"$Salary"}}}])
{ "_id" : "Sales", "sum" : 10000 }
{ "_id" : "Testing", "sum" : 15000 }
{ "_id" : "Account", "sum" : 4000 }
{ "_id" : "purch", "sum" : 2000 }
{ "_id" : "manufacturing", "sum" : 4000 }

> db.emp1.aggregate([{$group:{_id:"$Dept","min":{$min:"$Salary"}}}])
{ "_id" : "Sales", "min" : 10000 }
{ "_id" : "Testing", "min" : 15000 }
{ "_id" : "Account", "min" : 4000 }
{ "_id" : "purch", "min" : 2000 }
{ "_id" : "manufacturing", "min" : 1000 }

> db.emp1.aggregate([{$group:{_id:"$Dept","max":{$max:"$Salary"}}}])
{ "_id" : "Sales", "max" : 10000 }
{ "_id" : "Testing", "max" : 15000 }
{ "_id" : "Account", "max" : 4000 }
{ "_id" : "purch", "max" : 2000 }
{ "_id" : "manufacturing", "max" : 3000 }

> > db.emp1.aggregate([{$group:{_id:"","sum":{$sum:"$Salary"}}}])


{ "_id" : "", "sum" : 35000 }
> db.emp1.aggregate([{$group:{_id:"","avg":{$avg:"$Salary"}}}])
{ "_id" : "", "avg" : 5833.333333333333 }

> db.emp1.aggregate([{$group:{_id:"","min":{$min:"$Salary"}}}])
{ "_id" : "", "min" : 1000 }

> db.emp1.aggregate([{$group:{_id:"","max":{$max:"$Salary"}}}])
{ "_id" : "", "max" : 15000 }

> db.emp1.aggregate([{$group:{_id:"","first":{$first:"$Salary"}}}])
{ "_id" : "", "first" : 1000 }

> db.emp1.aggregate([{$group:{_id:"","last":{$last:"$Salary"}}}])
{ "_id" : "", "last" : 10000 }

> db.emp1.distinct("salary")
[ 11000 ]

.> db.emp1.find().count()
[5]

> db.emp1.find().sort({emp_id:-1})
{ "_id" : ObjectId("5966feed13742937e930b3ea"), "Name" : "snehal", "emp_id" : 4, "Dept" :
"IT", "Add" : "nagpur", "Salary" : 12000 }
{ "_id" : ObjectId("5966fee713742937e930b3e9"), "Name" : "vishal", "emp_id" : 3, "Dept" :
"mech", "Add" : "pune", "salary" : 11000 }
{ "_id" : ObjectId("5966fec313742937e930b3e7"), "Name" : "Ankita", "emp_id" : 2, "Dept" :
"civil", "Add" : "mumbai", "Salary" : 2340 }
{ "_id" : ObjectId("5966fe9e13742937e930b3e6"), "Name" : "Ashwini", "emp_id" : 1,
"Dept" : "Comp", "Add" : "pune", "Salart" : 1000 }

> db.emp1.remove({emp_id:03})
WriteResult({ "nRemoved" : 1 })
> db.emp1.find()
{ "_id" : ObjectId("5966fe9e13742937e930b3e6"), "Name" : "Ashwini", "emp_id" : 1,
"Dept" : "Comp", "Add" : "pune", "Salart" : 1000 }
{ "_id" : ObjectId("5966fec313742937e930b3e7"), "Name" : "Ankita", "emp_id" : 2, "Dept" :
"civil", "Add" : "mumbai", "Salary" : 2340 }
{ "_id" : ObjectId("5966feed13742937e930b3ea"), "Name" : "snehal", "emp_id" : 4, "Dept" :
"IT", "Add" : "nagpur", "Salary" : 12000 }
>
Assignment B3

[jcoe@localhost bin]$ su
Password:
[root@localhost bin]# ./mongo
MongoDB shell version: 2.6.12
connecting to: test

> use DBMS


switched to db DBMS
>db.customer.insert([{"Cust_id":"M534627","Name":"Ashwini","B_name":"Maharastra","B
alance":10000,"Location":"ABC"},
{"Cust_id":"S5346037","Name":"Anuja","B_name":"SBI","Balance":21000,"Location":"DE
F"},
{"Cust_id":"M50253","Name":"Madhuri","B_name":"Maharastra","Balance":2000,"Locatio
n":"GHI"},{"Cust_id":"I5367","Name":"Tejashri","B_name":"Bank
ofIndia","Balance":5000,"Location":"JKL"},
{"Cust_id":"M023527","Name":"Pooja","B_name":"Maharastra","Balance":10000,"Location
":"MNO"},
{"Cust_id":"S534627","Name":"Rupali","B_name":"SBI","Balance":10000,"Location":"PQ
R"},
{"Cust_id":"M5921007","Name":"Vishal","B_name":"Maharastra","Balance":100000,"Locat
ion":"STU"},
{"Cust_id":"S75823","Name":"Prasad","B_name":"SBI","Balance":10000,"Location":"VWX
"},
{"Cust_id":"I676429","Name":"Dipika","B_name":"BankofIndia","Balance":10000,"Locatio
n":"YZA"},
{"Cust_id":"M1636827","Name":"Sandip","B_name":"Maharastra","Balance":200000,"Loca
tion":"BCD"},
{"Cust_id":"S534627","Name":"Rahul","B_name":"SBI","Balance":50000,"Location":"EFG
"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 11,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.customer.find().pretty()
{
"_id" : ObjectId("597032104a81702a27e7c9ad"),
"Cust_id" : "M534627",
"Name" : "Ashwini",
"B_name" : "Maharastra",
"Balance" : 10000,
"Location" : "ABC"
}
{
"_id" : ObjectId("597032104a81702a27e7c9ae"),
"Cust_id" : "S5346037",
"Name" : "Anuja",
"B_name" : "SBI",
"Balance" : 21000,
"Location" : "DEF"
}
{
"_id" : ObjectId("597032104a81702a27e7c9af"),
"Cust_id" : "M50253",
"Name" : "Madhuri",
"B_name" : "Maharastra",
"Balance" : 2000,
"Location" : "GHI"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b0"),
"Cust_id" : "I5367",
"Name" : "Tejashri",
"B_name" : "Bank of India",
"Balance" : 5000,
"Location" : "JKL"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b1"),
"Cust_id" : "M023527",
"Name" : "Pooja",
"B_name" : "Maharastra",
"Balance" : 10000,
"Location" : "MNO"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b2"),
"Cust_id" : "S534627",
"Name" : "Rupali",
"B_name" : "SBI",
"Balance" : 10000,
"Location" : "PQR"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b3"),
"Cust_id" : "M5921007",
"Name" : "Vishal",
"B_name" : "Maharastra",
"Balance" : 100000,
"Location" : "STU"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b4"),
"Cust_id" : "S75823",
"Name" : "Prasad",
"B_name" : "SBI",
"Balance" : 10000,
"Location" : "VWX"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b5"),
"Cust_id" : "I676429",
"Name" : "Dipika",
"B_name" : "Bank of India",
"Balance" : 10000,
"Location" : "YZA"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b6"),
"Cust_id" : "M1636827",
"Name" : "Sandip",
"B_name" : "Maharastra",
"Balance" : 200000,
"Location" : "BCD"
}
{
"_id" : ObjectId("597032104a81702a27e7c9b7"),
"Cust_id" : "S534627",
"Name" : "Rahul",
"B_name" : "SBI",
"Balance" : 50000,
"Location" : "EFG"
}

> var map=function(){emit(this.Location,this.Balance)}

> var reduce=function(key,values){return Array.sum(values)}

> db.customer.mapReduce(map,reduce,{'out':'SBI'}).find()

{ "_id" : "ABC", "value" : 10000 }


{ "_id" : "BCD", "value" : 200000 }
{ "_id" : "DEF", "value" : 21000 }
{ "_id" : "EFG", "value" : 50000 }
{ "_id" : "GHI", "value" : 2000 }
{ "_id" : "JKL", "value" : 5000 }
{ "_id" : "MNO", "value" : 10000 }
{ "_id" : "PQR", "value" : 10000 }
{ "_id" : "STU", "value" : 100000 }
{ "_id" : "VWX", "value" : 10000 }
{ "_id" : "YZA", "value" : 10000 }

> db.SBI.find()
{ "_id" : "ABC", "value" : 10000 }
{ "_id" : "BCD", "value" : 200000 }
{ "_id" : "DEF", "value" : 21000 }
{ "_id" : "EFG", "value" : 50000 }
{ "_id" : "GHI", "value" : 2000 }
{ "_id" : "JKL", "value" : 5000 }
{ "_id" : "MNO", "value" : 10000 }
{ "_id" : "PQR", "value" : 10000 }
{ "_id" : "STU", "value" : 100000 }
{ "_id" : "VWX", "value" : 10000 }
{ "_id" : "YZA", "value" : 10000 }
>
Assignment B4

import java.util.Scanner;
import com.mongodb.*;
public class conn
{
public static void main(String[] args)
{
Try
{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "mydb" );
DBCollectipackage conMongo1;
on coll = db.createCollection("Stud",null);
int ch = 0;
do
{
System.out.println("Enter your choice: \n 1.Insert \n2.Select \n3.Update \n4.Delete");
Scanner s=new Scanner(System.in);
ch=s.nextInt();

switch (ch)
{
case 1:

BasicDBObject doc1 = new


BasicDBObject("age","1").append("name","Mona");
BasicDBObject doc2 = new
BasicDBObject("age","2").append("name","swati");
coll.insert(doc1);
coll.insert(doc2);
DBCursor cursor = coll.find();
System.out.println("Inserted into db");
while(cursor.hasNext())
{
System.out.println(cursor.next());
}
break;
case 2:
DBCursor cursor3 = coll.find();
System.out.println("Display Records");
while(cursor3.hasNext())
{
System.out.println(cursor3.next());
}
break;
case 3:
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("name",
"Mohan"));
newDocument.append("$set", new BasicDBObject().append("name",
"rahul"));
BasicDBObject searchQuery = new BasicDBObject().append("name",
"Ashwini");
BasicDBObject searchQuery1 = new BasicDBObject().append("name",
"swati");

coll.update(searchQuery, newDocument);
coll.update(searchQuery1, newDocument);
DBCursor cursor2 = coll.find();
System.out.println("Updated into db");

while(cursor2.hasNext())
{
System.out.println(cursor2.next());
}
break;
case 4:
System.out.println("Data is removed");
BasicDBObject R1 = new BasicDBObject();
R1.put("name", "Mona");
coll.remove(R1);
BasicDBObject R2 = new BasicDBObject();
R1.put("name", "swati");
coll.remove(R2);
break;
default:
break;
}

} while (ch!=6);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Output:
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
1
Inserted into db
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Ashwini"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "swati"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
2
Display Records
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Ashwini"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "swati"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
3
Updated into db
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Mohan"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "rahul"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
2
Display Records
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Mohan"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "rahul"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
4
Data is removed
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete

You might also like