0% found this document useful (0 votes)
30 views33 pages

DBDM Unit 5

The document covers concepts related to relational and NoSQL databases, including object identifiers, reference types, and the structure of objects. It explains the features and operations of MongoDB, such as CRUD operations, collections, and data types, while also discussing the CAP theorem and the benefits of NoSQL databases. Additionally, it delves into object-oriented database concepts, including class hierarchies, inheritance, and object identity.

Uploaded by

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

DBDM Unit 5

The document covers concepts related to relational and NoSQL databases, including object identifiers, reference types, and the structure of objects. It explains the features and operations of MongoDB, such as CRUD operations, collections, and data types, while also discussing the CAP theorem and the benefits of NoSQL databases. Additionally, it delves into object-oriented database concepts, including class hierarchies, inheritance, and object identity.

Uploaded by

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

AD3391 Database Design Management

notes Unit- 5
Relational and No-SQL Databases
to ODB schema - Object identifier-reference types-row
Subtypes and supertypes user-defined routines- Collection
Query Language, No-SQL: CAPtheorem -Document-based:
a model and CRUD operations: Column-based: Hbase data
UD operations
Part -A

Two Mars Questions with Answers

identifier(OID)?

rld entity is modeled as object. Associated with each object there is anunique ID maintained
which is called object identifier(OID)

2. Explain the structure of object

undamental unit of object oriented programming in which data varand code is encapsulated
in a single unit.
y all interactions among the objects and rest of the system are carriedout by using
messages.

3 .What is reference type?

pes are all types other than value types. Variables of reference typesstore a reference to the memory address
of the value.

type value can be stored in one table and used as a reference tospecific row in some other table.

4.What is row type?

s a sequence of name-datatype pair. It is used to provide a datatype to the rows in the table. Using row type
complete row can be stored in a variable.

5 .What is supertype and subtype?

t an entity type that has got the relationship as parent to child withone or more subtypes. It
contains attributes that are common to its subtypes.

e subtypes are group of subtype entity and have unique attributesbut they are different from
each subtype.

6. What is NoSQL ?

NoSQL stands for not only SQL. It is nontabular databasesystem that


store data differently than relational tables. There
are various types of NoSQL databases such document, key-value,
wide column and graph.
Using NoSQL we can maintain flexible schemas and theseschemas
can be scaled easily with large amount of data.

7. What is CAP theorem?

The CAP theorem states that it is not possible to guarantee all three of the desirable
properties Consistency, availability and partition tolerance at the sametime in a distributed
system with data replication.

8. Enlist features of MongoDB.

1) It is a schema-less, document based database system.


2) It provides high performance data persistence.
3) It supports multiple storage engines.
4) It has a rich query language support.

9. Enlist the features of NoSQL.


1. The NoSQL does not follow any relational mode
2. It is either schema free or have relaxed schema. That means itdoes not
require specific
3. definition of schema.
4. Multiple NoSQL databases can be executed in distributed fashion.
5. It can process both unstructured and semi-structured data
6. The NoSQL have higher scalability.

l0.Enlist the features of MongoDB.

7. It is a schema-less, document based database system.


8. lt provides high performance data persistence.
9. It supports multiple storage engines.
10. It has a rich query language support.
11. MongoDB provides high availability and redundancy with thehelp of
replication.
That means it creates multiple copies of the data and sends these copies to
a different server so that if one server fails, then the data is retrieved from
another server.

ll.What is mongoDB?
 MongoDB is an open source, document based database.
 It is developed and supported by a company named 10genwhich
is nowknown MongoDB Inc.
 The first ready version of MongoDB was released in March 2010.
l2.Why MongoDB is needed?
There are so many efficient RDBMS products available in themarket, then
why do we need MongoDB? Well, all the modern applications require
Big data, faster development and flexible
deployment. This need is satisfied by the document based databaselike
MongoDB.

l3.How the terms in MongoDB are different from SQL?

The terms in SQL are treated differently in MongoDB. In MongoDB the


data is not stored in tables, instead of that, the there is a concept called
collection which is analogous to the tables. In thesame manner the rows
in RDBMS are called documents in MongoDB, likewise the columns of
the record in RDBMS are calledfields.

l4.Define collections in MongoDB.

MongoDB groups data together through collections. A collection is simply a


grouping of documents that have the same or a similar purpose.
A collection acts similarly to a table in a traditional SQL database, with one
major difference.
In MongoDB, a collection is not enforced by a strict schema; instead,
documents in a collection can have a slightly different structure from one
another as needed.
This reduces the need to break items in a document into several different
tables, which is often done in SQL implementations.

l5.Define the term document in MongoDB.

A document is a representation of a single entity of data in the MongoDB


database. A collection is made up of one or more related objects.
A major difference between MongoDB and SQL is that documents are
different from rows.
Row data is flat, meaning there is one column for each value in the row.
However, in MongoDB, documents can contain embedded
subdocuments, thus providing a much closer inherent data modelto your
applications.

l6.Enlist any four data types in MongoDB.

Following are various types of data types supported by MongoDB.


