Practical File: CO 202: Database Management Systems
Practical File: CO 202: Database Management Systems
Practical File: CO 202: 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
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.
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.
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.
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.
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.
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
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.
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
M
P_NAME P_ID
COST
GENDER M P_ADD
CONTACT_NO
1 ADM_DATE
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
);
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)>;
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:
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:
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:
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:
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 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)>;
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)>
• Selecting those doctors whose id is greater than 10 and whose dept id is greater than 102.
• Retrieve patient name, disease and age of patients with age between 25 and 35.
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;
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.
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
• 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);
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>;
<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.
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;
• 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
• Impact
DELETE DEPARTMENT_269;
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.