Practical File: CO 202: Database Management Systems

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Practical File

CO 202: Database Management Systems

Submitted To: Submitted by:


Ms. Indu Singh Rahul (2K18/CO/269)
Assistant Professor Batch - A4, Group – G3
CSE Department, DTU
Table of Contents

S. No. Title Date Sign


1 Introduction to STL, Database and Database 10-Jan-2020
Management System
2 Various DBMS Software and Database Languages 17-Jan-2020
3 Introduction to E-R Diagrams and Symbol table 07-Feb-2020
4 Introduction to different Types of Constraints in SQL 21-Feb-2020
5 Case Study: Hospital Management System 28-Feb-2020
6 E-R diagram, constructing entity tables and relationship 27-Mar-2020
tables
7 To implement various DDL and DML statements 03-Apr-2020
8 To perform various Simple Queries 10-Apr-2020
9 To implement various Joins’ Queries 17-Apr-2020
10 To implement various Aggregate Functions and Date 24-Apr-2020
and Time Functions
11 To implement Views, Triggers, and Grant and Revoke 29-Apr-2020
commands
Experiment - 1
Aim: Introduction to SQL, Database and Database Management Systems

Theory:
SQL (Standard Query Language): SQL is a standard language for accessing and
manipulating databases. SQL lets you access and manipulate databases

Operations of SQL:
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
• SQL can create new user-defined constraints for the database

Some SQL commands are:


• SELECT: It extracts data from a database
• UPDATE: It updates data in a database
• DELETE: It deletes data from a database
• INSERT INTO: It inserts new data into a database
• CREATE DATABASE: It creates a new database
• ALTER DATABASE: It modifies a database
• CREATE TABLE: It creates a new table
• ALTER TABLE: It modifies a table
• DROP TABLE: It deletes a table
• CREATE INDEX: It creates an index (search key)
• DROP INDEX: It deletes an index

Database:
• It is a collection of interrelated data stored together.
• Example: The information relevant to an enterprise.
• A database has the following implicit properties:
▪ It represents some aspect of the real world
▪ It is a logically coherent collection of data
▪ It has an audience that is actively interested in its contents
• A database can of any size and complexity, for example: a list of names of students in
class, the users on Facebook or the items on Amazon etc.
• A database may be generated and maintained manually or it may be computerized. A
computerized database may be created and maintained either by a group of
application programs written specifically for that task or by a Database management
system.

Database Management System:


• It is a general-purpose software system used to create and maintain a database.
• It facilitates the processes of defining, constructing, manipulating, and sharing
databases among various users and applications along with proper protection of data.
▪ Defining a database involves specifying the data types, structures, and constraints
of the data to be stored in the database
▪ Constructing the database is the process of storing the data on some storage
medium that is controlled by the DBMS
▪ Manipulating a database includes functions such as querying the database to
retrieve specific data, updating the database, and generating reports from the data.
▪ Sharing a database allows multiple users and programs to access the database
simultaneously.
▪ Protection includes system protection against hardware or software malfunction
and security protection against unauthorized or malicious access.

Operations of DBMS:
• Adding new files to database
• Removing file from database
• Inserting new data into existing data files
• Retrieving data from existing files
• Changing data in existing files
• Deleting data in existing files
Experiment - 2
Aim: Various DBMS Software and Database Languages

Theory:
A database is a collection of information that is organized in tables and stored on a
computer system. This information can be updated or modified as required. A database
management system (DBMS) is a software for creating and managing data in the databases.
The DBMS provides users and programmers a defined process for data retrieval,
management, updating, and creation.

Various Database Management Software are:


1. Oracle: Oracle has been making database products since 1979 and is one of the most well-
recognized manufacturers worldwide.
• It’s powerful but complex database. New users will want to invest in solid training to
ensure they’re getting the most from the software. Oracle is also embracing the cloud.
• Oracle DB runs on most major platforms, including Windows, UNIX, Linux and Mac OS.
• Its architecture is split between the logical and the physical. This structure means that
for large-scale distributed computing, also known as grid computing, the data location
is irrelevant and transparent to the user.
• It allows a more modular physical structure that can be added to and altered without
affecting the activity of the database, its data or users.
• The sharing of resources in this way allows for very flexible data networks whose
capacity can be adjusted up or down to suit demand, without degradation of service.
• It also allows for a robust system to be devised as there is no single point at which a
failure can bring down the database, as the networked schema of the storage
resources means that any failure would be local only.

2. SQLite: It is a relational database management system contained in a C programming


library. In contrast to many other database management systems, SQLite is not a
database engine.
• Rather, it is embedded into the end program. Unlike server database management
systems, the SQLite is an integral part of the program.
• SQLite is popular choice for the database engine in cell phones, PDAs, MP3 players, set
top boxes, and other electronic gadgets.
• SQLite has a small code footprint, makes efficient use of memory, disk space, and disk
bandwidth, is highly reliable, and requires no maintenance from a Database
Administrator.
• Because it requires no configuration and stores information in ordinary disk files,
SQLite is a popular choice as the database to back small to medium-sized websites.
• SQLite is often used as a surrogate for an enterprise RDBMS for demonstration
purposes or for testing.
• SQLite is fast and requires no setup, which takes a lot of the hassle out of testing and
which makes demos perky and easy to launch.

3. MySQL: An open-source alternative to Microsoft’s offering that still uses structured query
language.
• MySQL has gained traction as the go-to DBMS for web-based business applications,
especially those running e-commerce sites or leveraging dynamic content.
• Tech enterprises such as Facebook, Google and Adobe use this database management
tool.
• MySQL follows a client /server architecture. There is a database server (MySQL) and
arbitrarily many clients (application programs), which communicate with the server;
that is, they query data, save changes, etc.
• MySQL is compatible to run on many operating systems like Windows, Linux, many
varieties of UNIX (such as Sun, Solaris, AIX, and DECUNIX), OS/2, FreeBSD, and others.
• MySQL also provides a facility that the clients can run on the same computer as the
server or on another computer (communication via a local network or the Internet).
• MySQL uses Triggers, Stored procedures and views which allows the developer to give
a higher productivity.

4. IBM DB2: Designed for high-load, high-availability enterprise workloads, DB2 is used by
several global corporations to help improve database performance and lower costs.
Features
• Initially, IBM had developed DB2 product for their specific platform. Since year 1990,
it decided to develop a Universal Database (UDB) DB2 Server, 8 which can run on any
authoritative operating systems such as Linux, UNIX, and Windows.
• It is designed for mid-size to large-size business organizations. Platform - Linux, UNIX,
and Windows.

Various Database Languages are:


