DBMS LAB File
DBMS LAB File
PRACTICAL FILE
SUBMITTED TO : SUBMITTED BY
:
THEORY :
1. SQL
What is SQL ?
Structured Query Language (SQL) is an acronym for Structured Query
Language. In a relational data management system, it is used to store and manage
data (RDBMS). It is the Related databases' standard language. Creates, reads,
updates, and deletes information and tables for the user. SQL is the standard
language for all RDBMS, including MySQL, Informix, Oracle, MS Access, and
SQL Server. SQL allows users to query data in a variety of methods, including
utilizing English statements.
2. DATABASE
What is DATABASE ?
A database is a collection of interconnected data that organizes data into tables,
views, schemas, and reports and enables for efficient data retrieval, insertion, and
deletion from a database.
● Distributed Database
● Cloud Database
● Relational Database
● NoSQL Database
● Centralized Database
● End User database
● Commercial Database
● Personal Database
● Operational Database
Applications Of DATABASE :
● The police maintain a database with information on all known criminals,
such as crimes they've committed.
● A hospital will keep track of all of its patients' information in a database,
such as their medical history.
● Schools employ a database to keep track of information about their
students, such as how many days they've missed due to illness.
● The government keeps track of people's income tax payments in a
database.
● A database is used to keep track of all the drivers in central London who
have (or haven't) paid the Congestion Charge.
AIM :
Introduction to various softwares of Database Management System and
Database Language.
THEORY :
DBMS Software : A database management system, often known as a DBMS, is a
computer programme that stores, manipulates, and manages data in a database. A
database management system (DBMS) is a software programme that links a user
to a database and allows them to interact with it. The database management
system (DBMS) can be used to create, read, update, and delete data.
1. MySQL
2. ORACLE
3. MongoDB
4. Amazon RDS
● The database schema is specified using DDL. It's used to build database
tables, schemas, indexes, and constraints, among other things.
DCL is a programming language that is used to grant and revoke database user
access.
In practice, a data definition language, a data manipulation language, and a data
control language are all components of a single database language, such as SQL.
LEARNING :
We've studied about the various database management systems (DBMS) that are
now in use. We also taught about database languages and the operations that go
along with them.
CONCLUSION :
Oracle is a well-known and extensively used database management system that
supports all four types of database languages, therefore we'll be using it to
conduct our experiment.
EXPERIMENT-3
AIM :
Introduction to ER Diagram, Symbol table.
THEORY :
ER Diagram: ENTITY RELATIONAL (ER) MODEL is a high-level conceptual
data model diagram. ER modeling helps you to analyze data requirements
systematically to produce a well-designed database. The Entity-Relation model
represents real- world entities and the relationship between them. It is considered
a best practice to complete ER modeling before implementing your database. ER
modeling helps you to analyze data requirements systematically to produce a
well-designed database. So It is considered a best practice to complete ER
modeling before implementing your database. It displays the relationships of
entity sets stored in a database. In other words, we can say that ER diagrams help
you to exp. the logical structure of databases. At first look an ER diagram looks
very similar to the flowchart. However ER Diagram includes many specialized
symbols, and its meanings make this model unique.
ER DIAGRAM
ADVANTAGES OF ER DIAGRAM :
● Here. are prime reasons for using the ER Diagram.
● Helps you to define terms mist. to entity relationship modeling ➢ Provide
a preview of how all your tables should connect , what fields are going to
be on each table.
● Helps to describe entities attributes Relationships.
● ER diagrams are translatable into relation tables which allows you to build
databases quickly.
● ER diagrams can be used by database designers as a blueprint for
implementing data in specific software applications.
● The database designer gains a better understanding of the ERD and allows
you to communicate with the logical structure of the database to users.
2. Database Troubleshooting:
ER diagrams are used to analyze existing databases to find and resolve problems
in logic or deployment. Drawing the diagram should reveal where it’s going
wrong.
5. Education:
Databases are today’s method of storing relational information for educational
purposes and later retrieval, so ER Diagrams can be valuable in planning those
data structures.
6. Research:
Since so much research focuses on structured data, ER Diagram can play a key
role in setting up a useful database to analyze the data.
Entity type:
A group of definable things, such as students or athletes, whereas the entity
would be the specific student or athlete. Other examples: customers, cars or
products.
Entity set: Same as an entity type, but defined at a particular point in time, such
as students enrolled in a class on the first day. Other examples: Customers who
purchased last month, cars currently registered in Germany. A related term is
instance, in which the specific person or car would be an instance of the entity
set.
Entity categories:
Entities are categorized as strong, weak or associative. A strong entity can be
defined solely by its own attributes, while a weak entity cannot. An associative
entity associates entities (or elements) within an entity set.
Entity keys:
Refers to an attribute that uniquely defines an entity in an entity set. Entity keys
can be super, candidate or primary. Super key: A set of attributes (one or more)
that together define an entity in an entity set. Candidate key: A minimal super
key, meaning it has the least possible number of attributes to still be a super key.
An entity set may have more than one candidate key. Primary key: A candidate
key chosen by the database designer to uniquely identify the entity set. Foreign
key: Identifies the relationship between entities.
Relationship:
How entities act upon each other or are associated with each other. Think of
relationships as verbs. For example, the named student might register for a
course. The two entities would be the student and the course, and the relationship
depicted is the act of enrolling, connecting the two entities in that way.
Relationships are typically shown as diamonds or labels directly on the
connecting lines.
Recursive relationship:
The same entity participates more than once in the relationship.
Cardinality views:
Cardinality can be shown as look-across or same-side, depending on where the
symbols are shown.
Cardinality constraints:
The minimum or maximum numbers that apply to a relationship.
SYMBOL TABLE:
● The information is collected by the analysis phases of the compiler and is used
by synthesis phases of the compiler to generate code.
Lexical Analysis: Creates new table entries in the table, examples like entries
about tokens.
Semantic Analysis: Uses available information in the tab to check for semantics
i.e. to verify that expressions and assignments are semantically correct(type
checking) and update it accordingly.
Intermediate Code generation: Refers symbol table for knowing how much and
what type of run-time is allocated and the table helps in adding temporary
variable information.
Code Optimization: Uses information present in the symbol table for machine
dependent optimization.
LEARNING :
We've studied about the Entity-Relationship diagram . We also learnt about the
uses of entity-relationship diagrams along with its advantages.Also learnt about
the components and features of entity-relationship diagrams.
CONCLUSION :
Entity-Relationship Diagram is a great tool to represent the relational model and
schema.ER diagram helps us to find the relation between various entities and at
the same time define various attributes for each entity.
EXPERIMENT-4
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.
The available constraints in SQL are:
● NOT NULL:
This constraint tells us 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 the 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 a: value is specified
by the user.
NOT NULL:
Ensures that a column cannot have NULL value. By default, a column can hold
NULL values. If we don't enter any value in a particular column, it will store
NULL if the default value has not been specified. The NULL value is omitted in
various operations in a database. To ensure that a column does not contain a
NULL value, NOT NULL constraint is used. NOT NULL constraint makes sure
that a column does not hold NULL value. When we don't provide a value for a
particular column while inserting a record into a table, it takes a NULL value by
default. By specifying NULL constraint, we can be sure that a particular
column(s) cannot have NULL values.
UNIQUE :
Ensures that all values in a column are different. UNIQUE constraint
ensures that all the values stored in a column are different from each other. The
UNIQUE Constraint prevents two records from having identical values in a
column, i.e., the values must not be repeated. UNIQUE Constraint enforces a
column or set of columns to have unique values. If a has a unique constraint, it
means that particular column cannot have a duplicate value table.
DEFAULT :
Provides a default value for a column when none is specified. DEFAULT
constraint is used to
provide a default value for the fields of a table. The DEFAULT Constraint is used
to fill a column
with a default and fixed value. The value will be added to all new records when
no other value is
provided.
CHECK :
The CHECK constraint is used to limit the value range that can be placed in a
column. If you define a CHECK constraint on a single column it allows only
certain values for this column. If you define a CHECK constraint on a table it can
limit the values in certain columns based on values in other columns in the row.
PRIMARY KEY :
Uniquely identifies each row/record in a database table. A primary key constraint
uniquely identifies each row/record in a database table. Primary keys must
contain unique values. A primary key column cannot have NULL values. A table
can have only one primary key, which may consist of single or multiple fields.
When multiple fields are used as a primary key, they are called a composite key.
The field with a PRIMARY KEY constraint can be used to uniquely identify each
record of a table in various database operations.
This query will create a table Employees(Emp_Id, Emp_Name and Salary) with
Emp_Id field as PRIMARY KEY constraint.
To create a PRIMARY KEY constraint on the “EMP_ID” column when
Employees table already exists, use the following SQL syntax:
FOREIGN KEY:
Uniquely identifies a row/record in any of the given database tables. Foreign Key
Constraint is used to link two tables. It is used to establish a relationship between
the data in two tables. A Foreign Key is a column or a combination of columns
whose values match a Primary Key in a different table. The table containing the
FOREIGN KEY is called the child table, and the table containing the candidate
key is called the parent table. FOREIGN KEY is used to enforce referential
integrity. It is used to prevent actions that would destroy links between tables. It
also prevents invalid data from being inserted into the foreign key column,
because it has to be one of the values contained in the table it points to.
This query creates a table Employees(Emp_Id, Emp_Name and Dept_Id) with
Dept_Id as FOREIGN KEY which links the Employees table to the Department
table.
KEY CONSTRAINTS :
● DOMAIN CONSTRAINTS: A set of rows and columns that contain data
Columns in the tables have a unique name, often referred to as attributes in
DBMS. A domain is a unique set of values permitted for an attribute in a
table. Domain constraints are user defined data type and we can define
them like this:
Example:
For example I want to create a table “student_info” with “stu_id” field having
value greater than 100, I can
create a domain and table like this:
INTEGRITY CONSTRAINT :
Integrity constraints are used to ensure accuracy and consistency of the data in a
relational database. Data integrity is handled in a relational database through the
concept of referential integrity. There are many types of integrity constraints that
play a role in Referential Integrity (RI). These constraints include Primary Key,
Foreign Key, Unique Constraints and other constraints which are mentioned
above.
LEARNING:
Learnt about different types of constraints in SQL with examples of commands
for each constraint.
CONCLUSION::
Constraints are the rules enforced on the data columns of a table. These are used
to limit the type of data that can go into a table. The purpose of constraints is to
maintain the data integrity during an update/delete/insert into a table.
EXPERIMENT-5
AIM :
CASE STUDY 1: Hospital Management System.
THEORY :
A good Hospital management system will help to keep account of immense data
related to its patients, departments, doctors, nurses and wards.
ASSUMPTIONS :
● A doctor may treat many patients but a single patient can be treated by
many doctors.
● A Department can have more than one doctor but a doctor can work in one
department only.
● Many patients are admitted in a single ward, each patient is admitted in
exactly one ward.
● A ward may have multiple wards but all nurses work in a specified ward
only.
● A patient may own reports from different departments.
LEARNING :
Learned about tables used to implement hospital management systems using
ER-diagrams and assumptions for the relations between different entities.
CONCLUSION :
The Hospital Management system can be implemented efficiently using the
ER-diagram with appropriate entities.
EXPERIMENT-6
● Department Table
Dept_id, contact no, dept_name, hod_name, hod_id
● Doctor Table
Doctor_id, doctor_name, doctor_age, doctor_salary, doctor_experience,
doctor_contact.
● Patient Table
patient_id, patient_name, patient_age.
● Ward Table
ward_id , ward_name , ward_capacity, ward_quality.
● Nurses Table
Nurse_id, nurse_name , nurse_experience, nurse_age, nurse_salary.
● Doctors:Departments(1:N)
● Nurse:Patients(1:N)
● Doctor:Patients(1:N)
● patient:ward(1:N)
● Nurses:Wards(M:N)
LEARNING :
Learned to implement the hospital management system using an ER diagram.
Also learnt how to allocate attributes to each entity and how to define
relationships between different entities.
CONCLUSION :
We successfully implemented the hospital management system using ER-diagram
and defined relations between different entities along with relation types.
TABLES (Pre-Requisite Data)
Department Table
create table department_266( dept_id int primary key, dept_name varchar(40) not
null, dept_hod_name varchar(40) not null, dept_contact int not null);
desc department_266;
Doctor Table
create table doctor_266(doctor_id int primary key,doctor_name varchar(40) not
null,doctor_salary int,doctor_gender varchar(10) not null,doctor_experience int not
null,doctor_specialisation varchar(40) not null);
desc doctor_266;
Patients Table
create table patients_266(patient_id int primary key,patient_name varchar(40) not
null,patient_gender varchar(10) not null,patient_age int not null,ward_id int);
desc patients_266;
Wards Table
create table wards_266(ward_id int primary key,ward_name varchar(40) not
null,ward_capacity int not null);
desc wards_266;
Nurses Table
create table nurses_266(nurse_id int primary key,nurse_name varchar(40) not null,
nurse_salary int,nurse_gender varchar(10) not null,nurse_experience int not null,
nurse_age int not null,nurse_contact int not null);
desc nurses_266;
Bill Table
create table bill_266(bill_id int primary key,patient_id int not null,doctor_id int not
null,ward_id int, bill_amount int not null);
desc bill_266;
WorksAt Table
create table worksat ( ward_id INT, nurse_id INT, PRIMARY KEY(ward_id,
nurse_id), FOREIGN KEY(ward_id) REFERENCES wards_266(ward_id) ON
DELETE CASCADE,FOREIGN KEY(nurse_id) REFERENCES
nurses_266(nurse_id) ON DELETE CASCADE);
desc worksat;
EXPERIMENT-7
THEORY :
DDL COMMANDS
In SQL, DDL means Data Definition Language. The Sql Server DDL commands are
used to create and modify the structure of a database and database objects.
CREATE :
Creates a database, table, triggers, index, functions, stored procedures, etc.
create table department_266( dept_id int primary key, dept_name varchar(40) not
null, dept_hod_name varchar(40) not null, dept_contact int not null);
DROP :
This SQL DDL command helps to delete objects. For example, delete tables, delete a
database, etc.
create table testing_table266 (test_id int, name varchar(20));
desc testing_table266;
TRUNCATE :
This SQL DDL command removes records from tables.
truncate table doctor_266;
DML COMMANDS
● DML means Data Manipulation Language in Sql Server. As its name suggests,
these Sql Server DML commands will perform data manipulation manipulate data
presented in the server).
SELECT :
This SQL DML command selects records or data from a table.
UPDATE :
This SQL DML command will update existing records within a table
DELETE :
Delete unwanted records from a table.
CONCLUSION :
To implement the tables and manage the data in MYSQL , DDL and DML commands
are very important . DDL commands help to define the data and create tables, DML
commands help to manipulate the data and update it according to convenience of the
user. Hence, we can conclude that these commands are essential in DBMS.
EXPERIMENT-8
THEORY :
Simple Queries in SQL involves :
1. SQL Queries using logical operators and operations.
2. SQL Query using group by functions.
3. SQL Query for group by functions.
Pre-requisite data :
DEPARTMENT TABLE
Queries
1.Name of the hod of the department whose dept_id is between
200 and 500.
QUERY
select count(doctor_id), doctor_specialisation from doctor_266 group by
doctor_specialisation;
Queries
1.Total capacity of all the wards combined.
select sum(ward_capacity) from wards_266;
2.Average capacity of all the wards in the hospital
LEARNING :
Learned about different types of simple queries like using logical operators , group by
and aggregate functions and used oracle tables and commands to implement these
queries taking examples of tables from the university management system analyzed
earlier.
CONCLUSION :
Implementation of simple queries with the help of operators , group by and aggregate
functions is very necessary to completely understand and get grasp on basic concepts
before moving to complex queries and joins.
EXPERIMENT-9
THEORY :
A JOIN clause is used to join rows from two or more tables together based on a
related column. To merge records from two or more tables in a database, use the SQL
Joins clause. A JOIN is a method of joining fields from two tables by using values
that are shared by both.
A SQL join clause combines columns from one or more tables in a relational
database. It creates a set that can be saved as a table or used as it is. A JOIN is a
means for combining columns from one (self-table) or more tables by using values
common to each. ANSI-standard SQL specifies five types of JOIN: INNER, LEFT
OUTER, RIGHT OUTER, FULL OUTER and CROSS. As a special case, a table
(base table, view, or joined table) can JOIN to itself in a self-join.
A programmer declares a JOIN statement to identify rows for joining. If the
evaluated
predicate is true, the combined row is then produced in the expected format, a row set
or a temporary table.
1.INNER JOIN
INNER JOIN returns records that have matching values in both Tables.
Syntax :
Table :
2.LEFT JOIN
LEFT(OUTER) JOIN returns all records from the left table, and
the matched records from the right table.
Syntax :
3. RIGHT JOIN
RIGHT JOIN returns all records from the right table, and the matched Records.
Syntax :
select * from patients_266 right join wards_266 on patients_266.ward_id =
wards_266.ward_id;
Table :
4. FULL JOIN
FULL JOIN returns all records when there is a match in either the left or the right
table. A FULL JOIN returns all the rows from the joined tables, whether they are
matched or not i.e. you can say a full join combines the functions of a LEFT JOIN
and a RIGHT JOIN. Full join is a type of outer join that's why it is also referred to as
full outer join.
Syntax :
Theory :
Pre-requisite Data :
Count Command-Query
select count(patient_id), ward_id from patients_266 group by ward_id;
Pre-requisite Data :
Progress_report table
Queries
1. Count Command-Query
SQL Server comes with the following data types for storing a date or a date/time
value in the database:
Table used
Output :
Output :
➔ Length of string query (LENGTH())
Output :
Learning :
Learned about aggregate function , date and time Queries and string queries with
proper implementation.
Conclusion :
Aggregate function,Date and time queries and String queries are important and
simple queries. These can be easily implemented using sql database.
EXPERIMENT-11
Theory :
Data Control Language (DCL) is used to manage database rights. A user must
have rights to do any activity in the database, such as creating tables, sequences,
or views. GRANT and REVOKE commands are used to manage these
privileges. Only the database administrator has the authority to grant or revoke
these rights.
⮚ GRANT
GRANT is used to grant users access such as Select, All, and Execute on
database objects such as Tables, Views, Databases, and other objects in a SQL
Server.
⮚REVOKE
⮚VIEW
In SQL, views are referred to as virtual tables. Rows and columns are also part
of a view. To build the view, we may choose fields from one or more tables in
the database. A view can contain either particular rows depending on criteria or
all of the rows in a table.
1. Create a view:
2. Select view:
⮚TRIGGER
A trigger is a piece of procedural code, similar to a stored procedure, that is only
performed when a specific event occurs. A trigger can be triggered by a variety
of actions, such as adding, removing, or modifying a record.
To create a new trigger, use the CREATE TRIGGER command.
Triggers are classified into two types: before triggers, which execute the triggers
before the action is completed, and after triggers, which run the triggers after the
action is completed.
1. To create triggers:
2. To insert values in triggers
Learning :
Learned about grant and view command , view command and triggers.
Conclusion :
We learnt about the grant and revoke functions and how they work as a result of
this project. We also built views, learned how the trigger works, and successfully
implemented it in our hospital management system.