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

Code Part

The document describes creating SQL objects like tables, views, indexes and constraints. It shows the SQL code for creating tables, inserting and updating data, creating indexes and altering tables. The code demonstrates basic CRUD operations and managing SQL objects.

Uploaded by

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

Code Part

The document describes creating SQL objects like tables, views, indexes and constraints. It shows the SQL code for creating tables, inserting and updating data, creating indexes and altering tables. The code demonstrates basic CRUD operations and managing SQL objects.

Uploaded by

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

Practical No : 01

Roll No : 104 Class : TE


Problem Statement : Propose a Conceptual Design using ER features using tools like
ERD plus, ER Win etc. (Identifying entities, relationships between entities, attributes, keys,
cardinalities, generalization, specialization etc.) Convert the ER diagram into relational
tables and normalize Relational data model.

Office Table:
Office ID Office Square
City Footage

Customer Agencies Table:


Agency ID Agency Address Agency city Phone No
Name

Rental Agreements Table:


Rental ID Rent Amount End date
Practical No : 2
Roll No : 12 Class : TE

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 :

archu@archu-virtual-machine:~$ sudo mysql


[sudo] password for archu:
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)

Copyright (c) 2000, 2022, 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 archu


-> ;
Query OK, 1 row affected (0.05 sec)

mysql> use archu


Database changed
mysql> create table client_master(client_no int,client_name varchar(20),address
varchar(50),city varchar(20),pincode int,state varchar(20),bal_due float,primary
key(client_no)); Query OK, 0 rows affected (0.13 sec)

mysql> insert into client_master


values(01,'archana','Garakole','Garakole',1043106,'MH',50000); Query OK, 1 row affected
(0.03 sec)