1. Integer: This data type is used for storing the numerical value.
2. Boolean: This data type is used for implementing the Booleanvalues
i.e. true or false.
3. Double: Double is used for storing floating point data.
4. String: This is the most commonly used data type used forstoring
thestring values.
l7.Explain how to create collection in MongoDB?

After creating some sample database, we must create somecollectioninside that


database.
Syntax
db.create Collection(name, options) where
We can create collection explicitly using createCollectioncommand name
is the name of collection
options is an optional field. This field is maximum
number of documents and soon.

l8.Define CRUD Operation


After creating some sample database, we must create some collection
inside that database.We can perform various operations such as insert, delete
and update on this collection. These operations are called CRUD operations.
CRUD stands for Create, Read. Update and Delete operation.
l9.How to create Create Collection in MongoDB

Syntax
We can create collection explicitly using createCollection command.
db.create Collection(name, options)
where
name is the name of collection
options is an optional field. This field is used to specify some parameters such
as Size, maximum number of documents and soon.

20.Give the syntax for insert the document. The document isanalogous to
rows in database.
The document is inserted within the collection. The
document is analogous to rows in database Syntax
db.collection name.insert( {key, value }

2l.How to insert multiple documents in a MongoDB database?

It is possible to insert multiple documents at a time using a single


command. Following command shows how to insert multiple documents in
the existing collection
> var = allStudents
=[
{
"name": AAA,
'age": 20
},
{
“name”:BBB,
“age": 21
},
{
"name": CCC
"age": 22
}
];
>db.Student
>db.Student details.insert(alliStudents),

22. How to delete Documents in MongoDB

For deleting the document, the remove command is used. Thisis the
simplest command
Syntax
db.collection_name.remove(delete_criteria)
23. What are the methods used in Update the document in MongoDB

The methods used in Update the document in MongoDB1.updateOne,


2. updateMany or bulkwrite.

24. . What is column-oriented database model? Give example of it.

The column-oriented database is a database system that stores the data table by
column rather than by rows. The advantage of column-oriented Databaseis that we can
access the data more efficiently when only subset of columns is used for querying. For
example - HBase

25. . Enlist the features of HBase data model.

1. HBase is horizontally scalable. That means we can add any number


of columns anytime.
2. The data is stored in key value format.
3. It is a platform for storing and retrieving data with random access.
4. It does not enforce relationship within the data.
5. It is designed to run on cluster of computers.
Part B & C Questions
l.Explain about Object Database Concept and Mapping EER and ODB Schema Object

Database Concept

Traditional applications are designed for business applications such as inventory,employee,


university, bank, library, air-line reservation systems, and so on.

These applications require relatively simple data types that are well suited to therelational data
model. But database systems were applied to a wider range of applications, such as
computer-aided design and geographical information systems. Hence such systems require
complex data type and processing.

The object based database provide the solution to model the real world object and their behavior. It
is an alternative to relational database model.

Mapping EER to ODB Schema

Object oriented paradigm encapsulate data and corresponding methods in a singleentity called
object.

The object oriented data model is a logical data model just like ER model. Loosely object can be
thought as an entity of E-R model.

Object oriented data model helps in adapting object oriented paradigm to database systems, In

object oriented database, information is represented in the form of objects.

Object oriented databases are exactly same as object oriented programming languages. If we can
combine the features of relational model (transaction, concurrency, recovery) to object oriented
databases, the resultant model is calledas object oriented database model.

The core object oriented Data model consists of following object oriented concepts-

 Object and Object Identifier

Any real world entity is modeled as object. Associated with each object there is an
unique ID maintained which is called object identifier(OID). Object identifiers used to
uniquely identify objects. Object identifiers are unique. No two objects havethe same
identifier, each object has only one object identifier.

 Attribute and method

Every object has state and behavior. The state is a set of values for attributes of the
object. The behavior is set of methods. For example - Object named circlehas state radius
and computing its area is a behavior.
Class
Class is a means of grouping all the objects which share the same set of attributes and
methods. An object must belong to only one class as an instance of that class (instance-of
relationship). A class is similar to an abstract data type.

 Class Hierarchy and Inheritance

The class hierarchy is used to represent the Inheritance property of object oriented
paradigm. The inheritance is a mechanism in which a new class is derived from existing
class.

l.Object Structure

 Object is a fundamental unit of object oriented programming in which data valueand code
is encapsulated in a single unit.

 Conceptually all interactions among the objects and rest of the system are carried out by
using messages. Thus the interface among various objects and rest of the system is
messages. Messages can be implemented as procedure invocations.

 In general, an object has associated with it:

 A set of variables that contain the data for the object. The value of eachvariable is
itself an object.

 A set of messages to which the object responds.

 A set of methods, each of which is a body of code to implement each message; a method
returns a value as the response to the message.

 Methods are programs written in general-purpose language with the following features-

 Only variables in the object itself may be referenced directly.


 Data in other objects are referenced only by sending messages.
 Methods can be read-only or update methods. Read-only methods do notchange
the value of the object.

 For example-the object Circle can have methods such as compute_area)

2. Object Classes

Similar objects are grouped into classes. In other words, object is an instance of a class.
For example

class Student {

/*variables*/

Int RollNo:
String Name:
String address;

/*Messages*/

String get name();


String get_address();
Int get RollNo();
}