1. Database Definition Language: It specifies a set of definitions which are used to define
the database schema i.e. to create schema, tables, indexes, constraints etc.
• We specify the storage structure and access methods used by the database system by
a set of statements in a special type of DDL called data storage and definition
language.
• The data values stored in the database must satisfy certain consistency constraints that
are checked every time the database is updated:
▪ Domain Constraints: It determines the values that an attribute can take. They are
tested easily by the system whenever a new data item is entered into the database.
▪ Referential Integrity: It ensures that a value that appears in one relation for a given
set of attributes also appears in a certain set of attributes in another relation.
Database modifications can cause violations of referential integrity.
▪ When a referential-integrity constraint is violated, the normal procedure is to reject
the action that caused the violation
▪ Authorization: We may want to differentiate among the users as far as the type of
access they are permitted on various data values in the database.
▪ These differentiations can be: read authorization, reading, not modification; insert
authorization, insertion of new data, not modification of existing data; update
authorization, modification, not deletion; and delete authorization, deletion of
data.
▪ We may assign the user all, none, or a combination of these types of authorization.
• The output of the DDL is placed in the data dictionary or meta data.
• Example: create, drop, alter, truncate, rename etc

2. Data-Manipulation Language: It enables users to access or manipulate data as organized


by the appropriate data model.
• The types of access are:
▪ Retrieval of information stored in the database.
▪ Insertion of new information into the database.
▪ Deletion of information from the database.
▪ Modification of information stored in the database.
• Example: select, insert, update, delete, lock table, merge and call etc.
• The portion of a DML that involves information retrieval is called a query language.
• There are basically two types of DML: Procedural and Non-Procedural

3. Data-Control Language: It is used to protect the stored or saved data.


• The DCL execution is transactional. It also has rollback parameters (But in Oracle
database, the execution of data control language does not have the feature of rolling
back.)
• Example: grant (give user access privileges), revoke (take back permissions from user),
analyse (to collect stats) etc

4. Transaction-Control Language: It is used to run the changes made by the DML


statements.
• Example: commit (save the transaction), rollback (restore the database to original
since the last commit), set transaction (specifying characteristics for transaction), save
point (create restore points for changes to be rolled back)
Experiment - 3
Aim: Introduction to E-R Diagram and Symbol Table

Theory:
An Entity–relationship model (ER model) describes the structure of a database with
the help of a diagram, which is known as Entity Relationship Diagram (ER Diagram).
• An ER model is a design or blueprint of a database that can later be implemented as a
database. The main components of E-R model are: entity set and relationship set.
• An ER diagram shows the relationship among entity sets.
• An entity set is a group of similar entities and these entities can have attributes.
• In terms of DBMS, an entity is a table or attribute of a table in database, so by showing
relationship among tables and their attributes, ER diagram shows the complete logical
structure of a database.

Components of an ER Diagram:
1. Entity: An Entity may be an object with a physical existence – a particular person, car,
house, or employee – or it may be an object with a conceptual existence – a company, a
job, or a university course.

2. Attributes: Attributes are the properties which define the entity type.
• For example, Roll_No, Name, DOB, Age, Address, Mobile_No are the attributes which
defines entity type Student.
• It is of following types:
• Simple: They can’t be divided any further into subparts. These are also single-
valued or atomic valued i.e. have only one value for each entity. Example: A
person’s age
• Composite: They can be divided into subparts. They help us to group together
related attributes, making the modelling cleaner. Example: Attribute name could
be structured as a composite attribute consisting of first name, middle initial, and
last name.
• Multivalued: They can have more than one values for a particular entity. Example:
Mobile number or email address
• Derived: The value for this type of attribute can be derived from the values of other
related attributes or entities. Example: Age of a person can be derived from his date
of birth

3. Relationship Set: It is a set of relationships (an association among several entities) of the
same type.
• In mathematical terms, if E1, E2…, En are entity sets, then a relationship set R is a subset
of {(e1, e2…, en) | e1 ∈ E1, e2 ∈ E2…, en ∈ En} where (e1, e2…, en) is a relationship instance.
• A relationship may also have attributes called descriptive attributes.
• Example: Relation between course and student may have grade.
Degree of a Relationship Set: The number of different entity set that are participating in a
relationship set is called as degree of a relationship set.
1. Unary Relationship: When there is only ONE entity set participating in a relation, the
relationship is called as unary relationship. For example, one person is married to only
one person.
2. Binary Relationship: When there are TWO entities set participating in a relation, the
relationship is called as binary relationship. For example, Student is enrolled in Course.
3. n-ary Relationship: When there are n entities set participating in a relation, the
relationship is called as n-ary relationship.

Mapping Cardinality: It is a data constraint that specifies how many entities an entity can be
related to in a relationship set:
1. One-to-One: When each entity in each entity set can take part only once in the
relationship, the cardinality is one to one. Let us assume that a male can marry to one
female and a female can marry to one male. So, the relationship will be one to one.
2. One-to-Many: When entities in one entity set can take part only once in relationship
set and entities in other entity set can take part more than once in the relationship
set, cardinality is many to one. Let us assume that a student can take only one course
but one course can be taken by many students. So, the cardinality will be n to 1.
3. Many-to-One: An entity in A can associated with at most one entity in B, but an entity
in B, can be associated with any number (zero or more) of entities in A.
4. Many-to-Many: When entities in all entity sets can take part more than once in the
relationship cardinality is many to many. Let us assume that a student can take more
than one course and one course can be taken by many students. So the relationship
will be many to many.

Existence of relationship: A relationship can be:


• Total or Mandatory: When every entity in E must participate in at least one
relationship in R.
• Partial or Optional: When some entities in E do not participate in relationship in R.
Symbol Table:
Experiment - 4
Aim: Introduction to different Types of Constraints in SQL

Theory:
Constraints are the rules that we can apply on the type of data in a table. That is, we
can specify the limit on the type of data that can be stored in a particular column in a table
using constraints.

Constraints in SQL are:


• NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a
column is specified as NOT NULL then we will not be able to store null in this particular
column any more.
• UNIQUE: This constraint when specified with a column, tells that all the values in the
column must be unique. That is, the values in any row of a column must not be repeated.
• PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table.
And this constraint is used to specify a field in a table as primary key.
• FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in another
table. And this constraint is used to specify a field as Foreign key.
• CHECK: This constraint helps to validate the values of a column to meet a particular
condition. That is, it helps to ensure that the value stored in a column meets a specific
condition.
• DEFAULT: This constraint specifies a default value for the column when no value is
specified by the user.

Definition: We can specify the constraints at the time of creating the table or after creating
a table using alter commands.
• Syntax: Below is the syntax to create constraints using CREATE TABLE statement at the
time of creating the table.
CREATE TABLE sample table
(
column1 datatype(size) constraint name,
column2 datatype(size) constraint name,
column3 datatype(size) constraint name,
....
);
Experiment - 5

