DBDM Unit 5
DBDM Unit 5
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
identifier(OID)?
rld entity is modeled as object. Associated with each object there is anunique ID maintained
which is called object identifier(OID)
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.
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.
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.
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 ?
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.
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.
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 }
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 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
Database Concept
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.
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 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-
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.
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.
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.
A set of variables that contain the data for the object. The value of eachvariable is
itself an object.
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-
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*/
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.
Class Hierarchy
class Person
{
String name;
String address;
}
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
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
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.
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.
Name: The user supplied name is used for identity. For example file name can beused for
identity of object.
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.
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.
The containment hierarchy can be used to find all object contained in documentobject.
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.
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
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 -
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.
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.
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.
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.
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.
VARCHAR(20) ARRAY(4):
ARRAY['Archana','Ashwini', 'Aishwarya', "Sharda']:
Syntax
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
We use the dot notation and path expressions to access components of complex values.
For example
9. What is NoSQL? What is the need for it. Enlist various feature of NoSQL
l.Introduction
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
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.
3. Features
1. Key-value store
2. Document store
3. Graph based
l. Key-Value Store
{
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.
For example-
Graph base database is mostly used for social networks, logistics, spatial data. Thegraph
databases are Neo4J, Infinite Graph, OrientDB.
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
The column store databases are widely used to manage data warehouses, businessintelligence,
HBase, Cassandra are examples of column based databases
8 Pessimistic. Optimistic
Understanding Collections
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.
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.
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
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.
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.
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
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.
For example
myStudents> show collections
nyStudents> db.create Collection("stedent details)
{ ok: 1}
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
db.collection_name.update(criteria, update_data)
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
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
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:
For example
> CREATE customer ef
2) Read Operation:
For example-
3) Insert Data:
Now, we can create a table by inserting some data into it in column-based mannerusing put
command
1 Supriya Pune 30
4) Update Operation
We can modify some properties of existing table using alter command. We add more
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
name>,<row>
For example-
For displaying the contents of the table 'scan' commad is used. For example