Methods can be defined separately. For exampleint get

rollNo()

{
Return RollNO;
}

3. Inheritance

In object oriented database schema, there can be large number of classes. Someclasses
are similar and can be derived from old existing classes. The classes are placed into
specialization or Is-a hierarchy.

For example - Consider following ER model –

Class Hierarchy

The class hierarchy can be defined in pseudo-code in which the

variablesassociated with each class are as follows

class Person
{
String name;
String address;
}

class Student isa Person //Inheritance: Deriving child class


{
int RollNo;
String Course taken
}

class Employee isa Person


{
date join-date;
int salary;
}
class Teacher isa Employee
{
String Subject:
}
class Accountant isa Employee
{
int office no
}

 The keyword isa is used to indicate that a class is a specialization of


anotherclass. The specialization of a class are called subclasses. For example -
Employeeis a subclass of Person. Conversely, Person is a superclass of class
Employee.

 Class hierarchy and inheritance of properties from more general classes.

 The important benefit of using inheritance is code-reusability. This can beachieved


by means of substitutability property.

 Substitutability means any method of a class, say person, can be invoked equally
well with any object belonging to any subclass, such as subclass Teacher of Person.

4. Multiple Inheritance

In most cases, tree-structured organization of classes is adequate to describeapplications. In


such cases, all super classes of a class are ancestors of descendants of another in the
hierarchy. However, there are situations thatcannot be represented well in a tree-structured
class hierarchy.

To avoid the conflicts between two occurrences, we use multiple inheritance.

Multiple inheritance is an ability of class to inherit variables from multiple super classes.
The class subclass relationship is represented by directed acylic graph(DAG). For
example-
Fig. 5.2 Multiple inheritance

Handling name conflicts: When multiple inheritance is used, there is potentialambiguity


if the same variable or method can be inherited from more than onesuperclass.

5 .Object Identity

• For identifying the RDBMS record uniquely in the table, we use primary key
associated with it. In object oriented database management system we use ObjectId to
identify the record.

Associated with each object there is an unique ID maintained which is calledobject


identifier(OID).

An object retains its identity even if some or all the values of the variables ordefinitions of
methods change over time.

The concept of object identity does not apply to tuples of relational database. It isa stronger
notion of identity.

The difference between primary of RDBMS and object ID of OODBMS is that


primary key is explicit; that means it is visible to the user whereas the object IDis
implicit; that means it is hidden from external world.

It has several forms of identity:


 Value: The data value is used for identity. For example the primary key of thetuple in a
relational database.

 Name: The user supplied name is used for identity. For example file name can beused for
identity of object.

 Built-in: A notion of identity is built-into the data model or


programming languages, and no user-supplied identifier is required.

Object identity is typically implemented via a unique, system-generated OID. The valueof the
OID is not visible to the external user, but is used internally by the system to identify each object
uniquely and to create and manage inter-object references.

6. Object Containment

 The objects that contain another objects are called composite objects or complexobjects.

 There can be multiple levels of object containment. These levels can be


represented using containment hierarchy.

 For example The following Fig. 5.2.3 represents the design of simple documentThe
document contains the text in the form of paragraphs. Each paragraph contains
word. Each word is made up of come characters.

 Also note that there can be some size and style for each paragraph, word
andeach character.

 The links between classes must be interpreted as is-part-of, rather than the is-a
interpretation of links in an inheritance hierarchy.

 Containment allows data to be viewed at different granularities by different users.For


example - document can be viewed by a reader for simply reading purpose, the size
and style of paragraphs can be viewed by DTP operator or proof editor forediting
purpose.

 The containment hierarchy can be used to find all object contained in documentobject.

 In certain applications, an object may be contained in several objects. In suchcases,


the containment relationship is represented by a DAG rather than by ahierarchy.

2. Write about Reference types in detail

Reference types are all types other than value types. Variables of reference typesstore a
reference to the memory address of the value.

A reference type value can be stored in one table and used as a reference tospecific
row in some other table.
To use a reference type in an SQL statement, use REF(type-name), where type-
name represents the referenced type.

REF IS SYSTEM GENERATED in a CREATE TYPE statement indicates that the


actual values of associated REF type are provided by the system.

3. Write about Row Types in detail.

A row type is a sequence of name-datatype pair. It is used to provide a datatypeto the rows
in the table.

Using row type complete row can be stored in a variable. This variable can be passed
as an argument to the routine or we can get the returned value from afunction call
within this variable.

For example Consider an Employee table containing Emp_Id and address andinsert
into the new table

