DBMS Final Code
DBMS Final Code
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)
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.
+——–+———-+————+——–+———-+———-+
| emp_id | Emp_name | DOB | Salary | Adddress | dept |
+——– +———-+————+——–+———-+———-+
|1 | Ashwini | 0000-00-00 | 10000 | Otur | Computer |
+——–+———-+————+——–+———-+———-+
1 row in 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
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)
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 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
+——–+———-+———-+————+————+———+
| 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)
+——–+———-+———-+————+————+———+
| 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 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)
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)
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.
+———+———+———-+——-+—————+
| 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 *from product_details Pro_id=(select Pro_id from product_details where Pro_-
name=’ABC’);
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)
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> 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> delimiter ;
mysql> call R3(7,’C++’);
+——+
| diff |
+——+
| 16 |
+——+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.10 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)
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)
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> 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)
+——+——+————-+
| 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> 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)
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)
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 table O_rollcall (rno int(3) primary key, name varchar(20),addr varchar(30));
Query OK, 0 rows affected (0.18 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)
+——+——-+——–+
| rno | name | addr |
+——+——-+——–+
| 2 | Anuja | Pune |
| 3 | Pooja | Manmad |
+——+——-+——–+
2 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> 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> 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)
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)
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> 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> 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)
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:
case 2:
case 3:
case 4:
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:"","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
> db.customer.mapReduce(map,reduce,{'out':'SBI'}).find()
> 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:
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