mysql> insert into client_master values(02,'rutuja','Ganeshgav','Ganeshgav',1043102,'MH',60000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into client_master


values(03,'Shivanjali','Shelgaon','Shelgaon',1043114,'MH',70000); Query OK, 1 row affected (0.02
sec)
mysql> insert into client_master values(04,'Pallavi','Survad','Survad',1043101,'MH',80000);
Query OK, 1 row affected (0.02 sec)

mysql> insert into client_master values(05,'Rohini','Solapur','Solapur',1043133,'MH',40000);


Query OK, 1 row affected (0.03 sec)

mysql> select * from client_master; +-----------+-------------+----------+----------+---------+-------+---


------+
| client_no | client_name | address | city | pincode | state | bal_due |
+-----------+-------------+----------+----------+---------+-------+---------+
| 1 | archana | Garakole | Garakole | 1043106 | MH | 50000 |
| 2 | Rutuja | Ganeshgav| Ganeshgav | 1043102 | MH | 60000 |
| 3 | Shivanjali | Shelgaon | Shelgaon | 1043114 | MH | 70000 |
| 4 | Pallavi | Survad | Survad | 1043101 | MH | 80000 |
| 5 | Rohini | Solapur | Solapur | 1043133 | MH | 40000 |
+-----------+-------------+----------+----------+---------+-------+---------+
5 rows in set (0.02 sec)

mysql> select client_name,client_no from client_master;


+-------------+-----------+
| client_name | client_no |
+-------------+-----------+
| archana | 1|
| rutuja | 2|
| Shivanjali | 3|
| Pallavi | 4|
| Rohini | 5|
+-------------+-----------+
5 rows in set (0.00 sec)

mysql> insert into client_master values(06,'ashwini','Shreepur','Shreepur',1043102,'MH',50000);


Query OK, 1 row affected (0.03 sec)

mysql> select client_name,client_no from client_master;


+-------------+-----------+
| client_name | client_no |
+-------------+-----------+
| archana | 1|
| rutuja | 2|
| Shivanjali | 3|
| | 4|
| Rohini | 5|
| ashwini | 6|
+-------------+-----------+
6 rows in set (0.00 sec)

mysql> create table product_master(product_no int,description varchar(20),profit_per


float,unit_measure varchar(10),quantity int,recorder int,sell_price float,cost_price float,primary
key(product_no));
Query OK, 0 rows affected (0.05 sec)

mysql> desc product_master;


+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| product_no | int | NO | PRI | NULL | |
| description | varchar(20) | YES | | NULL | |
| profit_per | float | YES | | NULL | |
| unit_measure | varchar(10) | YES | | NULL | |
| quantity | int | YES | | NULL | |
| recorder | int | YES | | NULL | |
| sell_price | float | YES | | NULL | |
| cost_price | float | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
8 rows in set (0.05 sec)

mysql> insert into product_master values(01,'KRANTIpoo',1,'one',4,2,10,15);


Query OK, 1 row affected (0.25 sec)

mysql> insert into product_master values(02,'Oil',13,'one',4,2,11,16);


Query OK, 1 row affected (0.01 sec)

mysql> alter table client_master add telephone_ni int;


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

mysql> select * from client_master;


+-----------+-------------+----------+----------+---------+-------+---------+--------------+
| client_no | client_name | address | city | pincode | state | bal_due | telephone_ni |
+-----------+-------------+----------+----------+---------+-------+---------+--------------+
| 1 | archana | Garakole| Garakole | 1043106 | MH | 50000 | NULL |
| 2 | rutuja | Ganeshgav | Ganeshgav| 1043102 | MH | 60000 | NULL |
| 3 | Shivanjali | Shelgaon | Shelgaon | 1043114 | MH | 70000 | NULL |
| 4 | Pallavi | Survad | Survad | 1043101 | MH | 80000 | NULL |
| 5 | Rohini | Solapur | Solapur | 1043133 | MH | 40000 | NULL |
| 6 | ashwini | Shreepur | Shreepur | 1043102 | MH | 50000 | NULL |
+-----------+-------------+----------+----------+---------+-------+---------+--------------+
6 rows in set (0.02 sec)

mysql> select * from product_master;


+------------+-------------+------------+--------------+----------+----------+------------+------------+
| product_no | description | profit_per | unit_measure | quantity | recorder | sell_price | cost_price |
+------------+-------------+------------+--------------+----------+----------+------------+------------+
| 1 | KRANTIpoo | 1 | one | 4| 2| 10 | 15 |
| 2 | Oil | 13 | one | 4| 2| 11 | 16 |
+------------+-------------+------------+--------------+----------+----------+------------+------------+
2 rows in set (0.02 sec)

mysql> create index client_search on client_master(client_no);


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

mysql> create table auto(roll_n int NOT NULL AUTO_INCREMENT,name varchar(20),primary


key(roll_n));
Query OK, 0 rows affected (0.04 sec)

mysql> select * from auto;


Empty set (0.02 sec)

mysql> insert into auto values(1,'anju'); Query


OK, 1 row affected (0.00 sec)

mysql> insert into auto values(2,'manju');


Query OK, 1 row affected (0.01 sec)

mysql> insert into auto values(3,'meet');


Query OK, 1 row affected (0.00 sec)

mysql> alter table auto auto_increment=100;


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

mysql> select * from auto;


+--------+--------+
| roll_n | name |
+--------+--------+
| 1 | anju |
| 2 | Rani |
| 3 | meet |
+--------+--------+ 3
rows in set (0.00 sec)

mysql> insert into auto values(null,'Ajit'); Query


OK, 1 row affected (0.00 sec)

mysql> select * from auto;


+--------+--------+
| roll_n | name |
+--------+--------+
| 1 | anju |
| 2 | Rani |
| 3 | meet |
| 100 | Ajit|
+--------+--------+ 4
rows in set (0.00 sec)

mysql> update client_master set client_name='Kittu' where client_no=4;


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

mysql> select * from client_master;


+-----------+-------------+----------+----------+---------+-------+---------+--------------+
| client_no | client_name | address | city | pincode | state | bal_due | telephone_ni | +-----------+---
----------+----------+----------+---------+-------+---------+--------------+
| 1 | archana | Garakole | Garakole | 1043106 | MH | 50000 | NULL |
| 2 | rutuja | Ganeshgav | Ganeshgav | 1043102 | MH | 60000 | NULL |
| 3 | Shivanjali | Shelgaon | Shelgaon | 1043114 | MH | 70000 | NULL |
| 4 | Kittu | Akluj | Akluj | 1043101 | MH | 80000 | NULL |
| 5 | Rohini | Solapur | Solapur | 1043133 | MH | 40000 | NULL |
| 6 | ashwini | Shreepur | Shreepur | 1043102 | MH | 50000 | NULL |
+-----------+-------------+----------+----------+---------+-------+---------+--------------+
6 rows in set (0.01 sec)

mysql> create index client_find on client_master(client_name,city);


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

mysql> show tables;


+------------------
+|
Tables_in_archu
|
+------------------+
| auto |
| client_master |
| product_master |
+------------------+ 3
rows in set (0.07 sec)

mysql> select * from product_master;


+------------+-------------+------------+--------------+----------+----------+------------+------------+
| product_no | description | profit_per | unit_measure | quantity | recorder | sell_price | cost_price |
+------------+-------------+------------+--------------+----------+----------+------------+------------+
| 1 | KRANTIpoo | 1 | one | 4| 2| 10 | 15 |
| 2 | Oil | 13 | one | 4| 2| 11 | 16 |
+------------+-------------+------------+--------------+----------+----------+------------+------------+
2 rows in set (0.00 sec)

mysql> alter table client_master rename to c_name;


Query OK, 0 rows affected (0.02 sec)

mysql> alter table product_master rename to p_master;


Query OK, 0 rows affected (0.05 sec)

mysql> insert into p_master values(3,'Books',15,'Three',40,5,110,123);


Query OK, 1 row affected (0.03 sec)

mysql> alter table p_master modify sell_price float(10,2);


Query OK, 0 rows affected, 1 warning (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 1

mysql> desc p_master;


+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------
+-------+
| product_no | int | NO | PRI | NULL | |
| description | varchar(20) | YES | | NULL | |
| profit_per | float | YES | | NULL | |
| unit_measure | varchar(10) | YES | | NULL | |
| quantity | int | YES | | NULL |
| | recorder | int | YES | | NULL |
|
| sell_price | float(10,2) | YES | | NULL | |
| cost_price | float | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
8 rows in set (0.04 sec)

mysql> create view client as select client_no,client_name from c_name;


Query OK, 0 rows affected (0.04 sec)

mysql> select * from c_name;


+-----------+-------------+----------+----------+---------+-------+---------+--------------+
| client_no | client_name | address | city | pincode | state | bal_due | telephone_ni |
+-----------+-------------+----------+----------+---------+-------+---------+--------------+
| 1 | archana | Garakole | Garakole | 1043106 | MH | 50000 | NULL |
| 2 | rutuja | Ganeshgav| Ganeshgav | 1043102 | MH | 60000 | NULL |
| 3 | Shivanjali | Shelgaon | Shelgaon | 1043114 | MH | 70000 | NULL |
| 4 | Kittu | Akluj | Akluj | 1043101 | MH | 80000 | NULL |
| 5 | Rohini | Solapur | Solapur | 1043133 | MH | 40000 | NULL |
| 6 | ashwini | Shreepur | Shreepur | 1043102 | MH | 50000 | NULL |
+-----------+-------------+----------+----------+---------+-------+---------+--------------+
6 rows in set (0.00 sec)

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)

Copyright (c) 2000, 2022, 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> show databases;


+--------------------+
| Database | +--------------------
+
| information_schema |
| mysql |
| archu |
| archu |
| performance_schema |
| sys |
+--------------------+ 6
rows in set (0.01 sec)

mysql> use archu;


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 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> insert into capital values(02,'Bihar',02);


Query OK, 1 row affected (0.04 sec)
mysql> insert into capital values(03,'Goa',03);
Query OK, 1 row affected (0.04 sec)

mysql> insert into capital values(04,'Gujrat',04);


Query OK, 1 row affected (0.04 sec)

mysql> insert into capital values(05,'karnataka',05);


Query OK, 1 row affected (0.03 sec)

mysql> insert into state values(01,'Asam',01,'Dispur');


Query OK, 1 row affected (0.04 sec)

mysql> insert into state values(02,'Bihar',02,'Patna');


Query OK, 1 row affected (0.01 sec)

mysql> insert into state values(03,'Goa',03,'Panaji');


Query OK, 1 row affected (0.01 sec)

mysql> insert into state values(04,'Gujarat',04,'Gandhinagar');


Query OK, 1 row affected (0.04 sec)

mysql> insert into state values(05,'Karnataka',05,'Bengaluru');


Query OK, 1 row affected (0.01 sec)

mysql> select * from capital; +--------+-----------+----------


+
| cap_no | cap_name | state_no |
+--------+-----------+----------+
| 1 | Asam | 1|
| 2 | Bihar | 2|
| 3 | Goa | 3|
| 4 | Gujrat | 4|
| 5 | karnataka | 5|
+--------+-----------+----------+
5 rows in set (0.03 sec)

mysql> select * from state;


+----------+------------+------------+-------------+
| state_no | state_name | state_code | capital |
+----------+------------+------------+-------------+
| 1 | Asam | 1 | Dispur |
| 2 | Bihar | 2 | Patna
|| 3 | Goa | 3 | Panaji
|
| 4 | Gujarat | 4 | Gandhinagar |
| 5 | Karnataka | 5 | Bengaluru |
+----------+------------+------------+-------------+
5 rows in set (0.00 sec)

mysql> select capital.cap_no, state.state_no from capital inner join state on


capital.cap_no=state.state_no;
+--------+----------+
| cap_no | state_no |
+--------+----------+
| 1| 1|
| 2| 2|
| 3| 3|
| 4| 4|
| 5| 5|
+--------+----------+ 5
rows in set (0.01 sec)

mysql> UPDATE state SET state_no="78" where state_no='1';


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

mysql> UPDATE state SET state_no="58" where state_no='2';


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

mysql> UPDATE state SET state_no="46" where state_no='3';


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

mysql> UPDATE state SET state_no="489" where state_no='4';


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

mysql> UPDATE state SET state_no="458" where state_no='5';


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

mysql> insert into state values('05','MP','05','BHO');


Query OK, 1 row affected (0.03 sec)

mysql> select capital.cap_no, state.state_no from capital inner join state on


capital.cap_no=state.state_no;
+--------+----------+
| cap_no | state_no |
+--------+----------+
| 5| 5|
+--------+----------+
1 row in set (0.00
sec) mysql> select
capital.cap_no,
state.state_no from
capital left join state
on
capital.cap_no=state
.state_no;
+--------+----------+
| cap_no | state_no |
+--------+----------
+ | 1 | NULL
|
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5| 5|
+--------+----------+ 5
rows in set (0.03 sec)
mysql> select capital.cap_no, state.state_no from capital left join state on
capital.cap_no=state.state_name;
+--------+----------+
| cap_no | state_no |
+--------+----------
+ | 1 | NULL
|
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
+--------+----------+
5 rows in set, 5 warnings (0.01 sec)

mysql> select capital.cap_no, state.state_no from capital right join state on


capital.cap_no=state.state_no;
+--------+----------+
| cap_no | state_no |
+--------+----------+
| 5| 5|
| NULL | 46 |
| NULL | 58 |
| NULL | 78 |
| NULL | 458 |
| NULL | 489 |
+--------+----------+ 6
rows in set (0.01 sec)

mysql> select * from capital; +--------+-----------+----------


+
| cap_no | cap_name | state_no |
+--------+-----------+----------+
| 1 | Asam | 1
| | 2 | Bihar | 2
|
| 3 | Goa | 3|
| 4 | Gujrat | 4|
| 5 | karnataka | 5|
+--------+-----------+----------+
5 rows in set (0.00 sec)

mysql> select * from state;


+----------+------------+------------+-------------+
| state_no | state_name | state_code | capital |
+----------+------------+------------+-------------+
| 5 | MP | 5 | BHO |
| 46 | Goa | 3 | Panaji |
| 58 | Bihar | 2 | Patna |
| 78 | Asam | 1 | Dispur |
| 458 | Karnataka | 5 | Bengaluru |
| 489 | Gujarat | 4 | Gandhinagar |
+----------+------------+------------+-------------+
6 rows in set (0.00 sec)

mysql> select * from capital;


+--------+-----------+----------+
| cap_no | cap_name | state_no |
+--------+-----------+----------+
| 1 | Asam | 1|
| 2 | Bihar | 2|
| 3 | Goa | 3|
| 4 | Gujrat | 4|
| 5 | karnataka | 5|
+--------+-----------+----------+
5 rows in set (0.00 sec)

mysql> select capital.cap_no, state.state_no from capital inner join state on


capital.cap_no=state.state_no;
+--------+----------+
| cap_no | state_no |
+--------+----------+
| 5| 5|
+--------+----------+
1 row in set (0.01
sec)

mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no from capital inner


join state on capital.cap_no=state.state_no; +--------+-----------+---------+----------+
| cap_no | cap_name | capital | state_no |
+--------+-----------+---------+----------+
| 5 | karnataka | BHO | 5|
+--------+-----------+---------+----------+
1 row in set (0.00 sec)
mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no from capital inner
join state on capital.cap_no=state.state_no; +--------+-----------+---------+----------+
| cap_no | cap_name | capital | state_no |
+--------+-----------+---------+----------+
| 5 | karnataka | BHO | 5|
+--------+-----------+---------+----------+
1 row in set (0.00 sec)

mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no from capital right


join state on capital.cap_no=state.state_no; +--------+-----------+-------------+----------+
| cap_no | cap_name | capital | state_no |
+--------+-----------+-------------+----------
+ | 5 | karnataka | BHO | 5|
| NULL | NULL | Panaji | 46 |
| NULL | NULL | Patna | 58 |
| NULL | NULL | Dispur | 78 |
| NULL | NULL | Bengaluru | 458 |
| NULL | NULL | Gandhinagar | 489 |
+--------+-----------+-------------+----------+
6 rows in set (0.00 sec)

mysql> select * from capital c1, state s1 where c1.cap_no=s1.state_no;


+--------+-----------+----------+----------+------------+------------+---------+
| cap_no | cap_name | state_no | state_no | state_name | state_code | capital |
+--------+-----------+----------+----------+------------+------------+---------+
| 5 | karnataka | 5| 5 | MP | 5 | BHO |
+--------+-----------+----------+----------+------------+------------+---------+
1 row in set (0.00 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> select * from state where state_no=(select capital.state_no from capital


where cap_name='KAR'); Empty set (0.00 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

SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 13 16:34:03 2022

Copyright (c) 1982, 2011, Oracle. All rights reserved.

Enter user-name: studentb


Enter password:

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> CREATE TABLE BORROWER(ROLL_NO NUMBER,NAME


VARCHAR(20),DATEOFISSUE DATE,NAMEOFBOOK VARCHAR(20),STATUS
VARCHAR(20));

Table created.

SQL> INSERT INTO BORROWER VALUES(1,'ARCHANA','17-06-2001','TE','I');

1 row created.

SQL> INSERT INTO BORROWER VALUES(2,'PRANALI,'22-04-2000','TE','I');

1 row created.

SQL> INSERT INTO BORROWER VALUES(3,'MADHURI','26-11-2002','SE','I');

1 row created.

SQL> SELECT * FROM BORROWER;


ROLL_NO NAME DATEOFIS NAMEOFBOOK
---------- -------------------- -------- --------------------
STATUS
--------------------
1 ARCHANA 17-06-01 TE
I

2 PRANALI 22-04-00 TE
I
3 MADHURI 26-11-02 SE
I

SQL> CREATE TABLE FINE(ROLL_NO NUMBER,SDATE DATE,AMT NUMBER);

Table created.

SQL> DESC FINE;


Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO NUMBER
SDATE DATE
AMT NUMBER

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> SELECT * FROM BORROWER;

ROLL_NO NAME DATEOFIS NAMEOFBOOK


---------- -------------------- -------- --------------------
STATUS
--------------------
1 ARCHANA 17-06-01 TE
I
2 PRANALI 22-04-00 TE
I
3 MADHURI 26-11-02 SE

SQL> SELECT * FROM FINE;

ROLL_NO SDATE AMT


------------------ ------------------- ---------------
1 11-10-22 97950

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

SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 13 19:17:53 2022

Copyright (c) 1982, 2011, Oracle. All rights reserved.

Enter user-name: studentb Enter


password:

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> CREATE TABLE STUD_MARKS(NAME VARCHAR(10),TOTAL_MARKS


NUMBER);

Table created.

SQL> CREATE TABLE RESULT(ROLL NUMBER,NAME VARCHAR(20),CLASS


VARCHAR(20));

Table created.

SQL> INSERT INTO STUD_MARKS VALUES('NEHA',1000);

1 row created.

SQL> INSERT INTO STUD_MARKS VALUES('KAVITA',800);

1 row created.

SQL> INSERT INTO STUD_MARKS VALUES('PRIYANKA',700);

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 /

PL/SQL procedure successfully completed.

SQL> SELECT * FROM STUD_MARKS;

NAME TOTAL_MARKS
---------- ----------- -----------
ARCHANA 1000
PRANALI 800
MADHURI 700

SQL> SELECT * FROM RESULT;

ROLL NAME CLASS


-------------- -------------------------- -------------------------------
1 ARCHANA DISTINCTION
2 PRANALI FIRST
3 MADHURI HIGHER SECOND

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

SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 13 19:35:19 2022

Copyright (c) 1982, 2011, Oracle. All rights reserved.

Enter user-name: studentb Enter


password:

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> CREATE TABLE O_ROLLCALL(ROLL NUMBER,NAME VARCHAR(10));

Table created.

SQL> INSERT INTO O_ROLLCALL VALUES(3,'TANU');

1 row created.

SQL> INSERT INTO O_ROLLCALL VALUES(1,'KRANTI');

1 row created.

SQL> INSERT INTO O_ROLLCALL VALUES(2,'KAVITA');

1 row created.

SQL> SELECT * FROM O_ROLLCALL;

ROLL NAME
---------- ----------
3 TANU
1 KRANTI
2 KAVITA
SQL> CREATE TABLE N_ROLLCALL(ROLL_NO NUMBER,NAME VARCHAR(10));

Table created.

SQL> INSERT INTO N_ROLLCALL VALUES(1,'KRANTI');

1 row created.

SQL> INSERT INTO N_ROLLCALL VALUES(2,'TANU');

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;

PL/SQL procedure successfully completed.

SQL> SELECT * FROM N_ROLLCALL;

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 :

archu@archu-virtual-machine:~$ sudo service oracle-xe start;


[sudo] password for archu:
archu@archu-virtual-machine:~$ sqlplus

SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 13 19:57:04 2022

Copyright (c) 1982, 2011, Oracle. All rights reserved.

Enter user-name: studentb Enter


password:

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> drop table library;

Table dropped.

SQL> drop table library_audit;

Table dropped.

SQL> CREATE TABLE LIBRARY(BNO NUMBER,BNAME VARCHAR(20),AUTHOR


VARCHAR2(20),ALLOWED_DAYS NUMBER);

Table created.

SQL> INSERT INTO LIBRARY VALUES(1,'DBMS','KORTH',20);

1 row created.
SQL> INSERT INTO LIBRARY VALUES(2,'CNS','TANEBAUM',13);

1 row created.

SQL> INSERT INTO LIBRARY VALUES(3,'SPOS','THOMAS',5);

1 row created.

SQL> SELECT * FROM LIBRARY;

BNO BNAME AUTHOR ALLOWED_DAYS


---------- -------------------- -------------------- ------------
1 DBMS KORTH 20
2 CNS TANEBAUM 13
3 SPOS THOMAS 5

SQL> CREATE TABLE LIBRARY_AUDIT(BNO NUMBER,OLD_ALL_DAYS NUMBER,


NEW_ALL_DAYS NUMBER);

Table created.

SQL> DESC LIBRARY_AUDIT;


Name Null? Type
----------------------------------------- -------- ----------------------------
BNO NUMBER
OLD_ALL_DAYS NUMBER
NEW_ALL_DAYS NUMBER

SQL> CREATE OR REPLACE TRIGGER TRG


2 BEFORE UPDATE OR DELETE ON LIBRARY
3 FOR EACH ROW
4 BEGIN
5 INSERT INTO LIBRARY_AUDIT
6 VALUES(:NEW.BNO,:OLD.ALLOWED_DAYS,:NEW.ALLOWED_DAYS);
7 END;
8 /
Trigger created.

SQL> UPDATE LIBRARY SET ALLOWED_DAYS = 25 WHERE BNO = 1;

1 row updated.
SQL> SELECT * FROM LIBRARY_AUDIT;

BNO OLD_ALL_DAYS NEW_ALL_DAYS


---------- ------------ ------------
1 20 25

SQL> SELECT * FROM LIBRARY;

BNO BNAME AUTHOR ALLOWED_DAYS


---------- -------------------- -------------------- ------------
1 DBMS KORTH 25
2 CNS TANEBAUM 13
3 SPOS THOMAS 5
SQL> DELETE FROM LIBRARY WHERE BNO = 2;

1 row deleted.

SQL> SELECT * FROM LIBRARY;

BNO BNAME AUTHOR ALLOWED_DAYS


---------- -------------------- -------------------- ------------
1 DBMS KORTH 25
3 SPOS THOMAS 5

SQL> SELECT * FROM LIBRARY_AUDIT;

BNO OLD_ALL_DAYS NEW_ALL_DAYS


---------- ------------ ------------
1 20 25
13

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.*;

public class crudoperations {

public static void main(String[] args) {


try
{

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;

case 2: String sql1 = "SELECT name,age from stud";


ResultSet rs = sm.executeQuery(sql1); while(rs.next())
{
String name1 = rs.getString("name");
int rno1 = rs.getInt("rno"); System.out.println("name
"+name1);
System.out.println("age "+rno1);
}
break;
case 3: sql1 = "update stud set name = 'Mohan' where name ='Aniket'";
sm.executeUpdate(sql1);
System.out.println("Record is updated"); break;

case 4: sql1 = "delete from stud where rno = 1";


sm.executeUpdate(sql1); System.out.println("Record is deleted");
break;
}
}
while(ch<5);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output :
Roll No : 12 Practical No : 09
Class : TE

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 client = session._getSessionAwareClient(); const readConcern =


client.getReadConcern(session); if (readConcern !== null &&
client.canUseReadConcern(session, cursor._convertToCommand(true))) {
cursor.readConcern(readConcern.level);
}
}
return cursor;
}
> 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 }
> db.Mycoll.insert({Roll:1,Name:"",Addr:"Barshi",Ae:19});
WriteResult({ "nInserted" : 1 })
> db.Mycoll.drop; function(options
= {}) {
const cmdObj = Object.assign({drop: this.getName()}, options);
ret = this._db.runCommand(cmdObj);
if (!ret.ok) { if (ret.errmsg == "ns not found")
return false; throw _getErrorWithCode(ret, "drop failed: "
+ tojson(ret));
}
return true; }
> use archu;
switched to db archu
> db.createCollection("Mycoll");
{
"ok" : 0,
"errmsg" : "Collection already exists. NS: archu.Mycoll",
"code" : 48,
"codeName" : "NamespaceExists"
}
> 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 client = session._getSessionAwareClient();


const readConcern = client.getReadConcern(session);
if (readConcern !== null &&
client.canUseReadConcern(session,
cursor._convertToCommand(true))) {
cursor.readConcern(readConcern.level);
}
}

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

Problem Statement : Implement Map reduces operation using MongoDB.

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 :

For Connection Purpose :

<?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

Create database Purpose :-

<?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

Insert values in database :

<?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

Update Values in database :

<?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

You might also like