CREATE TABLE Employee( empID


CHAR(5).address ROW( Street
VARCHAR(30),
City VARCHAR(25).
Pincode ROW(city id VARCHAR(6). Region VARCHAR(10))));
INSERT INTO Employee
VALUES('Account 1234', ROW('11 Shankar Rd', "Chennai", ROW('ch11','Anand
Nagar")));

To create a subtype StudentType of the supertype PersonType we write:CREATE

TYPE StudentType UNDER PersonType AS (


rollNo VARCHAR(5),
name VARCHAR(10),
branchID CHAR(4))

4. Explain about User Defined Types in detail

The user defined type(UDT) is a data type derived from an existing data type
UDT allows the database developer to extend the built in types and to create a
customized data type.
There are two types of user defined types-
Distinct Type
A distinct type is a user-defined data type that shares its internalrepresentation with
an existing built-in data type. For example-
CREATE TYPE empNumberType AS VARCHAR(5) FINAL
Structured Type
A structured type is a user-defined data type containing one or more named
attributes, each of which has a data type. Attributes are properties thatdescribe an
instance of a type. A structured type also includes a set of method
specifications. Methods enable you to define behaviors for structured types. Forexample -

CREATE TYPE person AS OBJECT (


name VARCHAR(30),
phone VARCHAR(20));
CREATE TYPE purchase_order AS OBJECT
( id NUMBER, contact person,
MEMBER FUNCTION
get value RETURN NUMBER);

In above example we have defined an object named person and created a structure type
purchase_order. The members of this structure are attributes suchas id, contact and one
member function named get_value
The value of the attribute can be accessed using the dot ooperator. For example ifp is an
instance of person then,
 p.name = 'Anand'
o allows to assign name to the arribute of the person data type.

5. Write a short notes on Subtypes and Supertypes

Supertype:
It an entity type that has got the relationship as parent to child with one or moresubtypes. It
contains attributes that are common to its subtypes.
Subtype:
The subtypes are group of subtype entity and have unique attributes but they
aredifferent from each subtype.

Figure.6 Example of Subtype and Super type


To create a subtype StudentType of the supertype PersonType we write:

CREATE TYPE StudentType UNDER PersonType AS (


rollNo VARCHAR(5),
name VARCHAR(10),
branchID CHAR(4))

Advantages of Subtype and super type

1. Avoids data redundancy, as each subtype contains only unique


information. This in turn leads to a reduction in the size of the
databaseitself.

2. Reduction of errors in programming operations on a table that correspondsto


a specific subtype of an entity.

3. Flexibility in modifying the database structure. There is no need to modify the super
type table if you add / change the structure of the subtype table.

6. Write about User-Defined Routines in detail

User-defined routines are routines that users create themselves. User-


definedroutines provide a means for users to extend the SQL language.

The User Defined routines capture the functionality of most commonly used
arithmetic, string, and casting functions. However, these also allows to createroutines to
encapsulate logic of your own.

User-defined procedures, functions and methods are created in the database by


executing the appropriate CREATE statement for the routine type. These routinecreation
statements include:

CREATE
PROCEDURECREATE
FUNCTION CREATE
METHOD
The clauses specific to each of the CREATE statements define characteristics ofthe
routine, such as the routine name, the number and type of routine arguments, and details
about the routine logic.

7. Write about the collection types in detail

A collection is an ordered group of elements having the same data type.


The collections are used to store multiple values in a single column of table.
The collection add flexibility to the database design.
Following are the ways by which the collection types can be used-

 ARRAY: It is an entity in that represents one dimensional array


with o maximum number of elements

 LIST: Ordered collection that allows duplicates.

 SET: Unordered collection that does not allow

duplicatesFor example-Array can be created as follows-

VARCHAR(20) ARRAY(4):
ARRAY['Archana','Ashwini', 'Aishwarya', "Sharda']:

8. Give the detail about Object Query Language(OQL) in Detail

Object Query Language (OQL) is a query language standard for object-oriented


databases modeled after SQL...

The following rules apply to OQL statements

(1) All complete statements must be terminated by a semi-colon.


(2) A list of entries in OQL is usually separated by commas but
not terminated by a comma(,).
(3) Strings of text are enclosed by matching quotation marks.

Basic From of OQL: SELECT, FROM, WHERE

Syntax

SELECT <list of values>


FROM <list of collections and variable assignments>WHERE
<condition>

Where the SELECT clause extracts those elements of a collection meeting a specific condition.
By using the keyword DISTINCT duplicated elements in the resulting collection get eliminated.
Collections in FROM can be either extents (persistent namessets) or expressions that evaluate
to a collection (a set).

Example: Give the names of people who are older than 30 years old:
SELECT SName:
p.name FROM p in
People WHERE page >
30

DOT notations and Path expressions

We use the dot notation and path expressions to access components of complex values.
For example

ta.salary -> real


t.students-> set of tuples of type tuple(name: string, fee: real) representing studentst.salary->
real

9. What is NoSQL? What is the need for it. Enlist various feature of NoSQL

l.Introduction

NoSQL stands for not only SQL.

It is nontabular database system that store data differently than relational tables.There are
various types of NoSQL databases such as document, key-value,wide column and graph.

Using NoSQL we can maintain flexible schemas and these schemas can be scaledeasily
with large amount of data

2. Need

The NoSQL database technology is usually adopted for following reasons-

1) The NoSQL databases are often used for handling big data as a part
of fundamental architecture.

2) The NoSQL databases are used for storing and modelling structured,
semi- structured and unstructured data.

3) For the efficient execution of database with high availability, NoSQL. is used.

4) The NoSQL database is non-relational, so it scales out better than


relationaldatabases and these can be designed with web applications.

5) For easy scalability, the NoSQL is used.

