DBMS Chapter 5
DBMS Chapter 5
(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
• 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:-
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
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
• 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
> db.dropDatabase()
{ "ok" : 1 }
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 }
> db.createCollection("tpt2")
{ "ok" : 1 }
Example:
• > db.abcd.drop()
• True
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" }
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
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