Вы находитесь на странице: 1из 2

A Hospital keeps records of doctors belonging to a department.

The attributes of
Doctor are DNO,DNAME,GENDER,AGE and SALARY.The attributes of department are DEPTNO
and DEPTNAME.A department can have more than one doctor.
----------------------------------------------------------------------------

CREATE TABLE DEPT(DEPTNO NUMBER(5) PRIMARY KEY,DEPTNAME VARCHAR2(20));

db.createCollection("DEPT" ,{validator:{$jsonSchema:{bsonType: "object",required: [


"DEPTNO","DEPTNAME"],
properties:{DEPTNO:{ bsonType: "number" }, DEPTNAME:{ bsonType: "string" }}}}})
-------------------------------------------------------------------------------

CREATE TABLE DOCTOR(DNO NUMBER(5) PRIMARY KEY,DNAME VARCHAR2(20),GENDER CHAR


CHECK(GENDER IN('M','F')),
AGE NUMBER(2),SALARY NUMBER(6),DEPTNO NUMBER(5),FOREIGN KEY(DEPTNO) REFERENCES
DEPT(DEPTNO));

db.createCollection("DOCTOR" , { validator: { $jsonSchema: { bsonType: "object",


required:
[ "DNO","DNAME","GENDER","AGE","SALARY","DEPTNO"], properties: { DNO: { bsonType:
"number" },
DNAME: { bsonType: "string" }, GENDER: { enum: [ "M", "F" ] },AGE:
{bsonType:"number"},
SALARY:{ bsonType: "number"},DEPTNO:{ bsonType:"number"} }}}})
--------------------------------------------------------------------------------

INSERT INTO DEPT VALUES(101,'ONCOLOGY');


INSERT INTO DEPT VALUES(102,'ORTHOLOGY');
INSERT INTO DEPT VALUES(103,'NEUROLOGY');

db.DEPT.insert({"DEPTNO":101,"DEPTNAME": "ONCOLOGY"})
db.DEPT.insert({"DEPTNO":102,"DEPTNAME": "ORTHOLOGY"})
db.DEPT.insert({"DEPTNO":103,"DEPTNAME": "NEUROLOGY"})
--------------------------------------------------------------------------------

INSERT INTO DOCTOR VALUES(201,'Jon','M',36,10000,101);


INSERT INTO DOCTOR VALUES(202,'Rob','M',42,25000,102);
INSERT INTO DOCTOR VALUES(203,'Arya','F',48,20000,101);
INSERT INTO DOCTOR VALUES(204,'Bran','M',39,30000,103);
INSERT INTO DOCTOR VALUES(205,'Ani','F',52,15000,102);

db.DOCTOR.insert({DNO:201,DNAME:"Jon",GENDER:"M",AGE:36,SALARY:10000,DEPTNO:101})
db.DOCTOR.insert({DNO:202,DNAME:"Rob",GENDER:"M",AGE:42,SALARY:25000,DEPTNO:102})
db.DOCTOR.insert({DNO:203,DNAME:"Arya",GENDER:"F",AGE:48,SALARY:20000,DEPTNO:101})
db.DOCTOR.insert({DNO:204,DNAME:"Bran",GENDER:"M",AGE:39,SALARY:30000,DEPTNO:103})
db.DOCTOR.insert({DNO:205,DNAME:"Ani",GENDER:"F",AGE:52,SALARY:15000,DEPTNO:102})
-----------------------------------------------------------------------------------
SELECT * FROM DEPT;
db.DEPT.find()
--------------------------------------------------------
SELECT * FROM DOCTOR;
db.DOCTOR.find()
--------------------------------------------------------
SELECT * FROM DOCTOR WHERE DNO = 201;
db.DOCTOR.find({DNO:201})
---------------------------------------------------------
SELECT * FROM DOCTOR WHERE Age >= 40 AND Age <= 50;
db.DOCTOR.find({ AGE:{$gte:40, $lte:50}})
---------------------------------------------------------
SELECT * FROM DOCTOR WHERE GENDER="F" AND SALARY < 20000;
db.DOCTOR.find( { GENDER: "F", SALARY: { $lt: 20000 }})
---------------------------------------------------------
SELECT * FROM DOCTOR WHERE DNAME LIKE "A%";
db.DOCTOR.find({DNAME: /^A/ })
----------------------------------------------------------
SELECT DNAME, SALARY FROM DOCTOR WHERE GENDER = "M";
db.DOCTOR.find({ GENDER: "M" },{ DNAME: 1,SALARY: 1})
-----------------------------------------------------------
UPDATE DOCTOR SET SALARY=25000 WHERE DNO=205;
db.DOCTOR.updateOne({DNO:205},{$set:{SALARY:25000}})
-----------------------------------------------------------
db.DOCTOR.updateMany({GENDER:"F"},{$inc:{SALARY:10000}})
UPDATE DOCTOR SET SALARY=SALARY+10000 WHERE GENDER="F";
----------------------------------------------------------
SELECT GENDER,COUNT(*) FROM DOCTOR GROUP BY GENDER;
db.DOCTOR.aggregate({ $group: {_id:'$GENDER', count: {$sum:1}}})
----------------------------------------------------------
SELECT AVG(SALARY) FROM DOCTOR;
db.DOCTOR.aggregate({ $group:{_id:null,Avg_Salary:{ $avg:'$SALARY'}}})
-----------------------------------------------------------
SELECT GENDER,Sum(SALARY) FROM DOCTOR GROUP BY GENDER;
db.DOCTOR.aggregate({$group:{_id:'$GENDER',Total:{$sum:'$SALARY'}}})
-------------------------------------------------------------
SELECT * FROM DOCTOR
LEFT JOIN DEPT
WHERE DOCTOR.DEPTNO=DEPT.DEPTNO;

db.DOCTOR.aggregate(
{ $lookup:{
from:"DEPT",
localField:"DEPTNO",
foreignField:"DEPTNO",
as:"DocDept"
}
})
---------------------------------------------------------------
DELETE FROM DEPT;
db.DEPT.deleteMany({})
---------------------------------------------------------------
DELETE FROM DOCTOR WHERE AGE>=45;
db.DOCTOR.deleteMany({AGE:{$gte:45}})
---------------------------------------------------------------