3. Features

1) The NoSQL does not follow any relational model.


2) It is either schema free or have relaxed schema. That means it does not requirespecific
definition of schema.
3) Multiple NoSQL databases can be executed in distributed fashion.
4) It can process both unstructured and semi-structured data.
5) The NoSQL have higher scalability.
6) It is cost effective.
7) It supports the data in the form of key-value pair, wide columns and graphs.

l0.Explain different types of NoSQL databases.

There are four types of NoSQL databases and those are-

1. Key-value store
2. Document store
3. Graph based

4. Wide column store

Let us discuss them in detail.

l. Key-Value Store

 Key-value pair is the simplest type of NoSQL database.


 It is designed in such a way to handle lots of data and heavy load.
 In the key-value storage the key is unique and the value can be JSON, string
orBinary objects.
 For example-

{
Customer:
[
{"id":1,"name":"Ankita"),
{"id":2,"name":"Kavita"}
]
}

Here id, name are the keys and 1,2, "Ankita", "Prajkta" are the values corresponding tothose keys.

Key-value stores help the developer to store schema-less data. They work best forShopping Cart
Contents.

The DynamoDB, Riak, Redis are some famous examples of key-value store

2 .Document Store

 The document store make use of key-value pair to store and retrieve data.
 The document is stored in the form of XML and JSON.
 The document stores appear the most natural among NoSQL. database types.
 It is most commonly used due to flexibility and ability to query on any field.
 For example -
{
"id": 101.
lOMoAR cPSD| 37346999

"Name": "AAA",
"City": "Pune"
}

MongoDB and CouchDB are two popular document oriented NoSQL database

3. Graph

The graph database is typically used in the applications where the relationships amongthe data
elements is an important aspect.

The connections between elements are called links or relationships. In a graph database,
connections are first-class elements of the database, stored directly. Inrelational databases, links
are implied, using data to express the relationships.

The graph database has two components-

1) Node: The entities itself. For example - People, student,


2) Edge: The relationships among the entities.

For example-

Graph base database is mostly used for social networks, logistics, spatial data. Thegraph
databases are Neo4J, Infinite Graph, OrientDB.

4. Wide Column Store

Wide column store model is similar to traditional relational database. In this model, the
columns are created for each row rather than having predefined by thetable structure.
In this model number of columns are not fixed for each record.
Columns databases can quickly aggregate the value of a given column.
For example-
Row ID Columns...

1 Name City

Ankita Pune

2 Name City email

Kavita Mumbai [email protected]

The column store databases are widely used to manage data warehouses, businessintelligence,
HBase, Cassandra are examples of column based databases

ll.Give the difference between RDBMS and NoSQL

S.No RDBM NoSQ


S L
1 The relational database system is It is non-relational
based on relationships among the database system. It can be
tables. used in distributed
environment
2 It is vertically scalable. It is horizontally scalable.

3 It has predefined schema. It does not have schema or it may


have related schema.

4 It uses SQL to query the It uses unstructured query


database. language.

5 It is a table based database. It is document based,


graph based on kry-value
pair.
6 It emphasizes on ACID properties It follows Brewers CAP thecerm
(Automicity, consistency, isolationand (Consistency, availability and
durability) partition tolerance)

7 Schema is fixed or rigid. Schema is dynamic

8 Pessimistic. Optimistic

9 Examples: MySQL, Oracle, Examples: MangoDB, Big Table,


PostgreSQL Redis
l2.Write a short note on CAP Theorem

Cap theorem is also called as brewer's theorem.


The CAP Theorem is comprised of three components (hence its name) as they relateto
distributed data stores:
o Consistency: All reads receive the most recent write or an error.
o Availability: All reads contain data, but it might not be the most recent
o
Partition tolerance: The system continues to operate despite network failures (i.e.; dropped
partitions, slow network connections, or unavailable network connections between nodes.)
The CAP theorem states that it is not possible to guarantee all three of the desirableproperties -
consistency, availability, and partition tolerance at the same time in adistributed system with
data replication.

l3.Understanding NoSQL and MongoDB in Detail


NoSQL
NoSQL stands for not only SQL.
It is nontabular database system that store data differently than relational tables. are various
types of NoSQL databases such as document, key-value, wide columnand graph.
Using NoSQL we can maintaın flexible schemas and these schemas can be scaled
easily with large amount of data.
Need
1. The NoSỌL database technology is usually adopted for following reasons -
2. The NoSQL databases are often used for handling big data as a part of
fundamental architecture.
3. The NoSQL databases are used for storing and modelling structured, semi-structured
and unstructured data.
4. For the efficient execution of database with high availability, NoSQL is used.
5. The NoSQL database is non-relational, so it scales out better than relational
6. databases and these can be designed with web applications.
7. For easy scalability, the NoSQL is used.
Features
1. The NoSQL does not follow any relational model.
2. It is either schema free or have relaxed schema. That means it does not requirespecific
definition of schema.
3. Multiple NoSQL databases can be executed in distributed fashion.
4. It can process both unstructured and semi-structured data.
5. The NoSQL have higher scalability.
6. It is cost effective.
7. It supports the data in the form of key-value pair, wide columns and graphs.
MongoDB
MongoDB is an open source, document based database.
It is developed and supported by a company named 10gen which is now known MongoDB Inc.
The first ready version of MongoDB was released in March 2010.
Why MongoDB is needed?
There are so many efficient RDBMS products available in the market, then why do we need
MongoDB? Well, all the modern applications require Big data, faster development and flexible
deployment. This need is satisfied by the document based database like MongoDB.
Features of MongoDB
1. It is a schema-less, document based database system.
2. It provides high performance data persistence.
3. It supports multiple storage engines.
4. It has a rich query language support.
5. MongoDB provides high availability and redundancy with the help of replication.That means
it creates multiple copies of the data and sends these copies to a different server so that if one
server fails, then the data is retrieved from another server.
6. MongoDB provides horizontal scalability with the help of sharding. Sharding means to
distribute data on multiple servers.
7. In MongoDB, every field in the document is indexed as primary or secondary. Due to
which data can be searched very efficiently from the database.

