0% found this document useful (0 votes)
21 views52 pages

DBMS Chapter 5

Uploaded by

reddynanda426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views52 pages

DBMS Chapter 5

Uploaded by

reddynanda426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Chapter-5

No SQL & MongoDB


Databases
Databases can be divided in 3 types:
1. RDBMS (Relational Database Management
System)
2. OLAP (Online Analytical Processing)
3. NoSQL (recently developed database)
• A relational database is a type of database
that stores and provides access to data that
are related to one another.
• Relational databases are based on the
relational model straightforward way of
representing data in tables.
OLAP
• OLAP stands for On-Line Analytical
Processing.
• It is used for analysis of database information
from multiple database systems at one time
such as sales analysis and forecasting, market
research, budgeting and etc.
• Data Warehouse is the example of OLAP
system.
No SQL
• NoSQL databases (“non SQL “ or"not only SQL") are non-
tabular databases and store data differently than relational
tables.
• NoSQL databases are databases that store data in a format
other than relational tables.
• NoSQL databases come in a variety of types based on their
data model.
The main types are
 Document
 Key-value
 Wide-column
 Graph
• They provide flexible schemas and scale easily with large
amounts of data and high user loads.
No SQL
• The acronym NoSQL was first used in 1998 by
Carlo Strozzi while naming his lightweight,
open-source “relational” database that did not
use SQL.
No SQL data model types
Types of NoSQL databases
There are four main types of NoSQL databases:
Key value
• This is the most flexible type of NoSQL database because the application has
complete control over what is stored in the value field without any
restrictions.
Document
• Also referred to as document store or document-oriented databases, these
databases are used for storing, retrieving, and managing semi-structured data.
There is no need to specify which fields a document will contain.
Graph
• This database organizes data as nodes and relationships, which show the
connections between nodes. This supports a richer and fuller representation
of data. Graph databases are applied in social networks, reservation systems,
and fraud detection.
Wide column
• These databases store and manage data in the form of tables, rows, and
columns. They are broadly deployed in applications that require a column
format to capture schema-free data.
Need And purpose of No Sql
• NoSQL Database is a non-relational Data
Management System, that does not require a fixed
schema.
• It avoids joins, and is easy to scale.
• The major purpose of using a NoSQL database is
for distributed data stores with homongous data
storage needs.
• NoSQL is used for Big data and real-time web apps.
• For example, companies like Twitter, Facebook and
Google collect terabytes of user data every single
day.
Need And purpose of No Sql
• As storage costs rapidly decreased, the amount of
data that applications needed to store and query
increased.
• This data came in all shapes and sizes —
structured, semi-structured, and polymorphic —
and defining the schema in advance became nearly
impossible.
• NoSQL databases allow developers to store huge
amounts of unstructured data, giving them a lot of
flexibility.
No SQL Databases
Advantages of NoSql
 Flexibility:NoSQL databases can easily handle any data
format, such as structured, semi-structured, and non-
structured data in a single data store.
 Scalability:
This has the ability to support increased traffic in order
to meet demand with zero downtime.
 High performance
NoSQL databases can also ingest data and deliver it
quickly and reliably, which is why NoSQL databases are
used in applications that collect terabytes of data every
day.
 Availability
NoSQL databases automatically replicate data across multiple
servers, data centers, or cloud resources.
 Highly Functional
NoSQL databases are designed for distributed data stores that
have extremely large data storage needs. This is what makes
NoSQL the ideal choice for big data, real-time web apps,
customer 360, online shopping, online gaming, Internet of
things, social networks, and online advertising applications.
Disadvantages of NoSql
(i) Lack of Standardization:
There is no standard that defines rules and roles of NoSQL
databases.

(ii) Improper Backup of Database:


Backups are a drawback in NoSQL databases. Though some
NoSQL databases like MongoDB provide some tools for backup,
these tools are not mature enough to ensure proper complete data
backup solution.

