39027-Vaibhavi Hule DBMS
39027-Vaibhavi Hule DBMS
emp_id varchar2(10),
emp_name varchar2(20),
emp_desc varchar2(15),
dept_name varchar2(20),
emp_city varchar2(10),
primary key(emp_name)
);
desc Department;
select D.dept_name,W.emp_name,W.emp_salary
from Department as D,Workers as W
where D.dept_name=W.dept_name and D.manager_name='Piyush';
-- Inner join
select Department.dept_name,Workers.emp_salary
from Department
inner join Workers
on Workers.dept_name=Department.dept_name;
-- Left join
select Department.dept_name,Workers.emp_salary
from Department
left join Workers
on Workers.dept_name=Department.dept_name;
-- Full join
select Department.dept_name,Workers.emp_salary
from Department
full join Workers
on Workers.dept_name=Department.dept_name;
-- Subqueries
select emp_name,dept_name from Employee
where emp_name in (select emp_name from Employee where emp_city='Pune');
update Department
set manager_name='Parth'
where dept_name in (select dept_name from Workers where emp_name='Gaurav');
select * from Department where dept_name='CustomerCare';
select dept_name from Department where exists(select emp_name from Employee where
emp_city='Pune');
select emp_id,Employee.emp_name,emp_salary
from Employee,Workers
where Employee.emp_name=Workers.emp_name and emp_city='Pune';
select emp_name,dept_name
from Employee
order by dept_name desc;
select avg(emp_salary)
from Workers;
select count(emp_name)
from Employee
where emp_city='Pune';
select sum(emp_salary)
from Workers
where dept_name='Finance';
select emp_name,emp_salary
from Workers
where emp_salary between 10000 and 20000;
select emp_name
from Employee
where exists(select emp_salary from Workers where Employee.emp_name=Workers.emp_name
and emp_salary>=20000);
create or replace function calfine(days number)
return number is
amount number:=0;
begin
IF(days>15 and days<30) then
amount:=days*5;
ELSIF(days>30) then
amount:=days*50;
ELSE
amount:=0;
END IF;
return amount;
end;
/
days:=sysdate-d_issue;
dbms_output.put_line('Roll no = '||roll_num);
dbms_output.put_line('Name = '||nm);
dbms_output.put_line('Book name = '||book_name);
dbms_output.put_line('Status '||s);
dbms_output.put_line('Days are '||days);
amt:=calfine(days);
dbms_output.put_line('Fine amount is '||amt);
end;
/
Output:
Microsoft Windows [Version 10.0.19044.2075]
(c) Microsoft Corporation. All rights reserved.
Table created.
Table created.
1 row created.
1 row created.
1 row created.
Roll no = 37
Name = Mohana
Book name = HCI
Status I
Days are 38
Fine amount is 1900
SQL>@library
date_fine date;
d_issue date;
days number(2);
amt number(5);
begin
roll_num:=&roll_num;
book_name:=&book_name;
select roll_no,name,name_of_book,status,date_of_issue into
roll_num,nm,book_name,s,d_issue from borrower where roll_no=roll_num;
days:=sysdate-d_issue;
dbms_output.put_line('Roll no = '||roll_num);
dbms_output.put_line('Name = '||nm);
dbms_output.put_line('Book name = '||book_name);
dbms_output.put_line('Status '||s);
dbms_output.put_line('Days are '||days);
amt:=calfine(days);
dbms_output.put_line('Fine amount is '||amt);
--Exception handling
IF(amt>0) THEN
insert into fine values(roll_num,sysdate,amt);
dbms_output.put_line('One row inserted');
update borrower
set status='R'
where roll_num=roll_no;
END IF;
exception
when no_data_found then
dbms_output.put_line('Record not found');
end;
/
Output:
Microsoft Windows [Version 10.0.19044.2075]
(c) Microsoft Corporation. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Roll no = 1
Name = Anushka
Book name = SPOS
Status I
Days are 20
Fine amount is 100
One row inserted
SQL>@library
total number(5);
begin
roll_num:=&roll_num;
select roll_no,s_name,total_marks into roll_num,stud_name,total from
stud_marks6 where roll_no=roll_num;
dbms_output.put_line('Roll number = '||roll_num);
dbms_output.put_line('Name of student = '||stud_name);
dbms_output.put_line('Total marks = '||total);
stud_class:=proc_grades(total);
dbms_output.put_line('Grade is '||stud_class);
Output:
Microsoft Windows [Version 10.0.19044.2075]
(c) Microsoft Corporation. All rights reserved.
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> select * from stud_marks;
ROLL_NO S_NAME TOTAL_MARKS
---------- -------------------- -----------
42 Aditi 1200
1 Anushka 920
37 Mohana 830
27 Vaibhavi 800
SQL>@grade_proc
SQL>@grade_proc
Enter value for roll_num: 01
old 8: roll_num:=&roll_num;
new 8: roll_num:=01;
Roll number = 1
Name of student = Anushka
Total marks = 920
Grade is First Class
One row inserted
begin
For a in(select roll_no,name,age,marks from O_RollCall where roll_no not
in(select roll_no from N_RollCall))
loop
insert into N_RollCall values(a.roll_no,a.name,a.age,a.marks);
end loop;
dbms_output.put_line('Values copied');
end;
/
Output:
Microsoft Windows [Version 10.0.19044.2075]
(c) Microsoft Corporation. All rights reserved.
Enter password:
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Values copied
PL/SQL procedure successfully completed.
7 rows selected.
create or replace function calfine(days number)
return number is
amount number:=0;
begin
IF(days>15 and days<30) then
amount:=days*5;
ELSIF(days>30) then
amount:=days*50;
ELSE
amount:=0;
END IF;
return amount;
end;
/
begin
insert into audit(roll_no,name,date_of_issue,name_of_book,status)
values(:old.roll_no,:old.name,:old.date_of_issue,:old.name_of_book,:old.status)
;
date_fine date;
d_issue date;
days number(2);
amt number(5);
begin
roll_num:=&roll_num;
book_name:=&book_name;
select roll_no,name,name_of_book,status,date_of_issue into
roll_num,nm,book_name,s,d_issue from library where roll_no=roll_num;
days:=sysdate-d_issue;
dbms_output.put_line('Roll no = '||roll_num);
dbms_output.put_line('Name = '||nm);
dbms_output.put_line('Book name = '||book_name);
dbms_output.put_line('Status '||s);
dbms_output.put_line('Days are '||days);
amt:=calfine(days);
dbms_output.put_line('Fine amount is '||amt);
end;
/
Output:
Microsoft Windows [Version 10.0.19044.2075]
(c) Microsoft Corporation. All rights reserved.
Enter password:
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
no rows selected
1 row updated.
1.Creating database:
test> use mystudents
switched to db mystudents
2.Creating collection:
mystudents> db.createCollection("student")
{ ok: 1 }
3.Insert:
mystudents> db.student.insertOne({
... name:"Vaibhavi",
... roll_no:1,
... Year:"Third",
... sports:"Badminton",
... marks:80})
{
acknowledged: true,
insertedId: ObjectId("6370e5ea13ac0618388dce78")
}
mystudents> db.student.insertMany([{
... name:"Mohana",
... roll_no:8,
... Year:"Second",
... sports:"Badminton",
... marks:55
... },
... {
... name:"Advait",
... roll_no:2,
... Year:"First",
... sports:"Football",
... marks:74
... },
... {
... name:"Prasad",
... roll_no:3,
... Year:"Second",
... sports:"Cricket",
... marks:50
... },
... {
... name:"Aditi",
... roll_no:4,
... Year:"Third",
... sports:"Badminton",
... marks:30
... },
... {
... name:"Anushka",
... roll_no:5,
... Year:"First",
... sports:"Football",
... marks:90
... },
... {
... name:"Safal",
... roll_no:6,
... Year:"First",
... sports:"Cricket",
... marks:63
... },
... {
... name:"Anvesh",
... roll_no:7,
... Year:"Third",
... sports:"Badminton",
... marks:32
... }])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("6370e60013ac0618388dce79"),
'1': ObjectId("6370e60013ac0618388dce7a"),
'2': ObjectId("6370e60013ac0618388dce7b"),
'3': ObjectId("6370e60013ac0618388dce7c"),
'4': ObjectId("6370e60013ac0618388dce7d"),
'5': ObjectId("6370e60013ac0618388dce7e"),
'6': ObjectId("6370e60013ac0618388dce7f")
}
}
mystudents> db.student.find({Year:"Third"})
[
{
_id: ObjectId("6370e5ea13ac0618388dce78"),
name: 'Vaibhavi',
roll_no: 1,
Year: 'Third',
sports: 'Badminton',
marks: 80
},
{
_id: ObjectId("6370e60013ac0618388dce7c"),
name: 'Aditi',
roll_no: 4,
Year: 'Third',
sports: 'Badminton',
marks: 30
},
{
_id: ObjectId("6370e60013ac0618388dce7f"),
name: 'Anvesh',
roll_no: 7,
Year: 'Third',
sports: 'Badminton',
marks: 32
}
]
5.Updating records:
mystudents> db.student.updateOne({roll_no:2},{$set:{marks:70}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
mystudents> db.student.find({roll_no:2})
[
{
_id: ObjectId("6370e60013ac0618388dce7a"),
name: 'Advait',
roll_no: 2,
Year: 'First',
sports: 'Football',
marks: 70
}
]
6.Delete records:
mystudents> db.student.deleteOne({roll_no:8})
{ acknowledged: true, deletedCount: 1 }
Assignment 11
Creating database:
test> use mystudents
switched to db mystudents
Indexes:
1.Creating Index:
mystudents> db.student.createIndex({name:1},{name:"Student's names"})
Student's names
2.Find Index:
mystudents> db.student.getIndexes()
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { name: 1 }, name: "Student's names" }
]
3.Display Indexes:
mystudents> db.student.find()
[
{
_id: ObjectId("6370e5ea13ac0618388dce78"),
name: 'Vaibhavi',
roll_no: 1,
Year: 'Third',
sports: 'Badminton',
marks: 80
},
{
_id: ObjectId("6370e60013ac0618388dce7a"),
name: 'Advait',
roll_no: 2,
Year: 'First',
sports: 'Football',
marks: 70
},
{
_id: ObjectId("6370e60013ac0618388dce7b"),
name: 'Prasad',
roll_no: 3,
Year: 'Second',
sports: 'Cricket',
marks: 50
},
{
_id: ObjectId("6370e60013ac0618388dce7c"),
name: 'Aditi',
roll_no: 4,
Year: 'Third',
sports: 'Badminton',
marks: 30
},
{
_id: ObjectId("6370e60013ac0618388dce7d"),
name: 'Anushka',
roll_no: 5,
Year: 'First',
sports: 'Football',
marks: 90
},
{
_id: ObjectId("6370e60013ac0618388dce7e"),
name: 'Safal',
roll_no: 6,
Year: 'First',
sports: 'Cricket',
marks: 63
},
{
_id: ObjectId("6370e60013ac0618388dce7f"),
name: 'Anvesh',
roll_no: 7,
Year: 'Third',
sports: 'Badminton',
marks: 32
}
]
4.Drop Index:
mystudents> db.student.dropIndex("Student's names")
{ nIndexesWas: 2, ok: 1 }
Aggregation:
1. $match:
mystudents>db.student.aggregate([{$match:{sports:{$in:["Football","Cricket"]
}}}])
[
{
_id: ObjectId("6370e60013ac0618388dce7a"),
name: 'Advait',
roll_no: 2,
Year: 'First',
sports: 'Football',
marks: 70
},
{
_id: ObjectId("6370e60013ac0618388dce7b"),
name: 'Prasad',
roll_no: 3,
Year: 'Second',
sports: 'Cricket',
marks: 50
},
{
_id: ObjectId("6370e60013ac0618388dce7d"),
name: 'Anushka',
roll_no: 5,
Year: 'First',
sports: 'Football',
marks: 90
},
{
_id: ObjectId("6370e60013ac0618388dce7e"),
name: 'Safal',
roll_no: 6,
Year: 'First',
sports: 'Cricket',
marks: 63
}
]
2. $sort:
mystudents> db.student.aggregate({$sort:{marks:-1}})
[
{
_id: ObjectId("6370e60013ac0618388dce7d"),
name: 'Anushka',
roll_no: 5,
Year: 'First',
sports: 'Football',
marks: 90
},
{
_id: ObjectId("6370e5ea13ac0618388dce78"),
name: 'Vaibhavi',
roll_no: 1,
Year: 'Third',
sports: 'Badminton',
marks: 80
},
{
_id: ObjectId("6370e60013ac0618388dce7a"),
name: 'Advait',
roll_no: 2,
Year: 'First',
sports: 'Football',
marks: 70
},
{
_id: ObjectId("6370e60013ac0618388dce7e"),
name: 'Safal',
roll_no: 6,
Year: 'First',
sports: 'Cricket',
marks: 63
},
{
_id: ObjectId("6370e60013ac0618388dce7b"),
name: 'Prasad',
roll_no: 3,
Year: 'Second',
sports: 'Cricket',
marks: 50
},
{
_id: ObjectId("6370e60013ac0618388dce7f"),
name: 'Anvesh',
roll_no: 7,
Year: 'Third',
sports: 'Badminton',
marks: 32
},
{
_id: ObjectId("6370e60013ac0618388dce7c"),
name: 'Aditi',
roll_no: 4,
Year: 'Third',
sports: 'Badminton',
marks: 30
}
]
3. $count:
mystudents>db.student.aggregate([{$match:{sports:{$in:["Football","Cricket"]
}}},{$count:"totalCount"}])
[ { totalCount: 4 } ]
Assignment 12
Create database:
test> use mystudents
switched to db mystudents
Create collection:
mystudents> db.createCollection("student_info")
{ ok: 1 }
mystudents> db.student_info.insertOne({
... name:"Vaibhavi",
... roll_no:27,
... Year:"Second",
... sports:"Badminton",
... marks:65})
{
acknowledged: true,
insertedId: ObjectId("63710ac823940cd204dbd0ac")
}
mystudents> db.student_info.insertOne({
... name:"Anushka",
... roll_no:01,
... Year:"Third",
... sports:"Hockey",
... marks:80})
{
acknowledged: true,
insertedId: ObjectId("63710ad223940cd204dbd0ad")
}
mystudents> db.student_info.insertOne({
... name:"Aditi",
... roll_no:42,
... Year:"Third",
... sports:"Cricket",
... marks:70})
{
acknowledged: true,
insertedId: ObjectId("63710ada23940cd204dbd0ae")
}
mystudents> db.student_info.insertOne({
... name:"Mohana",
... roll_no:37,
... Year:"Second",
... sports:"Football",
... marks:97})
{
acknowledged: true,
insertedId: ObjectId("63710ae223940cd204dbd0af")
}
MapReduce function:
mystudents> var map=function(){emit(this.Year,this.marks);};
mystudents> db.student_info.mapReduce(map,reduce,{out:"result"});
{ result: 'result', ok: 1 }
mystudents> db.result.find()
[
{ _id: 'Second', value: 227 },
{ _id: 'Third', value: 150 }
]