Understanding Collections

MongoDB groups data together through collections. A collection is simply a


grouping of documents that have the same or a similar purpose.
A collection acts similarly to a table in a traditional SQL database, with one majordifference.
In MongoDB, a collection is not enforced by a strict schema; instead, documentsin a
collection can have a slightly different structure from one another as needed.
This reduces the need to break items in a document into several different tables,which
is often done in SQL implementations.

Understanding Documents
A document is a representation of a single entity of data in the MongoDB database.
A collection is made up of one or more related objects.
A major difference between MongoDB and SQL is that documents are different from
rows.
Row data is flat, meaning there is one column for each value in the row. However,
in MongoDB, documents can contain embedded subdocuments, thus providing a muchcloser inherent
data model to your applications.
The records in MongoDB that represent documents are stored as BSON, which isa
lightweight binary form of JSON,
with field:value pairs corresponding to JavaScript property:value pairs.
These field:value pairs define the values stored in the document.

That means little translation is necessary to convert MongoDB records back intothe
JavaScript object that you use in your Node.js applications.

For example, a document in MongoDB may be structured similarly to the following withname,
version, languages, admin, and paths fields:

{
name: "New Project",
version: 1,
languages: ["JavaScript", "HTML", "CSS"],
admin: {name: "Brad", password: "****"},
paths: {temp: "/tmp", project: "/opt/project", html: "/opt/project/html"}
}
SQL Structure Vs. MongoDB
Following Fig. 3.1 shows the terms in SQL are treated differently in MongoDB. In MongoDB the
data is not stored in tables, instead of that, there is a concept called collection which is analogous
to the tables. In the same manner the rows in RDBMS are called documents in MongoDB,
likewise the columns of the record in RDBMS are called fields.

Figure 3.l SQL Structure Vs. MongoDB


Consider a student database as follows
To the left hand side we show the database in the form of table and to the right handside the
database is shown in the form of collection,

l4.What are the data types used in MongoDB? Explain in detail.

Following are various types of data types supported by MongoDB.

l. Integer: This data type is used for storing the numerical value.
2. Boolean: This data type is used for implementing the Boolean values ie.trueor false.
3. String: This is the most commonly used data type used for storing the stringvalues
4. Double: Double is used for storing floating point data.
5. Min/Max keys: This data type is used to compare a value against the
highestBSON element.
6. Arrays: For storing an array or list of multiple values in one key, this data used.
7. Object: The object is implemented for embedded documents.
8. Symbol: This data type is similar to string data type. This data type is usedspecific
symbol type.
9. Null: For storing the null yalues this data type is used.
l0. Date: This data type is used to store current date or time. We can also owndate or
time object.
ll. Binary data: In order to store binary data, we need to use this data type.
l2. Regular expression: This data type is used to store regular expression.
l5.Write the MongoDB command to create and drop the database.

Administering Databases.

Write the MongoDB command to create and drop the database.operations.

In this section we will discuss how to create and handle databasein


MongoDB using
various commands.

(1) Create Database

For example
Open the command prompt and type the command mongosh forstarting
themongDB shell.
The test> prompt will appear. For creating a database we need to “use”
thecommand.
Syntax

(2) Displaying all the databases present in the system

We can see the list of databases present in the MongoDB usingthe


command show dbs
For example
mystudents> show
dbs Admin 180.06
KiB
config 72.00 KiB
Local 72.00
kiB
mystudents>

Note that in above listing we can not see the mystudents database. This is
because we have not inserted any document into it. To get the name of the
database in the listing by means of showcommand, there should be some
record present in the database.

(3) Drop Database


The dropDatabase() command is used to delete the database. For example
- Mystudents> db.dropDatabase()
{ ok: 1,dropped:
„mystudents‟ } mystudents>
l6.Explain with suitable examples, CRUD operations in
MongoDB. Managing Collections
After creating some sample database, we must create some collection
inside that database.We can perform various operations such as insert, delete
and update on this collection. These operations are called CRUD operations.
CRUD stands for Create, Read. Update and Delete operation.

l.Create Collection
lOMoAR cPSD| 37346999