(iii) In-Consistency:
NoSQL puts a scalability and performance first but when it comes
to a consistency of the data NoSQL doesn’t take much
consideration so it makes it little insecure as compared to the
relational database
Comparison between RDBMS & NoSql
Relational Database NoSQL
1 It is used to handle data coming in low It is used to handle data coming in
velocity. high velocity.
2 It gives both read and write
It gives only read scalability.
scalability.
3 It manages structured data. It manages all type of data.
4 Data arrives from one or few locations. Data arrives from many locations.
5 It supports complex transactions. It supports simple transactions.
6 It has single point of failure. No single point of failure.
7 It handles data in less volume. It handles data in high volume.
8 Transactions written in many
Transactions written in one location.
locations.
9 Joins are supported Joins are not supported
10 Supports only fixed schema No fixed schema
ACID & BASE SYSTEM

 The ACID model provides a consistent system.

 The BASE model provides high availability.


ACID Data base system
• Collections of operations that form a single
logical unit of work are called transactions.
• The database system must ensure proper
execution of transactions .
• ACID database transaction model ensures
that a performed transaction is always
consistent.
ACID properties

• A-Atomicity
• C-Consistency
• I- Isolation
• D-Durability
ACID properties
• Atomicity: This property states transaction must be treated as an atomic unit, that is, either all of its
operations are executed or noneand there must be no state in a database where a transaction is left partially
completed also the states should be defined either before the execution of the transaction or after the
execution of the transaction.
• Consistency: The database must remain in a consistent state after any transaction also no transaction should
have any adverse effect on the data residing in the database and if the database was in a consistent state before
the execution of a transaction then it must remain consistent after the execution of the transaction as well.
• Isolation: In a database system where more than one transaction is being executed simultaneously and in
parallel, the property of isolation states that each one of the transactions is going to be administered and
executed as it is the only transaction in the system also no transaction will affect the existence of any other
transactions.
• Durability: The database should be durable enough to hold all its latest updates even if the system fails or
restarts so .

Note:
The databases that are ACID compliant are the one safe way to make sure your database is ACID compliant is to
choose a relational database management system like MySQL, PostgreSQL, Oracle, SQLite, and Microsoft SQL
Server.
Base Model
Base Model
Base Model
Base Model:
The rise in popularity of NoSQL databases provided a flexible and fluidity with ease to
manipulate data and as a result, a new database model was designed, reflecting these
properties. The acronym BASE is slightly more confusing than ACID but however, the words
behind it suggest ways in which the BASE model is different and acronym BASE stands for:-

Basically Available: Instead of making it compulsory for immediate consistency, BASE-modelled


NoSQL databases will ensure the availability of data by spreading and replicating it across
the nodes of the database cluster.

Soft State: Due to the lack of immediate consistency, the data values may change over time. The
BASE model breaks off with the concept of a database that obligates its own consistency,
delegating that responsibility to developers.

Eventually Consistent: The fact that BASE does not obligates immediate consistency but it does
not mean that it never achieves it. However, until it does, the data reads are still possible
(even though they might not reflect reality).

NOTE: Just as SQL databases are almost uniformly ACID compliant, some NoSQL databases
also tend to conform to BASE principles like MongoDB, Cassandra and Redis are among the
most popular NoSQL solutions, together with Amazon DynamoDB and Couchbase.
Difference between ACID and BASE:
S. No Criteria ACID BASE

1. Simplicity Simple Complex

2. Focus Commits Best attempt

3. Maintenance High Low

4. Consistency Of Data Strong Weak/Loose

5. Concurrency scheme Nested Transactions Close to answer

6. Scaling Vertical Horizontal

7. Implementation Easy to implement Difficult to implement

8. Upgrade Harder to upgrade Easy to upgrade

9. Type of database Robust Simple

10. Type of code Simple Harder

11. Time required for completion Less time More time.