Case Study: Hospital Management System


Problem Statement: Design and develop a database for hospital for which details are as
follows:
• It will maintain information about all the departments like department name, name of
head of department, its contact number etc.
• It will maintain information about the doctors like name, address, qualification,
specialization, contact number etc
• It will maintain information about all the patients like name, gender, age, admission
date, disease, address etc
• It will maintain information about all the nurses like name, address, qualification etc
• It will maintain information about all the tests that are being done in hospital like test
name, cost etc
• It will maintain information about all the wards in each department like, ward number,
capacity etc
• It will maintain information about all the Bills generated in the hospital in name of
patients, with entries like, hospital rent, doctor fee etc.
Experiment - 6
Aim: Creating E-R Diagram, Entity tables and Relationship tables

Assumptions:
• A department may have many doctors, nurses or wards but every doctor, nurse or
ward lies under only one department
• Many nurses can perform many tests and can take care of many wards
• Many doctors can attend many patients
• Only one bill is paid by one patient, i.e. no patient pays more than one bill and no bill
is paid by more than one patient.

Defining Entity Sets:


• Department: It contains information of all the departments such as department name,
department id, department HOD and his contact number.
• Nurse: It contains information of all the nurses such as name, id, address, contact
number, qualification, what are his/her responsibilities and from which department
she belongs.
• Ward: It contains information of all the wards such as ward number, ward id and its
type.
• Doctor: It contains information about all the doctors such as name, id, address,
qualification, contact number, their specialisation and from which department they
belong.
• Test: It contains all the information about which test is performed on which patient
and who performs the test , what is test id, name and its cost.
• Patient: : It contains all the information about patient such as name, id, address,
contact number, age, gender, disease and on which date he/she is being admitted to
the hospital.
• Bill: It will maintain information about all the Bills generated in the hospital in name of
patients, with entries like, hospital rent, doctor fee etc.

Defining Relationship Sets:


• Belongs to: It maintains information about which nurse belongs to which department.
• Has: : It maintains information about which doctor belongs to which department.
• Is in: It contains information about which ward belongs to which department.
• Performs: It contains information about which nurse performs test on which patient.
• Governs: It contains information about which nurse maintains which ward.
• Assigned: It contains information about which ward is assigned to which patient.
• Is given: It contains information about which test is given to which patient.
• Attends: It contains information about which patient attends which doctor.
• Pays: It contains information about how much money is paid by a patient.
Entity-Relationship Diagram
DPT_NAME DPT_ID HOD

1 HAS
BELONGS_TO DEPARTMENT_269 1

NUR_ID NUR_NAME 1
CONTACT_NO DOC_NAME DOC_SPEC
M
M
PERFORMS NURSE_269 NUR_ADD IS_IN M DOC_ADD

M
DOCTOR_269
W_ID
CONTACT_NO NUR_QUAL
M M
DOC_QUAL
M
GOVERNS WARD_269
CONTACT_NO DOC_ID
1
T_NAME
CAPACITY TYPE
M

TEST_269 T_ID ASSIGNED BED NO

M
P_NAME P_ID
COST

GENDER M P_ADD

IS_GIVEN PATIENT_269 ATTENDS


M M

CONTACT_NO
1 ADM_DATE

AGE PAYS DISEASE

1
TOTAL MEDICINE
AMOUNT BILL_269 COST

HOSPITAL
B_ID
DOCTOR FEE RENT
Experiment - 7
Aim: To implement various DDL and DML statements

Theory:
Data Definition Language: Data Definition Language is used to define the database
structure or schema. DDL is also used to specify additional properties of the data.
• The storage structure and access methods used by the database system by a set of
statements in a special type of DDL called a data storage and definition language.
• These statements define the implementation details of the database schema, which
are usually hidden from the users.

Some Commands:
• CREATE: Used to create objects in database
▪ Syntax:
CREATE TABLE <table name>
(
<column name> Data Type Constraint,
<column name> Data Type Constraint
);

• ALTER: Used to alter the structure of database


▪ Syntax:
ALTER TABLE <table name>
ADD <column name> Data Type;

• DROP: Used to delete objects from database


▪ Syntax:
DROP TABLE <table name>;

• RENAME: Used to rename objects in the database


▪ Syntax:
RENAME <table name>
TO <new name>;

Data Manipulation Language: DML statements are used for managing data with in schema
objects. DML are of two types
• Procedural DMLs: It requires a user to specify what data are needed and how to get
those data.
• Declarative DMLs: It require a user to specify what data are needed without specifying
how to get those data. Declarative DMLs are usually easier to learn and use than
procedural DMLs.
Some Commands:
• SELECT: Used to retrieve data from the database
• Syntax:
SELECT <column name(s)>
FROM <table name(s)>;

• INSERT: Used to insert data into a table


• Syntax:
INSERT INTO <table name>
VALUES (value1, value2... );

• UPDATE: Used to update existing data within a table


• Syntax:
UPDATE <table name>
SET <column1 = value1>, <column2 = value2>…
WHERE <condition>;

• DELETE: Used to delete all records from a table


• Syntax:
DELETE FROM <table name>
WHERE <condition>;

1. Department Table

Creation:
CREATE TABLE DEPARTMENT_269(
DPT_ID INTEGER,
DPT_NAME VARCHAR2(20),
HOD VARCHAR2(20),
CONTACT_NO NUMERIC(10),
PRIMARY KEY (DPT_ID)
);

Insertion:
INSERT INTO DEPARTMENT_269 VALUES (101, 'CARIOLOGY', 'DR. ARUN', 9582504451);
INSERT INTO DEPARTMENT_269 VALUES (102, 'NEUROLOGY', 'DR. NAVEEN', 9455832418);
INSERT INTO DEPARTMENT_269 VALUES (103, 'ORTHOPEDICS', 'DR. RAHUL', 8174192807);
INSERT INTO DEPARTMENT_269 VALUES (104, 'PHYSIOTHERAPY', 'DR. NEHA', 8795904507);
INSERT INTO DEPARTMENT_269 VALUES (105, 'ONCOLOGY', 'DR. VASTU', 8791254507);

Result:
2. Nurse Table

Creation:
CREATE TABLE NURSE_269(
NUR_ID INTEGER,
NUR_NAME VARCHAR2(20),
NUR_QUAL VARCHAR2(20),
NUR_ADD VARCHAR2(100),
CONTACT_NO NUMERIC(10),
PRIMARY KEY (NUR_ID)
);