Syntax
We can create collection explicitly using createCollectioncommand.
db.create Collection(name, options)
where
name is the name of collection
options is an optional field. This field is used to specify some parameters
such as Size, maximum number of documents and soon.

Following is a list of such options.

Field Typ Description


e
Capped Bool Capped collection is a fixed size collection.It
ean automaticallyoverwrites the oldest entries whenit reaches to
maximum size.ifit is set to true, enabled a capped collection.
When you specifythis value is
true, you need to specify the sizeparameter.
autolndexID Bool This field is required to create index id automatically. Its
ean detault value is false.
size Number This value indicates the maximum size inbytes
for a clapped collection.

Max Number It specifies the maximum number of documents allowed in


capped collection.

Table 5.3 Options for create collection

For example - Following command shows how to create collectionin a


database using explicit command
test> use myStudents
Switched to db
mystudents
myStudents> db.createCollection("Student_details”)
{ ok: 1 }
Mystudents>

2) Display Collection

To check the created collection use the command "show collections" atthe
commarnd prompt
myStudents> show
collections Student_details
myStudents>

3) Drop Collection

The drop collection command is actually used to remove the collection


completely from the database. The drop command removes a collection
completely from database.

Syntax

db.collection name.drop()

For Example

my Students>
db.Student_details.drop() truemy
Students>
We can verify the deletion of the collection by using "show collections" inthe
database.

myStudents> show collections


myStudents>

4) Insert documents within the collection

The document is inserted within the collection. The


document is analogous to rows in database Syntax
db.collection name.insert( {key, value }

For example
myStudents> show collections
nyStudents> db.create Collection("stedent details)
{ ok: 1}

myStudents> db.Student_details.insert(nameAAA" age":2})acknowledged :


true,
myStudents>
insertedIds:{ '0': 0bjectId("643e4b63436497399alala66")}
}
myStudents>

We can verify this insertion by issuing following


command myStudents> db. Student.details.find()
[{ _id: Object Id ("643e4b63436497399al a2 a66"), name: AAA, age: 22
}] myStudents>

5) Inserting Multiple Documents


It is possible to insert multiple documents at a time using a single
command. Following program shows how to insert multiple documents in the
existing collection.
my Students>
varall_students. =[
{
“name”:
“BBB”
“age":20
},
{
“name" :
"DDD”
“age":21
},
];
acknowledged
:true, ent
details.find()
myStudents> db.Student detaits . Insert(allstudents);
insertedIds: { „0‟ : ObjectId ("643e518d436197399ala2a67”)
„1':
“ObiectId"643e518d436497399aia2a68”)
}
}
myStudents>

To verify the existence of these documents in the collection you can usefind
command as
follows -
myStudents> db.Student_details. find()
{-id: ObjectId ("643e4b63436497399ala2a66"), name: „AAA‟ ,age: 22 }
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „BBB‟,
age: 21,}
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „DDD‟,
age: 20,}
}
myStudents>

6) Delete Documents
For deleting the document, the remove command is used. Thisis the
simplest command
Syntax
db.collection_name.remove(delete_criteria)

For example
First of all we can find out the documents presents in thecollection
usingfind() command
myStudents> db.Student_details. find()[
{-id: ObjectId ("643e4b63436497399ala2a66"), name: „AAA‟ ,age: 22 }
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „BBB‟, age: 21,}
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „DDD‟, age: 20,}
]
myStudents>
Now to delete a record with name “DDD” we can issue thecommand asfollows-
myStudents> db.Student details.deleteOne( {“name”: “DDD” });
{ acknowledged: true, deletecount: 0}myStudents>
Now using find() command we can verify if the desired data is deletedornot.
myStudents> db.Student_details. find()
{-id: ObjectId ("643e4b63436497399ala2a66"), name: „AAA‟ ,age: 22 }
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „BBB‟, age: 21,}
}
7. Delete all documents

For deleting all the documents from the collection,we can use.deleteMany()
For example
myStudents> db.Student details.deleteMany( { });
{ acknowledged: true,
deletecount:2} myStudents>
We can verify it using db.Student_details.find() command,we canverify that
records are deleted or not.
myStudents> db.Student_details. find()
myStudents>

8. Update Documents

For updating the document, we have to provide some criteriabased on


which the document can be updated.

db.collection_name.update(criteria, update_data)

For example - Suppose the collection "Student_details” containfollowing


documents
myStudents> db.Student_details.find() [
{_id: ObjectId ("643e4b63436497399ala2a66"), name: „AAA‟ ,age: 22 }
{_íd: ObjectId ("643e518d436497399ala2a67 "), name: „BBB‟, age: 21,}
{_íd: ObjectId ("643e518d436497399ala2a67 "), name: „DDD‟, age: 20,}
{_íd: ObjectId ("643e518d436497399ala2a67 "), name: „CCC‟, age: 23,}

And we want to change the name CCC" to WWW", then the


command can be issued as
myStudents>db.Student_detaits.update({“name:”CCC”},{set:{“name”:”W
WW”}}) DeprecationlWarning: Collection. update () is deprecated. Use
updateOne, updateMany, or bulkwrite.
{
acknowledged : true,
InsertedId: null,
matchedCount: 1,
modifiedCount:1,
upsertedCount:0
}

It can be verified using db.Student details.find() command


my Students> db.Student_details
.find().
[
{-id: ObjectId ("643e4b63436497399ala2a66"), name: „AAA‟ ,age: 22 }
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „BBB‟, age: 21,}
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „DDD‟, age: 20,}
{ -íd: ObjectId ("643e518d436497399ala2a67 "), name: „WWW‟, age: 23,}
]
myStudents>

