Dbms Complete Notes
Dbms Complete Notes
INTRODUCTION TO DBMS:
What is data?
• Data is nothing but facts and statistics stored or free flowing over a network,
generally it's raw and unprocessed.
• Data becomes information when it is processed, turning it into something
meaningful.
• What is database: The database is a collection of inter-related data which is used to
retrieve, insert and delete the data efficiently.
• It is also used to organize the data in the form of a table, schema, views, and
reports, etc.
• Using the database, you can easily retrieve, insert, and delete the information.
• For example: The college Database organizes the data about the admin, staff,
students and faculty etc.
What is dbms?
File System
DBMS
DBMS is a collection of data. In DBMS, the user File system is a collection of data. In this system, the
is not required to write the procedures. user has to write the procedures for managing the
database.
DBMS gives an abstract view of data that hides File system provides the detail of the data
the details. representation and storage of data.
DBMS provides a crash recovery mechanism, File system doesn't have a crash mechanism, i.e., if
i.e., DBMS protects the user from the system the system crashes while entering some data, then the
failure. content of the file will lost.
DBMS provides a good protection mechanism. It is very difficult to protect a file under the file
system.
DBMS contains a wide variety of sophisticated File system can't efficiently store and retrieve the
techniques to store and retrieve the data. data.
DBMS takes care of Concurrent access of data In the File system, concurrent access has many
using some form of locking. problems like redirecting the file while other deleting
some information or updating some information.
• The main purpose of database systems is to manage the data. Consider a university
that keeps the data of students, teachers, courses, books etc. To manage this data
we need to store this data somewhere where we can add new data, delete unused
data, update outdated data, retrieve data, to perform these operations on data we
need a Database management system that allows us to store the data in such a way
so that all these operations can be performed on the data efficiently.
Characteristics of DBMS
• Data stored into Tables: Data is never directly stored into the database. Data is
stored into tables, created inside the database.
• Reduced Redundancy: In the modern world hard drives are very cheap, but earlier
when hard drives were too expensive, unnecessary repetition of data in database
was a big problem. But DBMS follows Normalisation which divides the data in
such a way that repetition is minimum.
• Data Consistency: On Live data, i.e. data that is being continuosly updated and
added, maintaining the consistency of data can become a challenge. But DBMS
handles it all by itself.
Advantages of DBMS
• Controls database redundancy: It can control data redundancy because it stores all
the data in one single database file and that recorded data is placed in the database.
• Data sharing: In DBMS, the authorized users of an organization can share the data
among multiple users.
• Easily Maintenance: It can be easily maintainable due to the centralized nature of
the database system.
• Reduce time: It reduces development time and maintenance need.
• Backup: It provides backup and recovery subsystems which create automatic
backup of data from hardware and software failures and restores the data if
required.
• multiple user interface: It provides different types of user interfaces like graphical
user interfaces, application program interfaces
Disadvantages of DBMS
• Cost of Hardware and Software: It requires a high speed of data processor and
large memory size to run DBMS software.
• Size: It occupies a large space of disks and large memory to run them efficiently.
• Complexity: Database system creates additional complexity and requirements.
• Higher impact of failure: Failure is highly impacted the database because in most
of the organization, all the data stored in a single database and if the database is
damaged due to electric failure or database corruption then the data may be lost
forever.
Database systems are made-up of complex data structures. To ease the user interaction with
database, the developers hide internal irrelevant details from users. This process of hiding
irrelevant details from user is called data abstraction.
Logical level: This is the middle level of 3-level data abstraction architecture. It describes
what data is stored in database.
View level: Highest level of data abstraction. This level describes the user interaction
with database system.
Instance and schema in DBMS
Definition of instance:
The data stored in database at a particular moment of time is called instance of database.
Database schema defines the variable declarations in tables that belong to a particular
database; the value of these variables at a moment of time is called the instance of that
database.
DBMS ARCHITECTURE:
• Database management systems architecture will help us understand the components of
database system and the relation among them.
• The architecture of DBMS depends on the computer system on which it runs.
• the basic client/server architecture is used to deal with a large number of PCs, web
servers, database servers and other components that are connected with networks.
• The client/server architecture consists of many PCs and a workstation which are
connected via the network.
• DBMS architecture depends upon how users are connected to the database to get their
request done.
3-Tier Architecture
• In three-tier architecture, another layer is present between the client machine and
server machine.
• In this architecture, the client application doesn’t communicate directly with the
database systems present at the server machine, rather the client application
DATA MODELS:
• Data Model is the modeling of the data description, data semantics, and
consistency constraints of the data.
• It provides the conceptual tools for describing the design of a database at each
level of data abstraction.
• Therefore, there are following four data models used for understanding the
structure of the database:
• Hierarchical database
• Network database
• Relational database ER model database
Hierarchical DBMS
In a Hierarchical database, model data is organized in a tree-like structure. Data is Stored
Hierarchically (top down or bottom up) format. Data is represented using a parent-child
relationship. In Hierarchical DBMS parent may have many children, but children have
only one parent.
Relational model
Relational DBMS is the most widely used DBMS model because it is one of the easiest. This
model is based on normalizing data in the rows and columns of the tables. Relational model
stored in fixed structures and manipulated using SQL.
Entity-Relationship Model
Entity-Relationship (ER) Model is based on the notion of real-world entities and relationships
among them. While formulating real-world scenario into the database model, the ER Model
creates entity set, relationship set, general attributes and constraints.
DBMS languages
CREATE TABLE:
The CREATE TABLE statement is used to create a table in SQL. We know that a table
comprises of rows and columns. So while creating tables we have to provide all the
information to SQL about the names of the columns, type of data to be stored in columns,
size of the data etc. Let us now dive into details on how to use CREATE TABLE statement
to create tables in SQL.
Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
Example Query:
This query will create a table named Students with three columns, ROLL_NO, NAME and
SUBJECT.
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
DROP:
DROP is used to delete a whole database or just a table.The DROP statement destroys the
objects like an existing database, table, index, or view.
DML(Data Manipulation Language) : The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data Manipulation
Language and this includes most of the SQL statements. SELECT Statement
select statement is used to fetch data from relational database. A relational database is
organized collection of data. As we know that data is stored inside tables in a database. SQL
select statement or SQL select query is used to fetch data from one or more than one tables.
Column names and values both: In the second method we will specify both the columns
which we want to fill and their corresponding values as shown below:
EX2;
SQL> UPDATE EMPLOYEES
SET EMP_SALARY = 120000
WHERE EMP_NAME = 'Apoorv';
DELETE Statement
The DELETE Statement in SQL is used to delete existing records from a table. We can delete
a single record or multiple records depending on the condition we specify in the WHERE
clause. Basic Syntax:
DELETE FROM table_name WHERE some_condition;
Deleting single record: Delete the rows where NAME = ‘Ram’. This will delete only the first
row.
DELETE FROM Student WHERE NAME = 'Ram';
Deleting multiple records: Delete the rows from the table Student where Age is 20. This will
delete 2 rows(third row and fifth row).
DELETE FROM Student WHERE Age = 20;
Delete all of the records: There are two queries to do this as shown
below, query1: "DELETE FROM Student"; query2: "DELETE * FROM
Student";
DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE
which mainly deals with the rights, permissions and other controls of the database system.
Examples of DCL commands:
DATABASE MANAGEMENT SYSTEMS Page 22
GRANT-gives user’s access privileges to database.
REVOKE-withdraw user’s access privileges given by using the GRANT command.
TCL(transaction Control Language) : TCL commands deals with the transaction within the
database.
Examples of TCL commands:
COMMIT– commits a Transaction.
ROLLBACK– rollbacks a transaction in case of any error occurs.
SAVEPOINT–sets a savepoint within a transaction.
SET TRANSACTION–specify characteristics for the transaction.
• Any access to the stored data is done by the data manager. A user’s request for data is-
received by the data manager, which detern1ines the physical record required. The
decision as 10 which physical record is needed may require some preliminary
consultation of the database and/or the data dictionary prior to the access of the actual
data itself.
• The data manager sends the request for a specific physical record to the file manager.
The file manager decides which physical block of secondary storage devices contains
the required record and sends the request for the appropriate block to the disk
manager. A block is a unit of physical input/output operations between primary and
secondary storage. The disk manager retrieves the block and sends it to the file
manager, which sends the required record to the data manager.
Database Administrators
The life cycle of database starts from designing, implementing to administration of it. A
database for any kind of requirement needs to be designed perfectly so that it should work
without any issues. Once all the design is complete, it needs to be installed. Once this step is
complete, users start using the database. The database grows as the data grows in the
database. When the database becomes huge, its performance comes down. Also accessing the
data from the database becomes challenge. There will be unused memory in database, making
the memory inevitably huge. These administration and maintenance of database is taken care
by database Administrator – DBA.
A DBA has many responsibilities. A good performing database is in the hands of DBA.
Database Administrators coordinate all the activities of the database system. They have
all the permissions.
Tasks of DBA
• Creatingtheschema
• Specifying integrity constraints
• Storage structure and access method definition Granting permission to other
users.
Transaction Management?
ACID Properties are used for maintaining the integrity of database during transaction
processing. ACID in DBMS stands for Atomicity, Consistency, Isolation, and Durability.
• Atomicity: A transaction is a single unit of operation. You either execute it
entirely or do not execute it at all. There cannot be partial execution.
• Consistency: Once the transaction is executed, it should move from one
consistent state to another.
As query processing includes certain activities for data retrieval. Initially, the given user
queries get translated in high-level database languages such as SQL. It gets translated into
expressions that can be further used at the physical level of the file system. After this, the
actual evaluation of the queries and a variety of query -optimizing transformations and takes
place.
Query Evaluation Plan o In order to fully evaluate a query, the system needs to
construct a query evaluation plan.
o The annotations in the evaluation plan may refer to the algorithms to be used for the
particular index or the specific operations.
o Such relational algebra with annotations is referred to as Evaluation Primitives. The
evaluation primitives carry the instructions needed for the evaluation of the operation.
o Thus, a query evaluation plan defines a sequence of primitive operations used for
evaluating a query. The query evaluation plan is also referred to as the query
execution plan. o A query execution engine is responsible for generating the
output of the given query. It takes the query execution plan, executes it, and finally
makes the output for the user query.
Optimization o The cost of the query evaluation can vary for different types of
queries. Although the system is responsible for constructing the evaluation plan, the user
The table name and column names are helpful to interpret the meaning of values in each row.
The data are represented as a set of relations. In the relational model, data are stored as tables.
However, the physical storage of the data is independent of the way the data are logically
organized.
1. Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree
of the relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
Here are some reasons for using sql key in the DBMS system.
• Keys help you to identify any row of data in a table. In a real-world application, a
table could contain thousands of records. Moreover, the records could be
duplicated.
Keys ensure that you can uniquely identify a table record despite these challenges.
• Allows you to establish a relationship between and identify the relation between
tables
• Help you to enforce identity and integrity in the relationship.
Types of Keys in Database Management System
There are mainly seven different types of Keys in DBMS and each key has its different
functionality:
• Super Key - A super key is a group of single or multiple keys which identifies
rows in a table.
• Primary Key - is a column or group of columns in a table that uniquely identify
every row in that table.
Syntax
The syntax to create a primary key using the ALTER TABLE statement in SQL is:
ER model
o ER model stands for an Entity-Relationship model. It is a high-level data model.
This model is used to define the data elements and relationship for a specified
system. o It develops a conceptual design for the database. It also develops a
very simple and easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an
entityrelationship diagram.
For example, Suppose we design a school database. In this database, the student will be an
entity with attributes like address, name, id, age, etc. The address can be another entity with
attributes like city, street name, pin code, etc and there will be a relationship between them.
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak entity doesn't contain
any key attribute of its own. The weak entity is represented by a double rectangle.
2. Attribute
For example, id, age, contact number, name, etc. can be attributes of a student.
a. Key Attribute
The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.
b. Composite Attribute
An attribute that composed of many other attributes is known as a composite attribute. The
composite attribute is represented by an ellipse, and those ellipses are connected with an
ellipse.
An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
An attribute that can be derived from other attribute is known as a derived attribute. It can be
represented by a dashed ellipse.
For example, A person's age changes over time and can be derived from another
attribute like Date of birth.
A relationship is used to describe the relation between entities. Diamond or rhombus is used
to represent the relationship
When only one instance of an entity is associated with the relationship, then it is known
as one to one relationship.
For example, A female can marry to one male, and a male can marry to one female.
b. One-to-many relationship
For example, Scientist can invent many inventions, but the invention is done by the only
specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of an entity on
the right associates with the relationship then it is known as a many-to-one relationship.
For example, Student enrolls for only one course, but a course can have many students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one instance of an entity
on the right associates with the relationship then it is known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have many employees.
Notation of ER diagram
Database can be represented using the notations. In ER diagram, many notations are used to
express the cardinality. These notations are as follows:
of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to
be performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
Example:
o This is because the primary key value is used to identify individual rows in relation
and if the primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.
3. Referential
4. Key constraints o Keys are the entity set that is used to identify an entity within its
ER Design Issues
• ER design issues need to be discussed for better ER- design
• users often mislead the concept of the elements and the design process of the ER
diagram. Thus, it leads to a complex structure of the ER diagram and certain
issues that does not meet the characteristics of the real-world enterprise model.
The use of an entity set or attribute depends on the structure of the real-world enterprise that
is being modelled and the semantics associated with its attributes. It leads to a mistake when
the user use the primary key of an entity set as an attribute of another entity set. Instead, he
should use the relationship to do so. Also, the primary key attributes are implicit in the
relationship set, but we designate it in the relationship sets.
The cardinality ratios can become an affective measure in the placement of the relationship
attributes. So, it is better to associate the attributes of one-to-one or one-to-many relationship
sets with any participating entity sets, instead of any relationship set.
Conceptual design
Conceptual design is the first stage in the database design process. The goal at this stage is to
design a database that is independent of database software and physical details. The output of
this process is a conceptual data model that describes the main data entities, attributes,
relationships, and constraints of a given problem domain.
Keep in mind the following minimal data rule:
UNIT-2
Relational Algebra
• Relational Algebra is procedural query language, which takes Relation as input and
generates relation as output. Relational algebra mainly provides theoretical
foundation for relational databases and SQL.
• Relational algebra is a procedural query language, it means that it tells what data to
be retrieved and how to be retrieved.
• Relational Algebra works on the whole table at once, so we do not have to use loops
etc to iterate over all the rows (tuples) of data one by one.