Code Part
Code Part
Office Table:
Office ID Office Square
City Footage
Problem Statement : Design & dvelop SQL DDL statements which demonstrate the use of
SQL objects such as Table, View, Index, Sequence, Synonym, different constraints etc.
Code :
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Practical No : 03
Roll No : 12 Class : TE
Problem Statement : Design at least 10 SQL queries for suitable database application using
SQL DML statements: Insert, Select, Update, Delete with operators, functions, and set operator.
Code :
archu@archu-virtual-machine:~$ sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Database changed
mysql> create table capital(cap_no int, cap_name varchar(20),state_no int, primary key(cap_no));
Query OK, 0 rows affected (0.10 sec)
mysql> create table state(state_no int,state_name varchar(20),state_code
int,capital varchar(20),primary key(state_no)); Query OK, 0 rows affected (0.05
sec)
mysql>
mysql> insert into capital values(01,'Asam',01);
Query OK, 1 row affected (0.17 sec)
mysql>
mysql> select * from state where state_no=(select state_no from state where state_name='MH');
Empty set (0.04 sec)
mysql> select * from state where state_no=(select state_no from state where state_name='GUJ');
Empty set (0.01 sec)
mysql>
Practical No : 04
Roll No : 12 Class : TE
Problem Statement : Write a PL/SQL block to calculate fine for a library book by accessing
borrower information from the database.
Code :
archu@archu-virtual-machine:~$ sudo service oracle-xe start; archu@archu-virtual-
machine:~$ sqlplus
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Table created.
1 row created.
1 row created.
1 row created.
2 PRANALI 22-04-00 TE
I
3 MADHURI 26-11-02 SE
I
Table created.
DECLARE
RNO NUMBER(3):=1;
BNAME VARCHAR2(20):='DBMS';
NO_DAYS NUMBER(7);
ISSUEDATE DATE;
FINEAMT NUMBER(5):=0;
BEGIN
SELECT DATEOFISSUE INTO ISSUEDATE FROM BORROWER WHERE ROLL_NO=RNO;
SELECT SYSDATE-TO_DATE(ISSUEDATE) DAYS INTO NO_DAYS FROM DUAL;
IF(NO_DAYS>=15 AND NO_DAYS<=30) THEN
FINEAMT:=NO_DAYS*5;
ELSIF NO_DAYS >30 THEN
FINEAMT:=NO_DAYS*50;
END IF;
UPDATE BORROWER SET STATUS='R' WHERE ROLL_NO=RNO;
IF FINEAMT>=0 THEN
INSERT INTO FINE VALUES(RNO,SYSDATE,FINEAMT);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('RECORD NOT FOUND');
END;
/
SQL>
Practical no : 05
PROBLEM STATEMENT :Write and execute simple PL/SQL programs and apply this
knowledge to execute PL/SQL procedures and functions.
Code :
archu@archu-virtual-machine:~$ sudo service oracle-xe start
[sudo] password for archu:
archu@archu-virtual-machine:~$ sqlplus
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Table created.
Table created.
1 row created.
1 row created.
1 row created.
SQL> CREATE OR REPLACE PROCEDURE PROC_GRADE(RNO NUMBER,NAME
VARCHAR,MARKS NUMBER) IS
2 CLASS VARCHAR(20);
3 BEGIN
4 IF(MARKS <= 1500 AND MARKS >= 990) THEN
5 CLASS :='DISTINCTION';
6 ELSEIF(MARKS <= 989 AND MARKS >= 900) THEN 7 CLASS :='FIRST';
8 ELSEIF(MARKS <= 899 AND MARKS >= 825) THEN
9 CLASS :='HIGHER SECOND';
10 END IF;
11 INSERT INTO STUD_MARKS VALUES(NAME,MARKS);
12 INSERT INTO RESULT VALUES(RNO,NAME,CLASS);
13 END;
14 /
NAME TOTAL_MARKS
---------- ----------- -----------
ARCHANA 1000
PRANALI 800
MADHURI 700
SQL>
Practical No : 06
PROBLEM STATEMENT Write a PL/SQL block to create cursor to copy contents of one table
into another Avoid redundancy.
Code :
archu@archu-virtual-machine:~$ sqlplus
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Table created.
1 row created.
1 row created.
1 row created.
ROLL NAME
---------- ----------
3 TANU
1 KRANTI
2 KAVITA
SQL> CREATE TABLE N_ROLLCALL(ROLL_NO NUMBER,NAME VARCHAR(10));
Table created.
1 row created.
1 row created.
SQL> DECLARE
2 CURSOR C1 IS SELECT * FROM O_ROLLCALL;
3 CURSOR C2 IS SELECT ROLL_NO FROM N_ROLLCALL;
4 X O_ROLLCALL%ROWTYPE;
5 R NUMBER(3);
6 BEGIN
7 OPEN C1;
8 OPEN C2;
9 LOOP
10 FETCH C1 INTO X;
11 FETCH C2 INTO R;
12 IF C1%NOTFOUND THEN
13 EXIT;
14 END IF;
15 IF R<>X.ROLL THEN
16 INSERT INTO N_ROLLCALL VALUES(X.ROLL,X.NAME);
17 END IF;
18 END LOOP;
19 CLOSE C2;
20 CLOSE C1; 21 END;
ROLL_NO NAME
---------- ----------
1 KRANTI
2 TANU
3 KAVITA 1 KRANTI
Practical No : 07
PROBLEM STATEMENT: Write a PL/SQL block to create trigger on Library table to keep track
Of updation and deletion of records.
Code :
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Table dropped.
Table dropped.
Table created.
1 row created.
SQL> INSERT INTO LIBRARY VALUES(2,'CNS','TANEBAUM',13);
1 row created.
1 row created.
Table created.
1 row updated.
SQL> SELECT * FROM LIBRARY_AUDIT;
1 row deleted.
SQL>
Practical No : 8
Roll No : 12
Problem Statement : Write a program to implement MYSQL/Oracle databases connectivity with
any front end language to implement Database navigation operations (add,delete,edit,etc.)
Code :
import java.sql.*;
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/nehadb","root","root");
Statement sm = con.createStatement();
int ch;
do
{
System.out.println("Enter choice \n 1. Insert \n 2. Select \n 3. Update \n 4. Delete \n 5. Exit");
Object s;
ch = s.nextInt();
String sql; switch(ch)
{
case 1: sql = "Insert into stud values('Aniket',1)";
sm.executeUpdate(sql);
System.out.println("Record is Inserted");
break;
Problem Statement : Study of Open Source NOSQL Database: MongoDB (Installation, Basic
CRUD operations, Execution)
Code :
> use archu
switched to db
archu
> db.createCollection("Mycoll");
{ "ok" : 1 }
> db.Mycoll.insert({Roll:2,Name:"archu",Addr:"Garakole",Age:20});
WriteResult({ "nInserted" : 1 })
> db.Mycoll.insert({Roll:3,Name:"rohi",Addr:"Barshi",Age:19});
WriteResult({ "nInserted" : 1 })
> db.Mycoll.find
function(query, fields, limit, skip, batchSize, options) {
var cursor = new DBQuery(this._mongo,
this._db,
this, this._fullName,
this._massageObject(query),
fields, limit,
skip, batchSize,
options || this.getQueryOptions());
{
const session = this.getDB().getSession();
const readPreference =
session._getSessionAwareClient().getReadPreference(session); if (readPreference !==
null) { cursor.readPref(readPreference.mode, readPreference.tags);
}
{
const session = this.getDB().getSession();
const readPreference =
session._getSessionAwareClient().getReadPreference(session); if (readPreference !==
null) { cursor.readPref(readPreference.mode, readPreference.tags);
}
return cursor;
}
> db.Mycoll.insert({Roll:1,Name:"archu",Addr:"Garakole",Ae:19});
WriteResult({ "nInserted" : 1 })
> db.Mycoll.insert({Roll:2,Name:"rohi",Addr:"Barshi",Ae:20});
WriteResult({ "nInserted" : 1 })
> db.Mycoll.insert({Roll:3,Name:"pallu",Addr:"Survad",Ae:20});
WriteResult({ "nInserted" : 1 })
> db.Mycoll.find();
{ "_id" : ObjectId("10469d937e2b601047ab27c97b"), "Roll" : 2, "Name" : "archu", "Addr" :
"Garakole", "Age" : 20 }
{ "_id" : ObjectId("10469d960e2b601047ab27c97c"), "Roll" : 3, "Name" : "rohi", "Addr" :
"Barshi", "Age" : 19 }
{ "_id" : ObjectId("10469da00e2b601047ab27c97d"), "Roll" : 1, "Name" : "", "Addr" : "Barshi",
"Ae" : 19 }
{ "_id" : ObjectId("10469da90e2b601047ab27c97e"), "Roll" : 1, "Name" : "archu", "Addr" :
"Garakole", "Ae" : 19 }
{ "_id" : ObjectId("10469dac1e2b601047ab27c97f"), "Roll" : 2, "Name" : "archu", "Addr" :
"Garakole", "Ae" : 20 }
{ "_id" : ObjectId("10469dadde2b601047ab27c980"), "Roll" : 3, "Name" : "pallu", "Addr" :
"survad", "Ae" : 20 }
> db.Mycoll.findOne();
{
"_id" : ObjectId("10469da00e2b601047ab27c97d"),
"Roll" : 1,
"Name" : "archu",
"Addr" : "Garakole",
"Ae" : 19
}
> db.Mycoll.findOne(Roll:3); uncaught exception:
SyntaxError: missing ) after argument list : @(shell):1:22
> db.Mycoll.findOne({Roll:3});
{
"_id" : ObjectId("10469dadde2b601047ab27c980"),
"Roll" : 3,
"Name" : "pallu",
"Addr" : "survad",
"Ae" : 20
}
> db.Mycoll.remove({Name:"rohi"});
WriteResult({ "nRemoved" : 1 }) >
db.Mycoll.remove({Name:"archu"});
WriteResult({ "nRemoved" : 2 })
> db.Mycoll.find();
{ "_id" : ObjectId("10469da00e2b601047ab27c97d"), "Roll" : 1, "Name" : "", "Addr" : "Garakole",
"Ae" : 19 }
{ "_id" : ObjectId("10469dac1e2b601047ab27c97f"), "Roll" : 2, "Name" : "rohi", "Addr" :
"Barshi", "Ae" : 20 }
{ "_id" : ObjectId("10469dadde2b601047ab27c980"), "Roll" : 3, "Name" : "pallu", "Addr" :
"survad", "Ae" : 20 }
> db.Mycoll.update({Name:""},{$set:{Name:"archu"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.Mycoll.Find().pretty(); uncaught exception: TypeError:
db.Mycoll.Find is not a function : @(shell):1:1
> db.Mycoll.find();
{ "_id" : ObjectId("10469da00e2b601047ab27c97d"), "Roll" : 1, "Name" : "archu", "Addr" :
"Garakole", "Ae" : 19 }
{ "_id" : ObjectId("10469dac1e2b601047ab27c97f"), "Roll" : 2, "Name" : "rohi", "Addr" :
"Barshi", "Ae" : 20 }
{ "_id" : ObjectId("10469dadde2b601047ab27c980"), "Roll" : 3, "Name" : "pallu", "Addr" :
"survad", "Ae" : 20 } > db.Mycoll.Find().pretty() uncaught exception: TypeError:
db.Mycoll.Find is not a function : @(shell):1:1
> db.Mycoll.find().pretty()
{
"_id" : ObjectId("10469da00e2b601047ab27c97d"),
"Roll" : 1,
"Name" : "archu",
"Addr" : "Garakole",
"Ae" : 19
}{
"_id" : ObjectId("10469dac1e2b601047ab27c97f"),
"Roll" : 2,
"Name" : "rohi",
"Addr" : "Barshi",
"Ae" : 20 {
"_id" : ObjectId("10469dadde2b601047ab27c980"),
"Roll" : 3,
"Name" : "pallu",
"Addr" : "survad
"Roll" : 3,
"Name" : "pallu",
"Addr" : "survad", "Ae" : 20
Practical No : 10
Roll No : 12 Class : TE
Problem Statement : Design and Develop MongoDB Queries using aggregation and indexing
with suitable example using MongoDB.
Code :
> use archu
switched to db
archu
> db.Marks.find().pretty()
{
"_id" : ObjectId("10469eaf2525bc3d4edecbad2"),
"Name" : "ankita",
"Sub" : "SOM",
"Marks" : 69
}{
"_id" : ObjectId("10469eb11525bc3d4edecbad3"),
"Name" : "Prajkta",
"Sub" : "TOM",
"Marks" : 99
}{
"_id" : ObjectId("10469eb51525bc3d4edecbad4"),
"Name" : "Snehal",
"Sub" : "SOM",
"Marks" : 68
}{
"_id" : ObjectId("10469eb76525bc3d4edecbad5"),
"Name" : "Snehal",
"Sub" : "TOM",
"Marks" : 85
}{
"_id" : ObjectId("10469ebad525bc3d4edecbad6"),
"Name" : "ankita",
"Sub" : "SOM",
"Marks" : 58
}{
"_id" : ObjectId("10469ebd5525bc3d4edecbad7"),
"Name" : "ankita",
"Sub" : "DBMS",
"Marks" : 98
}
> db.Marks.createIndex({"Name":1});
{
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"createdCollectionAutomatically" : false,
"ok" : 1
}
> db.Marks.createIndex({"Sub":2,"Marks":3});
{
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"createdCollectionAutomatically" : false,
"ok" : 1
}
> db.Marks.aggregate([{$group:{_id:"$Sub",Engg_sub:{$sum:1}}}]);
{ "_id" : "SOM", "Engg_sub" : 3 } {
"_id" : "TOM", "Engg_sub" : 2 }
{ "_id" : "DBMS", "Engg_sub" : 1 }
> db.Marks.aggregate([{$group:{_id:"$Sub",Engg_sub:{$avg:"$Marks"}}}]);
{ "_id" : "SOM", "Engg_sub" : 65 } {
"_id" : "TOM", "Engg_sub" : 92 }
{ "_id" : "DBMS", "Engg_sub" : 98 }
> db.Marks.aggregate([{$group:{_id:"$Sub",Engg_sub:{$min:"$Marks"}}}]);
{ "_id" : "SOM", "Engg_sub" : 58 } {
"_id" : "TOM", "Engg_sub" : 85 }
{ "_id" : "DBMS", "Engg_sub" : 98 }
> db.Marks.aggregate([{$group:{_id:"$Sub",Engg_sub:{$max:"$Marks"}}}]);
{ "_id" : "SOM", "Engg_sub" : 69 } {
"_id" : "TOM", "Engg_sub" : 99 }
{ "_id" : "DBMS", "Engg_sub" : 98 }
> db.Marks.aggregate([{$group:{_id:"$Sub",Engg_sub:{$last:"$Marks"}}}]);
{ "_id" : "SOM", "Engg_sub" : 58 } {
"_id" : "TOM", "Engg_sub" : 85 }
{ "_id" : "DBMS", "Engg_sub" : 98 }
> db.Marks.aggregate([{$group:{_id:"$Sub",Engg_sub:{$first:"$Marks"}}}]); { "_id" : "DBMS",
"Engg_sub" : 98 }
{ "_id" : "SOM", "Engg_sub" : 58 } {
"_id" : "TOM", "Engg_sub" : 85 }
>
Practical No : 11
Roll No : 12 Class : TE
Code:
archu@archu-virtual-machine:~$ mongo MongoDB shell version v5.0.13 connecting to:
mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bffd69ac-1701-4ce3-b867-9d55118e80a4") }
MongoDB server version: 5.0.13
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated
and will be removed in an upcoming release.
For installation instructions, see https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
2022-11-13T18:16:38.079+05:30: Using the XFS filesystem is strongly recommended with the
WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-11-13T18:16:42.126+05:30: Access control is not enabled for the database. Read and
write access to data and configuration is unrestricted
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to
you
and anyone you share the URL with. MongoDB may use this information to make
product improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command:
db.disableFreeMonitoring() >
> db.orders.insert({Cust_id:"A123",Amount:500,Status:"A"});
WriteResult({ "nInserted" : 1 })
> db.orders.insert({Cust_id:"A123",Amount:250,Status:"A"});
WriteResult({ "nInserted" : 1 })
> db.orders.insert({Cust_id:"B123",Amount:350,Status:"A"});
WriteResult({ "nInserted" : 1 })
> db.orders.insert({Cust_id:"B123",Amount:440,Status:"B"});
WriteResult({ "nInserted" : 1 })
> db.orders.insert({Cust_id:"A123",Amount:430,Status:"B"});
WriteResult({ "nInserted" : 1 })
> db.orders.mapReduce(function(){emit(this.Cust_id,this.Amount);},function(key,values){return
Array.sum(values)},{out:"XYZ"});
{
"result" : "XYZ",
"timeMillis" : 521,
"counts" : {
"input" : 6,
"emit" : 6,
"reduce" : 2,
"output" : 2
},
"ok" : 1
}
> db.XYZ.find();
{ "_id" : "A123", "value" : 1510 }
{ "_id" : "B123", "value" : 790 }
> db.orders.mapReduce(function(){emit(this.Cust_id,this.Amount);},function(key,values){return
Array.sum(values)},{query:{Status:"A"},out:"XYZ"});
{
"result" : "XYZ",
"timeMillis" : 369,
"counts" : {
"input" : 3,
"emit" : 3,
"reduce" : 1,
"output" : 2
},
"ok" : 1
}
> db.XYZ.find();
{ "_id" : "A123", "value" : 750 }
{ "_id" : "B123", "value" : 350 }
> db.orders.drop();
true > exit
Bye
Practical No : 12
Roll No : 12 Class : TE
Problem Statement : Write a program to implement MogoDB database connectivity with PHP
Code :
<?php
// connect to mongodb $m
= new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
?>
Output :
Connection to database successfully
Database mydb selected
<?php
// connect to mongodb $m
= new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->createCollection("mycol"); echo
"Collection created succsessfully";
?>
Output :
Connection to database successfully
Database mydb selected
Collection created succsessfully
<?php
// connect to mongodb $m
= new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol; echo
"Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" => "http://www.tutorialspoint.com/mongodb/",
"by" => "tutorials point"
);
$collection->insert($document); echo
"Document inserted successfully";
?>
Output :
Connection to database successfully
Database mydb selected
Collection selected successfully
Document inserted successfully
<?php
// connect to mongodb $m = new
MongoClient(); echo "Connection to
database successfully";
// select a database
$db = $m->mydb; echo "Database mydb
selected"; $collection = $db->mycol;
echo "Collection selected
succsessfully";
// now update the document
$collection->update(array("title"=>"MongoDB"),
array('$set'=>array("title"=>"MongoDB Tutorial")));
echo "Document updated successfully";
// now display the updated document
$cursor = $collection->find();
// iterate cursor to display title of documents
echo "Updated document"; foreach ($cursor
as $document) { echo $document["title"] .
"\n";
}
?>
Output :
Connection to database successfully
Database mydb selected
Collection selected successfully
Document updated successfully
Updated document Delete
database / values :
<?php
// connect to mongodb $m
= new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb; echo "Database mydb
selected"; $collection = $db->mycol;
echo "Collection selected
succsessfully";
// now remove the document
$collection->remove(array("title"=>"MongoDB Tutorial"),false);
echo "Documents deleted successfully";
// now display the available documents
$cursor = $collection->find();
// iterate cursor to display title of documents
echo "Updated document"; foreach
($cursor as $document) { echo
$document["title"] . "\n";
}?>
Output :
Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully
Updated document