DynamoDB, Cassandra,
12. Examples Oracle, MySQL, SQL Server, etc. CouchDB, SimpleDB etc.
• MongoDB is an open-source document
database and leading NoSQL database.
MongoDB is written in C++.
Advantages of MongoDB
• Schema less − MongoDB is a document database in which one
collection holds different documents. Number of fields, content
and size of the document can differ from one document to
another.
• Structure of a single object is clear.
• No complex joins: Joins are not supported
• Deep query-ability: MongoDB supports dynamic queries on
documents using a document-based query language that's
nearly as powerful as SQL.
• Ease of scale-out − MongoDB is easy to scale.
• Conversion/mapping of application objects to database objects
not needed.
Data Types of MongoDB
• String − This is the most commonly used datatype to store the data.
String in MongoDB must be UTF-8 valid.
• Integer − This type is used to store a numerical value. Integer can be 32
bit or 64 bit depending upon your server.
• Boolean − This type is used to store a boolean (true/ false) value.
• Double − This type is used to store floating point values.
• Min/ Max keys − This type is used to compare a value against the
lowest and highest BSON elements.
• Arrays − This type is used to store arrays or list or multiple values into
one key.
• Timestamp − ctimestamp. This can be handy for recording when a
document has been modified or added.
• Object − This datatype is used for embedded documents.
Data Types of MongoDB
• Null − This type is used to store a Null value.
• Symbol − This datatype is used identically to a string; however, it's
generally reserved for languages that use a specific symbol type.
• Date − This datatype is used to store the current date or time in
UNIX time format. You can specify your own date time by creating
object of Date and passing day, month, year into it.
• Object ID − This datatype is used to store the document’s ID.
• Binary data − This datatype is used to store binary data.
• Code − This datatype is used to store JavaScript code into the
document.
• Regular expression − This datatype is used to store regular
expression.
MonogoDB - Create Database

1.Syntax to create a Database


>Use database_name

2. Syntax to check your currently selected database:


>db

3. Syntax to display your database list:


> Show dbs
• Example-1:
• > use iiyear
• switched to db iiyear
• > db
• Iiyear

• Example-2:
• > use hari
• switched to db hari
• > db
• hari
• > show dbs
• admin 0.000GB
• bindu 0.000GB
• iiisem 0.000GB
• local 0.000GB
• > use iiyear
• switched to db iiyear
• > db
• Iiyear

• NOTE: Your created database(iiyear) is not present in the list. To display database , you need to insert at
least one document into it.
>db.ghs.insert({name:"sankar",pin:18})
• WriteResult({ "nInserted" : 1 })
> show dbs
• admin 0.000GB
• config 0.000GB
• ghs 0.000GB
• hari 0.000GB
• local 0.000GB
MonogoDB - Drop Database

Syntax to drop a database:


db.dropDatabase()
Example-Drop database
>show dbs
• admin 0.000GB
• config 0.000GB
• ghs 0.000GB
• hari 0.000GB
• local 0.000GB
• xyz 0.000GB

> db.dropDatabase()
{ "ok" : 1 }

> show dbs


admin 0.000GB
config 0.000GB
ghs 0.000GB
hari 0.000GB
local 0.000GB
 MonogoDB - Create Collection
Syntax to create a collection:

db.createCollection(“Collection_name”)

 MongoDb-Display Collection:
Syntax to display collection list:
show collections
Example for creating and displaying
collection
>db.createCollection("tpt1")
{ "ok" : 1 }

> show collections


Tpt1

> db.createCollection("tpt2")
{ "ok" : 1 }

> show collections


tpt1
tpt2
MonogoDB- Drop Collection
Syntax to drop a collection:
db.collection_name.drop()

Example:

• > show collections


• abcd
• my collection

• > db.abcd.drop()
• True

• > show collections


• myCollection
MonogoDB- Insert Document
Syntax to insert a document into monoDB Collection:
db.collection_name.insert(document)

Example:

>db.tpt2.insert({name:"ramu",pin:018,DOB:"16-10-
2022"});
WriteResult({ "nInserted" : 1 })

> db.tpt2.insert({name:"venu",pin:019,DOB:"19-10-
2022"});
WriteResult({ "nInserted" : 1 })
MongoDB- Query Document
Syntax to query data from mongoDB Collection:
db.collection_name.find()
Example:
> db.tpt2.find()
{ "_id" : ObjectId("63576d87b4770c3be5e8bdf1"), "name" : "venkat",
"pin" : 19, "DOB" : "19-10-2022" }

{ "_id" : ObjectId("635770e4b4770c3be5e8bdf2"), "name" : "venu",


"pin" : 18, "DOB" : "22-10-2022" }

{ "_id" : ObjectId("63577103b4770c3be5e8bdf3"), "name" : "abc",


"pin" : 36, "DOB" : "25-10-2022" }

{ "_id" : ObjectId("635772d4b4770c3be5e8bdf4"), "name" : "venu",


"pin" : 18, "DOB" : "22-10-2022" }
• Note: To display results in a formatted way
pretty() is used
• > db. collection_name.find().pretty()
• > db.tpt2.find().pretty()
• {
• "_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
• "name" : "venkat",
• "pin" : 19,
• "DOB" : "19-10-2022"
• }
• {
• "_id" : ObjectId("635770e4b4770c3be5e8bdf2"),
• "name" : "venu",
• "pin" : 18,
• "DOB" : "22-10-2022"
• }
• {
• "_id" : ObjectId("63577103b4770c3be5e8bdf3"),
• "name" : "abc",
• "pin" : 36,
• "DOB" : "25-10-2022"
• }
• {
• "_id" : ObjectId("635772d4b4770c3be5e8bdf4"),
• "name" : "venu",
• "pin" : 18,
• "DOB" : "22-10-2022"
• }
MonogoDB - Update Document

Syntax to update values in the existing


document:

db.collection_name.update(selection_criteria
, updated_data)
> db.tpt2.find().pretty()
{
"_id" : ObjectId("635767f0b4770c3be5e8bdf0"),
"name" : "ramu",
"pin" : 18,
"DOB" : "16-10-2022"
}
{
"_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
"name" : "venu",
"pin" : 19,
"DOB" : "19-10-2022"
}
>db.tpt2.update({name:"venu"},{$set:{name:"venkat"}});
• WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.tpt2.find().pretty()
{
"_id" : ObjectId("635767f0b4770c3be5e8bdf0"),
"name" : "ramu",
"pin" : 18,
"DOB" : "16-10-2022"
}
{
"_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
"name" : "venkat",
"pin" : 19,
"DOB" : "19-10-2022"
}
MonogoDB - Delete Document

Syntax to delete a document from a collection:

db.collection_name.remove(deletion_criteria)
Example for delete document
> db.tpt2.find().pretty()
• {
• "_id" : ObjectId("635767f0b4770c3be5e8bdf0"),
• "name" : "ramu",
• "pin" : 21,
• "DOB" : "16-10-2022"
• }
• {
• "_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
• "name" : "venkat",
• "pin" : 19,
• "DOB" : "19-10-2022"
• }

> db.tpt2.remove({name:"ramu"})
WriteResult({ "nRemoved" : 1 })

> db.tpt2.find().pretty()
• {
• "_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
• "name" : "venkat",
• "pin" : 19,
• "DOB" : "19-10-2022"
• }
MonogoDB - Sort Document

• Syntax to sort documents in mongoDB:



db.collection_name.find().sort(KEY:1 )
• > db.tpt2.find().pretty().sort({pin:-1})
• {
• "_id" : ObjectId("63577103b4770c3be5e8bdf3"),
• "name" : "abc",
• "pin" : 36,
• "DOB" : "25-10-2022"
• }
• {
• "_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
• "name" : "venkat",
• "pin" : 19,
• "DOB" : "19-10-2022"
• }
• {
• "_id" : ObjectId("635770e4b4770c3be5e8bdf2"),
• "name" : "venu",
• "pin" : 18,
• "DOB" : "22-10-2022"
• }
• > db.tpt2.find().pretty().sort({pin:1})
• {
• "_id" : ObjectId("635770e4b4770c3be5e8bdf2"),
• "name" : "venu",
• "pin" : 18,
• "DOB" : "22-10-2022"
• }
• {
• "_id" : ObjectId("63576d87b4770c3be5e8bdf1"),
• "name" : "venkat",
• "pin" : 19,
• "DOB" : "19-10-2022"
• }
• {
• "_id" : ObjectId("63577103b4770c3be5e8bdf3"),
• "name" : "abc",
• "pin" : 36,
• "DOB" : "25-10-2022"
• }

You might also like