Insertion:
INSERT INTO NURSE_269 VALUES (1, 'ANJALI', 'BSC', 'INDRA COLONY', 8980461413);
INSERT INTO NURSE_269 VALUES (22, 'SNEHA', 'DIPLOMA', 'VASANT VIHAR', 9535868542);
INSERT INTO NURSE_269 VALUES (34, 'TRIPTI', 'BSC', 'KASHMIRI GATE', 9535417042);
INSERT INTO NURSE_269 VALUES (43, 'KAJOl', 'GNM', 'BAWANA', 8288803612);
INSERT INTO NURSE_269 VALUES (15, 'AMIT', 'DIPLOMA', 'NOIDA', 8960554011);
INSERT INTO NURSE_269 VALUES (6, 'RITIK', 'GNM', 'AZADPUR', 9510201992);

Result:

3. Doctor Table

Creation:
CREATE TABLE DOCTOR_269(
DOC_ID INTEGER,
DOC_NAME VARCHAR2(20),
DOC_SPEC VARCHAR2(20),
DOC_QUAL VARCHAR2(20),
DOC_ADD VARCHAR2(100),
CONTACT_NO NUMERIC(10),
PRIMARY KEY(DOC_ID)
);

Insertion:
INSERT INTO DOCTOR_269 VALUES (1, 'DR. RITU', 'SKIN', 'MBBS MD', 'NOIDA', 9320918900);
INSERT INTO DOCTOR_269 VALUES (12, 'DR. RAHUL', 'SURGEON', 'MBBS D.ORTHO', 'GURUGRAM',
8297081470);
INSERT INTO DOCTOR_269 VALUES (15, 'DR. VIRAT', 'SURGEON', 'MBBS', 'GREEN PARK', 838208
8066);
INSERT INTO DOCTOR_269 VALUES (21, 'DR. ARUN', 'CARDIO', 'MBBS MS', 'SAKET', 9518127001
);
INSERT INTO DOCTOR_269 VALUES (7, 'DR. NAVEEN', 'MIGRANE', 'MBBS MS', 'KASHMIRI GATE',
8787931884);
INSERT INTO DOCTOR_269 VALUES (8, 'DR. NEHA', 'CORONA', 'BPT', 'MALVIYA NAGAR', 9362879
550);
INSERT INTO DOCTOR_269 VALUES (4, 'DR. DAYA', 'ENT', 'MBBS MD', 'ROHINI', 9345812150);

Result:

4. Ward Table

Creation:
CREATE TABLE WARD_269(
W_ID INTEGER,
CAPACITY INTEGER,
W_TYPE VARCHAR2(20),
PRIMARY KEY (W_ID)
);

Insertion:
INSERT INTO WARD_269 VALUES (201, 40, 'VIP');
INSERT INTO WARD_269 VALUES (211, 120, 'NORMAL');
INSERT INTO WARD_269 VALUES (205, 50, 'EMERGENCY');
INSERT INTO WARD_269 VALUES (216, 60, 'OPD');

Result:

5. Patient Table

