Dbms Notes
Dbms Notes
NOTES
1
DBMS (Database Management System)
DBMS stands for "Database Management System." It's like a special software that
helps organize and manage large amounts of data. It is a software tool that helps
manage databases and store, organize, retrieve, and manipulate data efficiently.
A DBMS keeps your data safe and secure, making sure only authorized people can
access it. It also ensures that the data is accurate and consistent, so you don't end up
with conflicting or wrong information.
With a DBMS, many people can work with the data at the same time without getting
in each other's way. It allows for collaboration and prevents data from being
duplicated unnecessarily.
DBMS is a powerful tool that simplifies handling and organizing data, making it
useful in various fields like businesses, schools, and many other places where data
needs to be managed efficiently.
Advantages of DBMS:-
1. Data Organization: DBMS organizes data in a structured manner, making it
easier to store and retrieve information. This structured approach ensures data
integrity and reduces data redundancy.
4. Data Sharing: Multiple users can access the same data simultaneously in a
controlled manner. This enables collaboration and reduces data duplication.
5. Backup and Recovery: DBMS offers mechanisms for regular data backups and
easy data recovery in case of system failures or data loss.
Disadvantages of DBMS:-
1. Cost: Implementing a DBMS can be expensive, including licensing fees,
hardware requirements, and training costs for users.
2
2. Complexity: Managing a DBMS can be complex, requiring skilled personnel to
set up, maintain, troubleshoot the system.
and
4. Single Point of Failure: If the DBMS fails, it can affect the entire system and
lead to downtime, potentially disrupting business operations.
1. It's about the ability to change the logical structure or organization of the
database without affecting the applications or programs using it.
2. You can modify the way data is organized, add new tables, or change
relationships between tables without breaking the existing software that relies on
the database.
4. Application developers can focus on their tasks without worrying about the
underlying database changes, as long as the logical view remains consistent.
1. It refers to the ability to alter the physical storage or access methods of the
database without impacting the way data is represented in the applications.
3
2. You can change the storage hardware, database server, or storage optimizations
without needing to rewrite the application code.
3. Physical data independence shields the applications from the low-level technical
details of how data is stored and managed.
This is the topmost layer that interacts directly with the end-users or applications.
It defines how individual users or different software applications see and interact
with the database.
Each user or application can have its own customized view of the data, showing
only the relevant information needed for their specific tasks.
4
It provides a simplified and user-friendly interface, shielding users from the
complexities of the underlying database structure.
This is the middle layer that defines the overall logical structure and organization
of the entire database.
It represents the entire database as a single unified entity, irrespective of how the
data is physically stored.
It acts as an intermediary between the external level and the internal level,
translating user requests into database operations.
This is the bottommost layer that deals with the physical storage and retrieval of
data on the actual hardware.
It defines how the data is physically stored on disk, including data structures,
indexes, and access methods.
The internal level focuses on optimizing data storage and retrieval for efficient
performance.
Keys in DBMS
There are various types of keys that help establish relationships and ensure data
integrity within the database.
1. Primary Key:-
The primary key is a unique identifier for each record (or row) in a table.
5
It ensures that each row in the table has a distinct identity and can be uniquely
identified.
In a table, there can be only one primary key, and it cannot contain duplicate or
null values.
Example Consider a table named "Students" with columns "Student_ID,"
"Name," and "Age." If we use the "Student_ID" column as the primary key, it will
uniquely identify each student.
Student_ID Name Age
101 John 20
102 Emily 22
103 Michael 21
2. Foreign Key:-
A foreign key is a field in one table that refers to the primary key in another table.
It establishes a relationship between two tables, allowing data from one table to
reference data in another.
The foreign key ensures referential integrity, meaning that the value in the
foreign key column must match an existing value in the referenced table's
primary key.
Example Consider two tables, "Students" and "Courses." The "Courses" table
has a primary key "Course_ID," and the "Students" table has a foreign key
"Course_ID" to reference the courses taken by each student.
Students Table:-
Student_ID Name Age Course_ID
101 John 20 1
102 Emily 22 2
103 Michael 21 1
Courses Table:-
Course_ID Course_Name
1 Math
2 Science
6
3. Unique Key:-
A unique key is similar to a primary key but allows for one or more columns to
have unique values within a table.
Unlike the primary key, a table can have multiple unique keys.
It ensures that the specified columns have unique values but allows for null
values.
Example In the "Students" table, we might want to ensure that the "Student_ID"
and "Name" columns have unique values. In this case, both "Student_ID" and
"Name" can be unique keys.
Student_ID Name Age
101 John 20
102 Emily 22
103 Michael 21
4. Candidate Key:-
A candidate key is a column or set of columns that can uniquely identify each
record in a table, just like a primary key.
A table can have multiple candidate keys, but only one is chosen as the primary
key.
Example In the "Students" table, both "Student_ID" and "Email" might uniquely
identify each student. Therefore, both "Student_ID" and "Email" are candidate keys.
However, we can choose only one of them as the primary key.
Student_ID Name Age Email
101 John 20 abcd123@gmail.com
102 Emily 22 efgh456@gmail.com
103 Michael 21 ijkl789@gmail.com
5. Super Key:-
A super key is a set of one or more attributes that can uniquely identify records
in a table. It can include the primary key, candidate keys, or other attributes.
Unlike a primary key or candidate key, a super key may have additional attributes
that are not strictly necessary for uniqueness.
Example In the "Students" table, a super key could be the combination of
"Student_ID" and "Phone_Number." While "Student_ID" alone is sufficient for
7
uniqueness, adding "Phone_Number" creates a super key that still uniquely
identifies each student.
Student_ID Name Age Phone_Number
101 John 20 012345679
102 Emily 22 9876543210
103 Michael 21 1122443355
6. Composite Key:-
A composite key is a primary key or candidate key that consists of two or more
attributes (columns) working together to uniquely identify each record.
Example In the "Students" table, a composite key could be the combination of
"First_Name" and "Last_Name." Individually, "First_Name" and "Last_Name"
may not be unique, but together, they form a composite key that ensures uniqueness
for each student.
Student_ID First_Name Last_Name Age Email
101 John Miller 20 abcd123@gmail.com
102 Emily Heart 22 efgh456@gmail.com
103 James Bond 21 ijkl789@gmail.com
RDBMS (Rational Database Management System)
RDBMS stands for "Relational Database Management System." In simple
language, an RDBMS is a type of software that helps manage large amounts of data
in a structured and organized manner using a system of tables and relationships.
1. Tables: In an RDBMS, data is organized into tables, which are like spreadsheets
with rows and columns. Each table represents a specific type of data, such as
customers, products, or employees.
2. Rows and Columns: Each row in a table represents a single record or data entry,
while each column represents a specific attribute or characteristic of that data,
such as name, age, or address.
3. Keys: RDBMS uses keys to uniquely identify each record in a table. The primary
key ensures that no two records have the same identification, and foreign keys
are used to establish relationships between tables.
8
4. Relationships: RDBMS allows you to create relationships between tables. For
example, you can link customers to their orders or employees to their
departments using primary and foreign keys.
7. Data Security: RDBMS provides security features to control access to the data.
It allows you to define user roles and permissions, ensuring that only authorized
users can view or modify specific data.
8. Scalability: RDBMS can handle large amounts of data and can scale to support
growing data needs. It offers efficient data storage and retrieval mechanisms to
maintain performance as the database size increases.
Relational Model
The relational model is a way of organizing data in a database that follows a tabular
structure, where data is stored in tables with rows and columns. It was introduced
by Edgar F. Codd in the 1970s and has become the foundation for most modern
Database Management Systems (DBMS).
In the relational model:-
1. Tables (Relations): Data is represented using tables, also called "relations."
Each table is given a unique name and consists of rows (tuples) and columns
9
(attributes). Each row in the table represents a specific data entry, and each
column represents a specific attribute or characteristic of the data.
2. Primary Key: Each table has a column designated as the primary key. The
primary key uniquely identifies each row in the table, ensuring no two rows have
the same identifier.
3. Foreign Key: Tables can be related to each other using foreign keys. A foreign
key is a column in one table that refers to the primary key of another table,
creating a relationship between the two tables.
5. SQL (Structured Query Language): To interact with the data stored in the
relational database, you use SQL, which is a special language designed for
querying and manipulating relational databases.
6. Data Integrity and Constraints: The relational model allows you to enforce
various constraints on the data, such as ensuring unique values, restricting certain
data entries, and defining relationships between tables. This helps maintain data
integrity and prevents inconsistencies.
EMPLOYEE table:
11
Second Normal Form (2NF):-
In the 2NF, relational must be in 1NF.
In the second normal form, all non-key attributes are fully functional dependent on
the primary key
Example Let's assume, a school can store the data of teachers and the subjects
they teach. In a school, a teacher can teach more than one subject.
TEACHER table:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
Third Normal Form (3NF):-
A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
3NF is used to reduce the data duplication. It is also used to achieve the data
integrity.
12
If there is no transitive dependency for non-prime attributes, then the relation must
be in third normal form.
A relation is in third normal form if it holds atleast one of the following conditions
for every non-trivial function dependency X → Y.
X is a super key.
Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:-
EMPLOYEE_DETAIL table:
EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY
222 Harry 201010 UP Noida
333 Stephan 02228 US Boston
444 Lan 60007 US Chicago
555 Katharine 06389 UK Norwich
666 John 462007 MP Bhopal
Super key in the table above:-
{EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}....so
on
Candidate key: {EMP_ID}
N on-prime a tt ributes : In the given table, all attributes except EMP_ID are non-prime.
Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP
dependent on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY)
transitively dependent on super key(EMP_ID). It violates the rule of third normal
form.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMP_ID EMP_NAME EMP_ZIP
222 Harry 201010
333 Stephan 02228
444 Lan 60007
555 Katharine 06389
666 John 462007
EMPLOYEE_ZIP table:
13
EMP_ZIP EMP_STATE EMP_CITY
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Boyce Codd normal form (BCNF):-
BCNF is the advance version of 3NF. It is stricter than 3NF.
A table is in BCNF if every functional dependency X → Y, X is the super key of
the table.
For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Example Let's assume there is a company where employees work in more than
one department.
EMPLOYEE table:
EMP_ID EMP_COUNTRY EMP_DEPT DEPT_TYPE EMP_DEPT_NO
264 India Designing D394 283
264 India Testing D394 300
364 UK Stores D283 232
364 UK Developing D283 549
In the above table Functional dependencies are as follows:
EMP_ID → EMP_COUNTRY
EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate key: {EMP-ID, EMP-DEPT}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
364 UK
EMP_DEPT table:
14
EMP_DEPT DEPT_TYPE EMP_DEPT_NO
Designing D394 283
Testing D394 300
Stores D283 232
Developing D283 549
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:-
EMP_ID → EMP_COUNTRY
EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:-
For the first table: EMP_ID
For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
Now, this is in BCNF because left side part of both the functional dependencies is
a key.
15
STU_ID COURSE HOBBY
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two
independent entity. Hence, there is no relationship between COURSE and HOBBY.
In the STUDENT relation, a student with STU_ID, 21 contains two
courses, Computer and Math and two hobbies, Dancing and Singing. So there is a
Multi-valued dependency on STU_ID, which leads to unnecessary repetition of
data.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE table:
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY table:
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
Fifth normal form (5NF):-
A relation is in 5NF if it is in 4NF and not contains any join dependency and joining
should be lossless.
5NF is satisfied when all the tables are broken into as many tables as possible in
order to avoid redundancy.
5NF is also known as Project-join normal form (PJ/NF).
Example:-
16
SUBJECT LECTURER SEMESTER
Computer Anshika Semester 1
Computer John Semester 1
Math John Semester 1
Math Akash Semester 2
Chemistry Praveen Semester 1
In the above table, John takes both Computer and Math class for Semester 1 but he
doesn't take Math class for Semester 2. In this case, combination of all these fields
required to identify a valid data.
Suppose we add a new Semester as Semester 3 but do not know about the subject
and who will be taking that subject so we leave Lecturer and Subject as NULL. But
all three columns together acts as a primary key, so we can't leave other two columns
blank.
So to make the above table into 5NF, we can decompose it into three relations P1,
P2 & P3:
P1
SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math
P2
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMESTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
17
Cardinality of a Relationship
Cardinality is crucial in database design as it helps define the relationships between
different entities in a database. It describes how many records in one table are
related to how many records in another table.
Cardinality is expressed using notations like "one-to-one" (1:1), "one-to-many"
(1:N), and "many-to-many" (N:N).
One-to-One (1:1) Cardinality:-
In a one-to-one relationship, each record in one table is related to only one record
in the other table, and vice versa.
Example Consider two tables, "Person" and "Passport." Each person can have
only one passport, and each passport is issued to only one person. This is a one-to-
one relationship.
Functional Dependency
18
Functional dependency is a concept in database management that describes the
relationship between attributes (columns) in a table. It indicates how one attribute's
value uniquely determines another attribute's value.
Functional dependency is denoted as X -> Y, where X determines Y, meaning for
every unique value of X, there is a unique value of Y associated with it.
19
4. Multivalued Dependency:-
A multivalued dependency occurs when an attribute (Y) is functionally dependent
on an attribute (X), and X can have multiple values for each Y.
Example Consider a table "Student_Courses" with attributes: Student_ID and
Course_Name. If each student can be enrolled in multiple courses and each course
can have multiple students, then both Student_ID and Course_Name are
multivalued dependent on each other.
Generalization
It is the process of combining several entities with similar attributes and behaviors
into a more abstract entity. It allows us to represent common characteristics shared
by different entities as a single higher-level entity.
Example:-
Consider a database for a company that sells different types of vehicles, such as
cars, motorcycles, and bicycles. Instead of having separate tables for each vehicle
type, we can use generalization to create a higher-level entity called "Vehicle" that
contains common attributes like "Model," "Manufacturer," and "Price" shared by
all types of vehicles.
The specific attributes unique to each type, like "Number of Doors" for cars or
"Number of Wheels" for bicycles, can be represented in separate specialized tables.
Specialization
It is the opposite of generalization. It involves breaking down an abstract entity into
more specific sub-entities with distinct attributes and behaviors.
Example:-
Continuing with the vehicle example, we can use specialization to create specialized
entities like "Car," "Motorcycle," and "Bicycle," each representing a specific type
of vehicle. These specialized entities inherit the common attributes from the
20
"Vehicle" entity and add their own unique attributes, like "Number of Doors" for
cars or "Type of Handlebars" for bicycles.
Aggregation
It is a concept used to represent a relationship between a whole entity and its parts.
It allows us to treat a group of related entities as a single entity.
Example:-
Suppose we have a database for a school, where "Class" is an entity that contains
information about a specific class, and "Student" is another entity representing
individual students. We can use aggregation to create an entity called "Classroom,"
which aggregates all the students belonging to a particular class. The "Classroom"
entity doesn't have its own separate existence but represents a group of students
associated with a specific class.
21
DDL commands are used to define and manage the structure of the database and its
objects, like tables, indexes, and constraints.
Examples:-
CREATE TABLE: Used to create a new table with specified columns and data
types.
ALTER TABLE: Used to modify the structure of an existing table, such as
adding or dropping columns.
DROP TABLE: Used to delete an entire table and its data from the database.
MariaDB
MariaDB is an open-source relational database management system (RDBMS) and
a popular alternative to MySQL. It was developed by the original creators of
MySQL and is designed to be a drop-in replacement for MySQL, meaning it is
compatible with MySQL applications and tools.
Key Features:-
22
1. Open Source: MariaDB is open-source software, which means its source code
is freely available to the public. Anyone can view, modify, and distribute the
software as per the terms of the General Public License (GPL).
4. Data Security: MariaDB provides robust data security features, including user
authentication, role-based access control, and data encryption, ensuring that only
authorized users can access sensitive data.
7. Dynamic Columns and JSON Support: MariaDB offers dynamic columns and
native JSON support, allowing you to store and query JSON data directly in the
database.
10. Community and Support: MariaDB has a large and active community,
providing support, documentation, and continuous improvements to the
software.
23
Datatypes used in MariaDB
In MariaDB, data types define the type of data that can be stored in a column of a
table. Each column in a table is associated with a specific data type, which
determines what kind of data can be stored in that column.
Common data types:-
1. INTEGER (or INT): Used to store whole numbers, both positive and negative,
without any decimal places. For example, 10, -20, 1000, etc.
5. DATE: Used to store dates (year, month, and day) in the format 'YYYY-MM-
DD'. For example, '2023-08-03'.
6. TIME: Used to store time values in the format 'HH:MM:SS'. For example,
'12:30:45'.
7. DATETIME: Used to store both date and time values in the format 'YYYY-
MM-DD HH:MM:SS'. For example, '2023-08-03 12:30:45'.
8. BOOLEAN: Used to store boolean values, which can be either true (1) or false
(0).
9. BLOB: Used to store binary data, such as images, audio, or video files.
10. ENUM: Used to create a list of allowed values for a column. The column can
only store one of the predefined values.
24
SQL Commands
1. Create:-
The "CREATE" command is used to create a new database or a new table within an
existing database. When creating a table, you need to specify the table's name and
define its columns along with their data types.
Example:-
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100)
);
2. Insert:-
The "INSERT" command is used to add new data (rows) into an existing table. You
provide the values for each column to insert a new record.
Example:-
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
VALUES (1, 'John', 'Doe', 'john@example.com');
3. Update:-
The "UPDATE" command is used to modify existing data in a table. You specify
the column(s) to update and the new values.
Example:-
UPDATE Customers
SET Email = 'newemail@example.com'
WHERE CustomerID = 1;
4. Delete:-
25
The "DELETE" command is used to remove one or more rows from a table based
on certain conditions.
Example:-
DELETE FROM Customers
WHERE CustomerID = 1;
5. Drop:-
The "DROP" command is used to remove an existing database or table from the
database system permanently. This action cannot be undone, so be careful when
using it.
Example:-
DROP TABLE Customers;
6. Alter:-
The "ALTER" command is used to modify an existing table's structure, such as
adding or removing columns, changing data types, or altering constraints.
Example (adding a new column to the table):-
ALTER TABLE Customers
ADD PhoneNumber VARCHAR(15);
SQL Statements
1. SELECT Statement:-
The "SELECT" statement is used to retrieve data from a database table. It allows
you to specify which columns you want to see and filter the rows based on certain
conditions using the "WHERE" clause.
Example:-
SELECT FirstName, LastName, Age
FROM Customers
WHERE Age >= 18;
26
This query selects the first name, last name, and age of customers from the
"Customers" table where their age is greater than or equal to 18.
2. LIKE Clause:-
The "LIKE" clause is used with the "SELECT" statement to perform pattern
matching on text data. It allows you to search for values that match a specified
pattern using wildcard characters.
Example:-
SELECT ProductName
FROM Products
WHERE ProductName LIKE 'Apple%';
This query selects the product names from the "Products" table where the product
name starts with 'Apple'.
3. GROUP BY Clause:-
The "GROUP BY" clause is used with the "SELECT" statement to group rows
based on the values of one or more columns. It is often used with aggregate
functions like SUM, AVG, COUNT, etc., to perform calculations on the grouped
data.
Example:-
SELECT Category, AVG(Price) AS AvgPrice
FROM Products
GROUP BY Category;
This query groups the products based on their category and calculates the average
price for each category.
4. ORDER BY Clause:-
The "ORDER BY" clause is used with the "SELECT" statement to sort the result
set in ascending or descending order based on one or more columns.
Example:-
SELECT FirstName, LastName, Age
27
FROM Customers
ORDER BY Age DESC;
This query retrieves the first name, last name, and age of customers from the
"Customers" table and sorts them in descending order based on the age.
5. JOIN:-
A "join" is an operation used to combine data from two or more tables based on
related columns. Joins are crucial for querying data from multiple tables and
retrieving meaningful information.
There are different types of joins, each serving a specific purpose.
Consider two tables:-
"Employees" and "Departments," where the "Employees" table has columns
"Employee_ID," "Employee_Name," and "Department_ID," and the
"Departments" table has columns "Department_ID" and "Department_Name."
1. Inner Join:-
An inner join returns only the rows that have matching values in both tables based
on the specified condition (the join predicate).
Example To get a list of employees along with their corresponding department
names, we can perform an inner join on the "Employee_ID" column in the
"Employees" table and the "Department_ID" column in the "Departments" table.
28
Example To get a list of all employees along with their corresponding department
names (if available), we can perform a left join on the "Employee_ID" column in
the "Employees" table and the "Department_ID" column in the "Departments"
table.
6. UNION: -
The "UNION" operator combines the result sets of two or more SELECT statements
into a single result set. It removes duplicates from the combined result.
Example:-
SELECT FirstName
FROM Customers UNION SELECT FirstName
FROM Employees;
This statement will retrieve the first names of customers and employees and
combine them into a single result set, removing any duplicate names.
Lossy Decomposition:-
It is a method where data is divided into smaller parts, but some information is
intentionally discarded to reduce the overall size.
30
In this process, some level of quality loss occurs, and the original data cannot be
perfectly reconstructed.
Example:-
An example of lossy decomposition is image compression. When you take a high-
quality image and compress it to reduce its file size, you are using lossy
decomposition. During compression, some image details are removed to save space,
which results in a decrease in image quality. If you later decompress the image, it
won't be identical to the original because some information was lost in the
compression process.
Functional dependencies:-
1. StudentID -> StudentName
2. CourseID -> CourseName
Sub-Relation 2: "CourseInfo"
- CourseID (Primary Key)
- CourseName
Acid Properties
the ACID properties are a set of four essential characteristics that ensure the
reliability and consistency of transactions. A transaction is a sequence of operations
that are executed as a single unit of work.
The ACID properties stand for:-
1. Atomicity:-
It ensures that a transaction is treated as a single indivisible unit of work. Either all
the operations within the transaction are completed successfully, or none of them
are. If any part of the transaction fails, the entire transaction is rolled back, and the
database returns to its original state before the transaction started.
Example:-
Suppose a transaction involves transferring money from one bank account to
another. Atomicity ensures that if the withdrawal from the sender's account
succeeds, but the deposit into the receiver's account fails, the whole transaction is
rolled back, and the withdrawal is undone.
2. Consistency:-
Consistency ensures that a transaction takes the database from one valid state to
another. It means that the data must satisfy all the defined integrity constraints after
the successful execution of a transaction. If a transaction violates any integrity
constraints, it is rolled back to maintain data consistency.
Example:-
If a database has a constraint that all ages must be greater than zero, and a
transaction tries to insert a record with an age of -5, the transaction will be rolled
back to keep the database in a consistent state.
3. Isolation:-
33
Isolation ensures that each transaction is executed in isolation from other
transactions. It means that the operations of one transaction are not visible to other
transactions until the transaction is committed. This prevents interference between
concurrent transactions and maintains data integrity.
Example:-
If two transactions simultaneously try to modify the same record in the database,
isolation ensures that one transaction's changes are not visible to the other until both
transactions are completed.
4. Durability:-
Durability ensures that once a transaction is committed, its changes are permanent
and survive any subsequent failures, such as power outages or crashes. The changes
made by a committed transaction are stored in a non-volatile memory, like a hard
disk, to ensure they are retained even if the system fails.
Example:-
After a successful transaction that updates a customer's address, the new address
will be permanently stored in the database, and even if the system crashes later, the
address change will still be there when the system restarts.
NoSQL
NoSQL, short for "Not Only SQL," is a type of database management system that
differs from traditional relational databases (SQL databases). It is designed to
handle large volumes of unstructured or semi-structured data, providing a more
flexible and scalable approach to data storage and retrieval.
Advantages:-
1. Scalability: NoSQL databases are built to scale horizontally, meaning they can
handle increased amounts of data by adding more servers to the system. This
makes them well-suited for applications with growing data needs.
2. Flexibility: NoSQL databases can store and handle various types of data,
including documents, key-value pairs, column-family data, and graphs. They are
not bound by a fixed schema, making them adaptable to changes in data
requirements.
34
3. High Performance: NoSQL databases can achieve faster read and write
operations compared to traditional SQL databases, especially for certain use
cases like retrieving large sets of unstructured data.
Disadvantages:-
1. Limited Querying Capability: NoSQL databases often lack the powerful
querying capabilities found in SQL databases, making it more challenging to
perform complex data analysis.
3. Data Integrity Constraints: NoSQL databases may not enforce strong data
integrity constraints, which could lead to data inconsistencies if not handled
carefully.
5. Less Mature Ecosystem: NoSQL databases, especially the newer ones, may
have a less mature ecosystem compared to well-established SQL databases,
leading to fewer available tools, documentation, and community support.
4. Joins: Relational databases support complex joins, allowing you to retrieve data
from multiple tables based on specified relationships.
NoSQL Databases:-
1. Structure: NoSQL databases store data in a more flexible and dynamic way.
They can use various data models, such as documents, key-value pairs, column
families, or graphs, to handle different types of data.
4. Joins: NoSQL databases may not support complex joins like relational
databases. Instead, they often encourage denormalization and embedding data
for more efficient data retrieval.
36