Thus the document gets updated.

By default the update command updates a single document. But wecan


update multiple document as well. For that purpose we have to add

db. Student details. update({ "age"21}, ($set:{"age":23}},{multi:true})

l7.Write about Column Oriented Database: HBase

Concept of Column Oriented Database

The column-oriented database is a database system that stores the data table by column rather
than by rows. The advantage of column-oriented Database is that we canaccess the data more
efficiently when only subset of columns is used for querying, For example-Consider the
customer table

email address gender age


Archana
Female
[email protected] 30

email address gender


Archana
Male
[email protected]

email address Country

Aswini
India
[email protected]

we can see that a column family has several rows. Within each row, there can beseveral different
columns, with different names, links, and even sizes
Advantages of Column oriented database

a. The most important benefit of column oriented database is faster


performance compared to row oriented database
b. The column-oriented database can store more data in smaller amount ofmemory.
c. The column-oriented database is highly used for Big data applications.
d. Another benefit of a column-based DBMS is self-indexing, which uses lessdisk
space than Relational database schema.
e. We can use aggregation for vast amount of data.

l8. Write about Introduction to Hbase Data Model in detail.

 HBase is a column oriented non-relational database management system. It runson top


of Hadoop Distributed File System(HDFS)
 HBase is an Open-source technology developed by Apache Software Foundation.
 The HBase can store massive amount of data from terabyte to petabytes

l.Features of HBase Data Model

1) HBase is horizontally scalable. That means we can add any number


of columns anytime.
2) The data is stored in key value format.
3) It is a platform for storing and retrieving data with random access.
4) It does not enforce relationship within the data.
5) It is designed to run on cluster of computers.

2.Difference between RDBMS and HBase

RDBM HBase
S.No
S
1 It requires SQL. There is no SQL. in HBase.
2 It has fixed schema. It does not have a fixed schema
3 It is row-oriented. It is column oriented.
4 It is static in nature It is dynamic.
The data retrieval is slower. Data can be retrieved fast in
5
Habe
In RDBMS, the data is
6 The HBase is not normalized
normalized.
7 It accommodates small tables. It accommodates large tables
3 .HBASE CRUD Operations

The CRUD stands for Create, Read, Update and Delete Operations. In order to communicate
with HBase, the HBase Shell is used. The CRUD operations are verified by issuing the
Create, put, get alter, delete, list and many more such commands. Let usdiscuss some
commonly used commands

l) Create Table:

The create command is used to create a table.


CREATE <table-table>','<column-family>

For example
> CREATE customer ef

2) Read Operation:

The 'get' command is used to read the contents of row or cell.The


syntax is-
get<table-name>', '<row>"

For example-

hbase(main):015:0> get 'customer',

'1' COLUMN CELL


cf: ge timestamp = 1417621885277, value - 30
ef:city timestamp =1417521848375, value-Pune
cf: name timestamp = 1417521785385, value- Supriya

Reading a specific column

hbase(main):018:0> get customer', 'T', (COLUMN


'cfname") COLUMN CELL

cf: name timestamp = 1418035791555, value Supriya1

row(s) in 0.0080 seconds

3) Insert Data:

We can insert data to the table using the put command.

Now, we can create a table by inserting some data into it in column-based mannerusing put
command

>put customer 1,'ct name", "Supriya


>put 'customer',1,'cf:city', 'Pune'
>put 'customer',1,'cfage',

'30"We get the table as follows


Row Key cf:name cf:city cf:age

1 Supriya Pune 30

We can check if table is created using list command. For example

> list 'customer

4) Update Operation

We can modify some properties of existing table using alter command. We add more

column families or we can modify the table attributes.


The syntax is alter <tablename>, NAME> <column family name> VERSIONS>
<newversion no>
To add the column family 'status' in table 'customer' and to keep a maximum of 5
cellsVERSIONS
>alter 'customer, NAME'status', VERSIONS =>5

To delete some column family 'status' we use alter command as


>alter customer, NAME->'status', METHOD=>delete

5) Deletion Operation

We can delete a particular cell by using the delete command. The syntax is

For example-We can delete a cell for the column city from the customer table as
followshbase(main) 0060 delete customer, T. of city,
1417621948376
0 row 0.0060 seconds

Delete all: It deletes all the

cells in a rowdesteel table

name>,<row>

For example-

bum (main) 0070 deletesil customer. T

(E) Display the Contents of Table

For displaying the contents of the table 'scan' commad is used. For example

hBase(main) 040:0> scan „customer‟ NOW COLUMN+CELL


1 column =cf:age, timestamp 1505200004953, value-30
1 column =cf:city, timestamp-1505200291903, value-Pune
1 column=ct:name, timestamp=1505200278058, value-Supriya OMoARcPSD|37346999

You might also like