Creation:
CREATE TABLE PATIENT_269(
P_ID INTEGER,
P_NAME VARCHAR2(20),
AGE INTEGER,
GENDER CHAR(1),
DISEASE VARCHAR2(50),
ADM_DATE DATE,
P_ADD VARCHAR2(100),
CONTACT_NO NUMERIC(10),
PRIMARY KEY (P_ID)
);
Insertion:
INSERT INTO PATIENT_269 VALUES (1,'KAVITA',32,'F','CORONA',TO_DATE('12/01/2017', 'DD/MM
/YYYY'),'NOIDA',8180784554);
INSERT INTO PATIENT_269 VALUES (34,'NEHA',36,'F','CORONA',TO_DATE('16/01/2018', 'DD/MM/
YYYY'),'HARI COLONY',9115258051);
INSERT INTO PATIENT_269 VALUES (5,'MAMTA',20,'F','CANCER',TO_DATE('18/01/2016', 'DD/MM/
YYYY'),'SEEMAPUR',8501942687);
INSERT INTO PATIENT_269 VALUES (28,'SOHAM',27,'M','VIRAL FEVER',TO_DATE('21/01/2017', '
DD/MM/YYYY'),'CHAND BAGH',9413554438);
INSERT INTO PATIENT_269 VALUES (33,'ROHAN',32,'M','HIV',TO_DATE('23/01/2018', 'DD/MM/YY
YY'),'SANJAY NAGAR',8858760103);
INSERT INTO PATIENT_269 VALUES (25,'NAZIYA',26,'F','SKIN INFECTION',TO_DATE('25/01/2016
', 'DD/MM/YYYY'),'PRATAP NAGAR',9216209915);
INSERT INTO PATIENT_269 VALUES (17,'SNEHA',54,'F','SEVERE BURNS',TO_DATE('26/01/2016',
'DD/MM/YYYY'),'KASHMIRI GATE',8172630052);
INSERT INTO PATIENT_269 VALUES (16,'RAKESH',48,'M','INTESTINE BLOCKAGE',TO_DATE('27/01/
2017', 'DD/MM/YYYY'),'TIS HAZARI',9585869583);
INSERT INTO PATIENT_269 VALUES (45,'JEETU',45,'M','CORONA',TO_DATE('29/01/2019', 'DD/MM
/YYYY'),'PITAMPURA',8698508249);
INSERT INTO PATIENT_269 VALUES (24,'SHIV',50,'M','HIV',TO_DATE('1/02/2019', 'DD/MM/YYYY
'),'ROHINI SEC-19',9422179167);
INSERT INTO PATIENT_269 VALUES (44,'AMAN',34,'M','FLU',TO_DATE('2/02/2019', 'DD/MM/YYYY
'),'BAWANA',8503516944);
INSERT INTO PATIENT_269 VALUES (46,'AMIT',59,'M','EYE INFECTION',TO_DATE('6/02/2020', '
DD/MM/YYYY'),'NARELA',8144256142);
INSERT INTO PATIENT_269 VALUES (27,'ASHISH',20,'M','JAUNDICE',TO_DATE('10/02/2020', 'DD
/MM/YYYY'),'SONIPAT',9632910827);
INSERT INTO PATIENT_269 VALUES (50,'DEEPIKA',47,'F','MALARIA',TO_DATE('11/02/2016', 'DD
/MM/YYYY'),'KHERA',9328577085);
INSERT INTO PATIENT_269 VALUES (31,'NAVEEN',27,'M','DENGUE',TO_DATE('13/02/2020', 'DD/M
M/YYYY'),'SOUTH GATE',8223078520);

Result:

6. Test Table

Creation:
CREATE TABLE TEST_269(
T_ID INTEGER,
T_NAME VARCHAR2(20),
COST INTEGER,
PRIMARY KEY (T_ID)
);

Insertion:
INSERT INTO TEST_269 VALUES (1, 'HIV', 1000);
INSERT INTO TEST_269 VALUES (3, 'CANCER', 700);
INSERT INTO TEST_269 VALUES (7, 'TB', 300);
INSERT INTO TEST_269 VALUES (4, 'CORONA', 800);
INSERT INTO TEST_269 VALUES (10, 'ALLERGY', 5000);

Result:

7. Bill Table

Creation:
CREATE TABLE BILL_269(
B_ID INTEGER,
MEDICINE_COST INTEGER,
DOCTOR_FEE INTEGER,
HOSPITAL_RENT INTEGER,
PRIMARY KEY(B_ID)
);

Insertion:
INSERT INTO BILL_269 VALUES (1, 850, 1860, 720);
INSERT INTO BILL_269 VALUES (29, 540, 1690, 530);
INSERT INTO BILL_269 VALUES (7, 680, 1770, 660);
INSERT INTO BILL_269 VALUES (19, 1380, 1890, 950);
INSERT INTO BILL_269 VALUES (18, 1410, 1920, 1350);
INSERT INTO BILL_269 VALUES (13, 1270, 1630, 920);
INSERT INTO BILL_269 VALUES (2, 1190, 1980, 1690);
INSERT INTO BILL_269 VALUES (16, 910, 1720, 1470);
INSERT INTO BILL_269 VALUES (25, 1080, 1420, 1020);
INSERT INTO BILL_269 VALUES (27, 1120, 2000, 900);

Result:
8. Belongs_to Table

Creation:
CREATE TABLE BELONGS_TO(
DPT_ID INTEGER,
NUR_ID INTEGER,
FOREIGN KEY (DPT_ID) REFERENCES DEPARTMENT_269(DPT_ID),
FOREIGN KEY (NUR_ID) REFERENCES NURSE_269(NUR_ID)
);

Insertion:
INSERT INTO BELONGS_TO VALUES (101, 1);
INSERT INTO BELONGS_TO VALUES (102, 22);
INSERT INTO BELONGS_TO VALUES (101, 34);
INSERT INTO BELONGS_TO VALUES (103, 15);
INSERT INTO BELONGS_TO VALUES (104, 6);

Result:

9. Has Table

Creation:
CREATE TABLE HAS(
DPT_ID INTEGER,
DOC_ID INTEGER,
FOREIGN KEY (DPT_ID) REFERENCES DEPARTMENT_269(DPT_ID),
FOREIGN KEY (DOC_ID) REFERENCES DOCTOR_269(DOC_ID)
);

Insertion:
INSERT INTO HAS VALUES (104, 1);
INSERT INTO HAS VALUES (103, 15);
INSERT INTO HAS VALUES (101, 21);
INSERT INTO HAS VALUES (102, 7);
INSERT INTO HAS VALUES (104, 8);

Result:
10. Is_in Table

Creation:
CREATE TABLE IS_IN(
DPT_ID INTEGER,
W_ID INTEGER,
FOREIGN KEY (DPT_ID) REFERENCES DEPARTMENT_269(DPT_ID),
FOREIGN KEY (W_ID) REFERENCES WARD_269(W_ID)
);

Insertion:
INSERT INTO IS_IN VALUES (102, 201);
INSERT INTO IS_IN VALUES (104, 211);
INSERT INTO IS_IN VALUES (103,205);
INSERT INTO IS_IN VALUES (103,216);

Result:

11. Performs Table

Creation:
CREATE TABLE PERFORMS(
NUR_ID INTEGER,
T_ID INTEGER,
FOREIGN KEY (NUR_ID) REFERENCES NURSE_269(NUR_ID),
FOREIGN KEY (T_ID) REFERENCES TEST_269(T_ID)
);

Insertion:
INSERT INTO PERFORMS VALUES (1,1);
INSERT INTO PERFORMS VALUES (22,1);
INSERT INTO PERFORMS VALUES (1,10);
INSERT INTO PERFORMS VALUES (1,4);
INSERT INTO PERFORMS VALUES (34,7);
INSERT INTO PERFORMS VALUES (15,3);
INSERT INTO PERFORMS VALUES (15,4);
Result:
12. Governs Table

Creation:
CREATE TABLE GOVERNS(
NUR_ID INTEGER,
W_ID INTEGER,
FOREIGN KEY (NUR_ID) REFERENCES NURSE_269(NUR_ID),
FOREIGN KEY (W_ID) REFERENCES WARD_269(W_ID)
);

Insertion:
INSERT INTO GOVERNS VALUES (6,211);
INSERT INTO GOVERNS VALUES (6,205);
INSERT INTO GOVERNS VALUES (6,216);
INSERT INTO GOVERNS VALUES (22,201);
INSERT INTO GOVERNS VALUES (22,205);
INSERT INTO GOVERNS VALUES (34,201);
INSERT INTO GOVERNS VALUES (15,201);

Result:

13. Assigned Table

Creation:
CREATE TABLE ASSIGNED(
W_ID INTEGER,
P_ID INTEGER,
BED_NO INTEGER,
FOREIGN KEY (W_ID) REFERENCES WARD_269(W_ID),
FOREIGN KEY (P_ID) REFERENCES PATIENT_269(P_ID)
);

Insertion:
INSERT INTO ASSIGNED VALUES (205,1,12);
INSERT INTO ASSIGNED VALUES (205,34,34);
INSERT INTO ASSIGNED VALUES (205,5,23);
INSERT INTO ASSIGNED VALUES (216,28,45);
INSERT INTO ASSIGNED VALUES (201,33,12);
INSERT INTO ASSIGNED VALUES (211,25,96);
INSERT INTO ASSIGNED VALUES (216,17,55);
INSERT INTO ASSIGNED VALUES (211,16,90);
INSERT INTO ASSIGNED VALUES (205,45,42);
INSERT INTO ASSIGNED VALUES (201,24,23);
INSERT INTO ASSIGNED VALUES (216,44,25);
INSERT INTO ASSIGNED VALUES (211,46,14);
INSERT INTO ASSIGNED VALUES (211,27,113);
INSERT INTO ASSIGNED VALUES (201,50,35);
INSERT INTO ASSIGNED VALUES (201,31,7);

Result:

14. Is_given Table

Creation:
CREATE TABLE IS_GIVEN(
T_ID INTEGER,
P_ID INTEGER,
FOREIGN KEY (T_ID) REFERENCES TEST_269(T_ID),
FOREIGN KEY (P_ID) REFERENCES PATIENT_269(P_ID)
);

Insertion:
INSERT INTO IS_GIVEN VALUES (4,1);
INSERT INTO IS_GIVEN VALUES (4,34);
INSERT INTO IS_GIVEN VALUES (3,5);
INSERT INTO IS_GIVEN VALUES (7,28);
INSERT INTO IS_GIVEN VALUES (1,33);
INSERT INTO IS_GIVEN VALUES (3,25);
INSERT INTO IS_GIVEN VALUES (3,17);
INSERT INTO IS_GIVEN VALUES (3,16);

Result:
15. Attends Table

Creation:
CREATE TABLE ATTENDS(
DOC_ID INTEGER,
P_ID INTEGER,
FOREIGN KEY (DOC_ID) REFERENCES DOCTOR_269(DOC_ID),
FOREIGN KEY (P_ID) REFERENCES PATIENT_269(P_ID)
);

Insertion:
INSERT INTO ATTENDS VALUES (8,1);
INSERT INTO ATTENDS VALUES (8,34);
INSERT INTO ATTENDS VALUES (1,5);
INSERT INTO ATTENDS VALUES (12,28);
INSERT INTO ATTENDS VALUES (15,33);
INSERT INTO ATTENDS VALUES (1,25);
INSERT INTO ATTENDS VALUES (1,17);
INSERT INTO ATTENDS VALUES (12,16);
INSERT INTO ATTENDS VALUES (8,45);
INSERT INTO ATTENDS VALUES (15,24);
INSERT INTO ATTENDS VALUES (21,44);
INSERT INTO ATTENDS VALUES (7,46);

Result:

16. Pays Table

Creation:
CREATE TABLE PAYS(
P_ID INTEGER,
B_ID INTEGER,
FOREIGN KEY (P_ID) REFERENCES PATIENT_269(P_ID),
FOREIGN KEY (B_ID) REFERENCES BILL_269(B_ID)
);

Insertion:
INSERT INTO PAYS VALUES (1,1);
INSERT INTO PAYS VALUES (34,29);
INSERT INTO PAYS VALUES (5,7);
INSERT INTO PAYS VALUES (33,19);
INSERT INTO PAYS VALUES (25,18);
INSERT INTO PAYS VALUES (16,13);
INSERT INTO PAYS VALUES (45,2);
INSERT INTO PAYS VALUES (44,16);
INSERT INTO PAYS VALUES (27,25);
INSERT INTO PAYS VALUES (31,27);

Result:

Updation:
• Update the capacity of ‘Emergency’ ward to 70

UPDATE WARD_269
SET CAPACITY = 70
WHERE W_TYPE = 'EMERGENCY';

• Update the contact number of Nurse with ID 34 to 9203307592

UPDATE NURSE_269
SET CONTACT_NO = 9203307592
WHERE NUR_ID = 34;
Learnings and Findings:
1. We learnt about the basics of the DBMS – creating tables .
2. How to declare several columns of different data types
3. How to make them primary keys and implementing several constraints.

Discussion:
From this experiment we learnt about creating tables as per the different
requirements. It is the basic pillar for storing information.

Conclusion:
All the DML and DDL commands were successfully understood and implemented on
the database in the experiment.
Experiment - 8
Aim: To perform various Simple Queries

Theory:
SELECT Query: It is the most commonly used statement in SQL used to retrieve or fetch
data from a database. We can fetch either the entire table or according to some specified
rules.
• The data returned is stored in a result table. This result table is also called result-set.
• With the SELECT clause of a SELECT command statement, we specify the columns that
we want to be displayed in the query result and, optionally, which column headings
we prefer to see above the result table.
• The select clause is the first clause and is one of the last clauses of the select statement
that the database server evaluates.
• The reason for this is that before we can determine what to include in the final result
set, we need to know all of the possible columns that could be included in the final
result set.
• Syntax:
SELECT <column(s)>
FROM <table name(s)>
WHERE <condition(s)>;

WHERE Clause: It is used for fetching filtered data in a result set.


• It is used to fetch data according to a particular criterion.
• WHERE keyword can also be used to filter data by matching patterns.

FROM Clause: From clause can be used to specify a sub-query expression in SQL.
• The relation produced by the sub-query is then used as a new relation on which the
outer query is applied.
• Sub queries in the from clause are supported by most of the SQL implementations.

Condition: These are used for filtering the data and getting precise result.
• Syntax:
<Column Name (operator) Value> (joining operator) <other condition(s)>

Operator Description Operator Description


> Greater Than <> Not Equals To
>= Greater Than or Equal To BETWEEN In an Inclusive Range
< Less Than LIKE Search for a Pattern
<= Less Than or Equal To IN Specify Multiple Possible Values
= Equals To NOT To Negate the Statement
AND To join two conditions using OR To join two conditions using
conjunction disjunction
Queries:
• Select those wards where capacity is more than 50.
SELECT *
FROM WARD_269
WHERE CAPACITY > 50;

• Select name address and disease of all female patients.


SELECT P_NAME, DISEASE, P_ADD
FROM PATIENT_269
WHERE GENDER = 'F';

• Retrieve male patients and department of doctors that attend them.


SELECT P_NAME, DOC_NAME, DPT_NAME
FROM PATIENT_269, ATTENDS, DOCTOR_269, HAS, DEPARTMENT_269
WHERE GENDER = 'M' AND
PATIENT_269.P_ID = ATTENDS.P_ID AND
ATTENDS.DOC_ID = DOCTOR_269.DOC_ID AND
DOCTOR_269.DOC_ID = HAS.DOC_ID AND
HAS.DPT_ID = DEPARTMENT_269.DPT_ID;

• Retrieve the names of tests done by nurses of ‘CARDIOLOGY’ Department.


SELECT T_NAME, NUR_NAME, DPT_NAME
FROM TEST_269, NURSE_269, DEPARTMENT_269, PERFORMS, BELONGS_TO
WHERE DPT_NAME = 'CARIOLOGY' AND
DEPARTMENT_269.DPT_ID = BELONGS_TO.DPT_ID AND
BELONGS_TO.NUR_ID = NURSE_269.NUR_ID AND
NURSE_269.NUR_ID = PERFORMS.NUR_ID AND
PERFORMS.T_ID = TEST_269.T_ID;
• Retrieve patient name and address whose name contain letter ‘I’.

SELECT P_NAME, P_ADD


FROM PATIENT_269
WHERE P_NAME LIKE '%I%';

• Retrieve all tests given to female patients.

SELECT PATIENT_269.P_ID, P_NAME, TEST_269.T_ID, T_NAME


FROM TEST_269, PATIENT_269, IS_GIVEN
WHERE GENDER = 'F' AND
PATIENT_269.P_ID = IS_GIVEN.P_ID AND
TEST_269.T_ID = IS_GIVEN.T_ID;

• Selecting those doctors whose id is greater than 10 and whose dept id is greater than 102.

SELECT DPT_ID, DOCTOR_269.DOC_ID, DOC_NAME


FROM DOCTOR_269, HAS
WHERE DOCTOR_269.DOC_ID > 10 AND
DOCTOR_269.DOC_ID = HAS.DOC_ID AND
DPT_ID > 102;

• Retrieve patient name, disease and age of patients with age between 25 and 35.

SELECT P_NAME, AGE, DISEASE


FROM PATIENT_269
WHERE AGE BETWEEN 25 AND 35;
Learnings and Findings:
1. We learnt about how to store the information in tables using the INSERT command
2. We also come to know how to update the information already stored in the tables
using the UPDATE command
3. We also come to know how to delete the information already stored in the tables using
the DELETE command as per the requirement.

Discussion:
From this experiment we learnt about query the database and extracting the relevant
information from the database.

Conclusion:
Various Simple Query formats and syntaxes are successfully understood and
implemented on the database in the experiment.
Experiment - 9
Aim: To implement various Join Queries

Theory:
Join: A SQL Join statement is used to combine data or rows from two or more tables
based on a common field between them.
• Different types of Joins are:

INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the
condition satisfies.
• This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e. value of the common field will be same.
• Syntax:
SELECT <column(s)>
FROM <table1> INNER JOIN <table2>
ON table1.matching_column = table2.matching_cloumn;

LEFT JOIN: This join returns all the rows of the table on the left side of the join and matching
rows for the table on the right side of join.
• The rows for which there is no matching row on right side, the result-set will
contain null.
• Syntax:
SELECT <column(s)>
FROM <table1> LEFT JOIN <table2>
ON table1.matching_column = table2.matching_cloumn;
RIGHT JOIN: This join returns all the rows of the table on the right side of the join and
matching rows for the table on the left side of join.
• The rows for which there is no matching row on left side, the result-set will
contain null.
• Syntax:
SELECT <column(s)>
FROM <table1> RIGHT JOIN <table2>
ON table1.matching_column = table2.matching_cloumn;

FULL JOIN: It creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN.
• The result-set will contain all the rows from both the tables.
• The rows for which there is no matching, the result-set will contain NULL values.
• Syntax:
SELECT <column(s)>
FROM <table1> FULL JOIN <table2>
ON table1.matching_column = table2.matching_cloumn;

Join Query: Join the Department and Doctor tables, matching the Doctor names

-- INNER JOIN
SELECT DPT_ID, DPT_NAME, HOD, DOC_ID
FROM DEPARTMENT_269 INNER JOIN DOCTOR_269
ON HOD = DOC_NAME
ORDER BY DPT_ID ASC;
-- LEFT JOIN
SELECT DPT_ID, DPT_NAME, HOD, DOC_ID
FROM DEPARTMENT_269 LEFT JOIN DOCTOR_269
ON HOD = DOC_NAME
ORDER BY DPT_ID ASC;

-- RIGHT JOIN
SELECT DPT_ID, DPT_NAME, HOD, DOC_ID
FROM DEPARTMENT_269 RIGHT JOIN DOCTOR_269
ON HOD = DOC_NAME
ORDER BY DPT_ID ASC;

-- FULL JOIN
SELECT DPT_ID, DPT_NAME, HOD, DOC_ID
FROM DEPARTMENT_269 FULL JOIN DOCTOR_269
ON HOD = DOC_NAME
ORDER BY DPT_ID ASC;

Learnings and Findings:


1. We learnt about different types of joins available.
2. We come to know about the use and method of implementation of them.
3. How the information retrieval can be made unique with the help of joins.

Discussion:
From this experiment we learnt about different joins available in the SQL and the way
of implementing them. The joins present a unique way of information retrieval based upon
the situation and the conditions.

Conclusion:
All the join commands were successfully understood and implemented on the
database in the experiment.
Experiment - 10
Aim: To implement various Aggregate functions, and date and time functions

Theory:
Aggregate functions: An aggregate function is a function where the values of multiple
rows are grouped together as input on certain criteria to form a single value of more
significant meaning.

Various Aggregate Functions:


1. Count():
• COUNT(*): Returns total number of records
• COUNT(<column name>): Return number of Non-Null values over the given column
• COUNT(Distinct <column name>): Return number of distinct Non-Null values over the
given column

2. Sum():
• SUM(<column name>): Returns the sum all Non-Null values of the given column
• SUM(<column name>): Returns the sum of all distinct Non-Null values

3. Avg():
• AVG(<column name>): Returns the value of SUM() / COUNT()
• AVG(Distinct <column name>): Returns the value of SUM(Distinct) / COUNT(Distinct)

4. Min():
• Min(<column name>): Minimum value in the given column except NULL

5. Max():
• Max(<column name>): Maximum value in the given column except NULL

Aggregate Function Queries:


• Count the number of patients which are male.
SELECT COUNT(*) AS NO_OF_PATIENTS
FROM PATIENT_269
WHERE GENDER = 'M';

• Find the sum of all bills paid by patients .


SELECT SUM(HOSPITAL_RENT + DOCTOR_FEE + MEDICINE_COST) AS TOTAL_AMT
FROM BILL_269;
• Find average hospital rent paid by patients.
SELECT AVG(HOSPITAL_RENT) AS AVG_RENT
FROM BILL_269;

• Count the number of patients assigned to each ward.


SELECT WARD_269.W_ID, COUNT(WARD_269.W_ID) AS NO_OF_PATIENTS
FROM PATIENT_269 INNER JOIN ASSIGNED
ON PATIENT_269.P_ID = ASSIGNED.P_ID
INNER JOIN WARD_269
ON ASSIGNED.W_ID = WARD_269.W_ID
GROUP BY WARD_269.W_ID
ORDER BY WARD_269.W_ID;

• Find the ward with maximum capacity.


SELECT *
FROM WARD_269
WHERE CAPACITY = (
SELECT MAX(CAPACITY)
FROM WARD_269
);

• Find the test with minimum cost.


SELECT *
FROM TEST_269
WHERE COST = (
SELECT MIN(COST)
FROM TEST_269
);

• Count the number of patients which are older than 20 years.


SELECT COUNT(*) AS NO_OF_PATIENTS
FROM PATIENT_269
WHERE AGE >= 20;
Date AND TIME functions: In SQL, while working with database, the format of the date in
table must be matched with the input date in order to insert.
• In various scenarios instead of date, datetime (time is also involved with date) is used.

Various Date and Time commands are:


• DATE(): Extracts the date part of a date or date/time expression.
• EXTRACT(): Returns a single part of a date/time. There are several units that can be
considered such as: second, minute, hour, day, week, month, year, etc.
▪ Syntax: EXTRACT(Unit FROM Date);

• DATE_ADD(): Adds a specified time interval to a date. Type can be one of the
following: microsecond, second, minute, hour, day, week, month, quarter, year, etc.
▪ Syntax: DATE_ADD(Interval, Type);

• DATE_SUB(): Subtracts a specified time interval from a date.


• DATE_FORMAT(): Displays date/time data in different formats.
▪ Syntax: DATE_FORMAT(Date, Format);

Date and Time Queries:


• Find the patients who are admitted after 1-2-2016.
SELECT *
FROM PATIENT_269
WHERE ADM_DATE - TO_DATE('20/1/2018', 'DD/MM/YYYY') > 0;

• Find the number of patients admitted in each year.


SELECT A_YEAR, COUNT(A_YEAR)
FROM (
SELECT EXTRACT(YEAR FROM ADM_DATE) AS A_YEAR
FROM PATIENT_269
)
GROUP BY A_YEAR
ORDER BY A_YEAR;
Learnings and Findings:
1. The AVERAGE command computed the average of the hospital rent paid by all the
patients and the SELECT command prints the output table.
2. The COUNT command returns the number of entries satisfying the condition, Age >=
20 from the specified table.
3. As the name suggest the MAX command compares and tells the maximum of the
compatible entries of the column CAPACITY from the specified WARD table.
4. The MIN command finds the minimum of the specified information COST from the
specified TEST table.

Discussion:
From this experiment we learnt about different commands helpful in retrieving many
specific information about the entries – AVERAGE, SUM , MINIMUM ,MAXIMUM, COUNT of
different entries in different conditions. We also implemented various Date and Time
functions on different tables with different conditions.

Conclusion:
All the aggregate commands and Date and Time commands were successfully
understood and implemented on the database in the experiment.
Experiment - 11
Aim: To implement Views, Triggers, and Grant and Revoke commands

Theory:
Views: Views are kind of virtual tables. A view also has rows and columns as they are
in a real table in the database.
• We can create a view by selecting fields from one or more tables present in the
database.
• A View can either have all the rows of a table or specific rows based on certain
condition.
• All DML and DDL commands which are used with tables can also be used with Views in
the same manner and syntax. For example, to create a View, we use CREATE
VIEW statement. A View can be created from a single table or multiple tables.
• Syntax:
CREATE VIEW <view name> AS
SELECT <column name(s)>
FROM <table name>
WHERE <condition>;

Triggers: A trigger is a stored procedure in database which automatically invokes whenever


a special event in the database occurs.
• For example, a trigger can be invoked when a row is inserted into a specified table or
when certain table columns are being updated.
• Syntax:
CREATE [OR REPLACE] TRIGGER <trigger name>
{BEFORE | AFTER} <triggering event>
ON <table name>
[FOR EACH ROW]

<trigger body>
DECLARE
declaration statements
BEGIN
executable statements
EXCEPTION
exception handling statements
END;

Explanation of Syntax:
• create trigger <trigger name>: Creates or replaces an existing trigger.
• [before | after]: This specifies when the trigger will be executed.
• {insert | update | delete}: This specifies the DML operation.
• on <table name>: This specifies the name of the table associated with the trigger.
• [for each row]: This specifies a row-level trigger, i.e., the trigger will be executed for
each row being affected.
• <trigger body>: This provides the operation to be performed as trigger is fired.

Triggers can be used for:


• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions

Grant and Revoke Commands: DCL commands are used to enforce database security in a
multiple user database environment. Two types of DCL commands are Grant and Revoke.
• Only Database Administrator or owner of the database can provide/ remove privileges
on a database object.

GRANT Command: It is used to provide access or privileges on the database objects to the
users.
• Syntax:
GRANT <privilege name>
ON <object name>
TO {username | PUBLIC | role name}
[WITH GRANT OPTION];

Explanation of Syntax:
• <privilege name>: It is the access right or privilege granted to the user. Some of the
privileges are all, execute, and select.
• <object name>: It is the name of the database object like table, view etc
• <username>: It is the name of the user to whom an access right is being granted.
• Public: It is used to grant access rights to all the users.
• Roles: These are a set of privileges grouped together.
• With Grant Option: It allows a user to grant access rights to other users.

REVOKE Command: It is used to remove user access rights or privileges to the database
objects.
• Syntax:
REVOKE <privilege name>
ON <object name>
FROM {username | PUBLIC | role name};
Implementing View:
CREATE VIEW TESTING AS
SELECT PATIENT_269.P_ID, P_NAME, TEST_269.T_ID, T_NAME, NURSE_269.NUR_ID, NUR_NAME
FROM PATIENT_269, TEST_269, NURSE_269, PERFORMS, IS_GIVEN
WHERE GENDER = 'F' AND
PATIENT_269.P_ID = IS_GIVEN.P_ID AND
TEST_269.T_ID = IS_GIVEN.T_ID AND
NURSE_269.NUR_ID = PERFORMS.NUR_ID AND
PERFORMS.T_ID = TEST_269.T_ID;

Implementing Trigger: A trigger is created which will automatically assign the new patient to
the emergency ward if its age is more than 60 years.

• Creating Trigger
CREATE TRIGGER ASSIGN_WARD
AFTER INSERT ON PATIENT_269
FOR EACH ROW
BEGIN
IF (:NEW.AGE > 60)
THEN INSERT INTO ASSIGNED VALUES(205, :NEW.P_ID, NULL);
END IF;
END;

• Inserting new value


INSERT INTO PATIENT_269 VALUES (32,'NATWAR',67,'M','TB',TO_DATE('13/03/2020', 'DD/MM/YY
YY'),'KASHMIRI GATE',8278978520);

• Checking the Assigned Table after Insertion


SELECT * FROM ASSIGNED;
Implementing Control Commands:
• Creating a new user
CREATE USER user1 IDENTIFIED BY tiger;
-- USER CREATED

• Giving new user, the rights to insert, select, update and delete Department Table
GRANT INSERT, DELETE
ON DEPARTMENT_269
TO user1;

• Impact
INSERT INTO DEPARTMENT_269 VALUES (106, 'GERMOLOGY', 'DR. VYAS', 8714526307);
-- INSERTION SUCCESSFULL

• Revoking the Delete privilege


REVOKE DELETE
ON DEPARTMENT_269
FROM user1;

• Impact
DELETE DEPARTMENT_269;

Learnings and Findings:


1. We learnt the functioning of TRIGGER, VIEW, GRANT, REVOKE commands and where
and how they are used.
2. We also the implementation of these and where they are useful.

Discussion:
From this experiment we learnt about different commands VIEW, TRIGGER, GRANT,
REVOKE. We also learnt how to implement them and why would have been developed. We
discussed several areas of their implementation.

Conclusion:
All the commands, VIEW, GRANT, REVOKE, TRIGGER were successfully understood and
implemented on the database in the experiment.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy