0% found this document useful (0 votes)
8 views65 pages

AD3391 Unit5

The document outlines the objectives and content of a course on database design and management, focusing on various database models including relational, object-relational, and No-SQL databases. It covers topics such as SQL, the database development life cycle, and user-defined types, along with practical examples of MongoDB and its advantages over traditional RDBMS. Additionally, it includes detailed steps for mapping EER to ODB schema and CRUD operations in MongoDB.

Uploaded by

mk4997320
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)
8 views65 pages

AD3391 Unit5

The document outlines the objectives and content of a course on database design and management, focusing on various database models including relational, object-relational, and No-SQL databases. It covers topics such as SQL, the database development life cycle, and user-defined types, along with practical examples of MongoDB and its advantages over traditional RDBMS. Additionally, it includes detailed steps for mapping EER to ODB schema and CRUD operations in MongoDB.

Uploaded by

mk4997320
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/ 65

AD3391 DATABASE DESIGN AND MANAGEMENT LT PC

3003
COURSE OBJECTIVES:
To introduce database development life cycle and conceptual modeling.
To learn SQL for data definition, manipulation and querying a database.
To learn relational database design using conceptual mapping and normalization.
To learn transaction concepts and serializability of schedules.
To learn data model and querying in object-relational and No-SQL databases.
TEXT BOOKS:
1. Thomas M. Connolly, Carolyn E. Begg, Database Systems – A Practical Approach to Design, Implementation, and
Management, Sixth Edition, Global Edition, Pearson Education, 2015.
M. RAMNATH,
2. Ramez Elmasri, Shamkant B. Navathe, Fundamentals of Database Systems, AP /Pearson,
7th Edition, AI&DS, 2017.
[email protected]
RIT

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


UNIT V OBJECT RELATIONAL AND NO-SQL DATABASES 9
Mapping EER to ODB schema – Object identifier – reference types – row types – UDTs –
Subtypes and supertypes – user-defined routines – Collection types – Object Query
Language; No-SQL: CAP theorem – Document-based: MongoDB data model and CRUD
operations; Column-based: Hbase data model and CRUD operations.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Topic Text Book 1 Text Book 2
Unit – V OBJECT RELATIONAL AND NO-SQL DATABASES
Mapping EER to ODB schema
Object identifier – reference types – row types
UDTs – Subtypes and supertypes
user-defined routines – Collection types
Object Query Language
No-SQL: CAP theorem
Document-based: MongoDB data model and CRUD operations
Column-based: Hbase data model and CRUD operations

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object model database
 In the object model of the database, data is represented and
stored as objects, similar to objects used in programming
languages.
 This model allows the application of object-oriented
principles such as encapsulation, inheritance, and
polymorphism to the database design.
 It offers a way to manage data with complex relationships
and behavior by combining data and behavior into a single
unit.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object database conceptual design
 The objects in computer programs to represent real-world
entities and data.
 It is very much flexible and dynamic compared to the
relational database model.
 The relationships between different objects, which can be
very complex, are modeled very easily.
 Object-oriented based concepts such as inheritance,
encapsulation, and polymorphism.
 The codes can be maintained and modified easily in
databases based on the object-oriented based model.
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Object Database Relational Database
Properties or reference attributes that include Object Attributes with matching values specify relationships
Ids of the objects are used to handle the object's among the tuples (records).
relationships.

Relationships among the object are represented using Relationships are single-valued since multivalued
single references and collections of references. attributes are not allowed in the relational model.

Inheritance constructs such as derived (:) and extends No inheritance construct is available in a relational
are used to handle inheritance. model.
Ad-hoc queries can be handled by an object-oriented Ad-hoc queries are not encouraged in a relational
database. database.
Mapping binary relationships with attributes can be A: B relationships must be represented as separate
difficult, and using a separate class to represent the relations (table).
relationship itself can be more advantageous.

The object model defines a set of valid behaviors or The relational model does not require database
operations. designers to predefine a specific set of valid behaviors
or operations.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Mapping EER to ODB schema
 A schema is used to define the structure of the database and
how the tables and data models are related to each other.
 It defines the framework for storing and managing data.
 Mapping an EER schema to an Object Database schema is
designing the type declarations of object classes for an
Object Database Management System (ODBMS) from an
Entity-Relationship (ER) diagram.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Mapping EER to ODB schema
 Step-1: Create an ODL class for each EER entity type or
subclass. The ODL class should include all the attributes of
the EER class.
 Multivalued details are typically declared using set, bag, or
list constructors. Declare an extent for each class and
specify any critical attributes as keys of the extent.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Step1: Identify Entities
@Entity
public class Person {
@Id
private Long personId;
private String name;
private int age;
@OneToMany(mappedBy = "person")
private List<Address> addresses;
// Constructors, getters, setters, and other methods
}
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Mapping EER to ODB schema
 Step-2: Add relationship properties or reference attributes
for each binary relationship into the ODL classes
participating in the relationship.
 Depending on the cardinality ratio of the binary
relationship, the relationship properties or reference
attributes may be single-valued or collection types.
class Person (extent persons key ssn) {
attribute struct Pname {string fname, string lname} name;
attribute string ssn; attribute date birthdate; short age();
}
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Step 2: Map Attributes to Fields
@Entity public class Address {
@Id
private Long addressId;
private String street;
private String city;
@ManyToOne
@JoinColumn(name = "personId")
private Person person;
// Constructors, getters, setters, and other methods}
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Mapping EER to ODB schema
 Step-3: Include appropriate operations for each class. These
are not available from the EER schema and must be added
to the database design by referring to the original
requirements.

 Step-4: An ODL class corresponding to a subclass in the


EER schema inherits the type and methods of its superclass
in the ODL schema. Its specific (non-inherited) attributes,
relationship references, and operations are specified.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Step 4: Inheritance Mapping
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Employee extends Person {
private String jobTitle;

// Additional fields specific to employees


}

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Mapping EER to ODB schema
 Step-5: Weak entity types can be mapped in the same way
as regular entity types. An alternative mapping is possible
for weak entity types that do not participate in any
relationships except their identifying relationship.

 Step-6: Categories (union types) in an EER schema are


difficult to map to ODL. One way is to create a class to
represent the category and define 1:1 relationships between
the category and each of its super classes.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Step 6: Associative Entities
@Entity
public class Project {
@Id
private Long projectId;
private String projectName;
@ManyToMany
private List<Employee> employees;
// Constructors, getters, setters, and other methods
}
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Mapping EER to ODB schema

 Step-7: An n-array relationship with degree n > 2 can be


mapped into a separate class, with appropriate references to
each participating class. An M: N binary relationship may
also use this mapping option.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


User-Defined Types (UDTs)
 User-Defined Types (UDTs) and subtypes are concepts in
SQL that allow you to create custom data types and extend
existing types.
1. Creating a UDT:
Description: Define a new user-defined data type.
Query:
CREATE TYPE ColorType AS ENUM ('Red', 'Green', 'Blue');

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


User-Defined Types (UDTs)
2. Creating a Table with UDT Column:
Description: Create a table using a UDT as one of its columns.
Query:
CREATE TABLE Cars ( Model VARCHAR(50), Manufacturer
VARCHAR(50), PaintColor ColorType );

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


User-Defined Types (UDTs)
3. Inserting Data into UDT Column:
Description: Insert data into a table with a UDT column.
Query:
INSERT INTO Cars (Model, Manufacturer, PaintColor)
VALUES ('Accord', 'Honda', 'Blue');
4. Querying Data from UDT Column:
Description: Retrieve data from a table with a UDT column.
Query:
SELECT * FROM Cars WHERE PaintColor = 'Red';

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


User-Defined Types (UDTs)
5. Updating Data in UDT Column:
Description: Update data in a UDT column.
Query:
UPDATE Cars SET PaintColor = 'Green' WHERE
Manufacturer = 'Toyota';

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


User-Defined Types (UDTs)
6. Subtypes with CHECK Constraint:
Description: Define a subtype using a CHECK constraint.
Query:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Position VARCHAR(50),
CONSTRAINT CheckManager CHECK (Position =
'Manager')
);
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
User-Defined Types (UDTs)
7. Inserting Data into Subtype Table:
Description: Insert data into a table with a subtype
constraint.
Query:
INSERT INTO Employees (EmployeeID, FirstName,
LastName, Position) VALUES (1, 'John', 'Doe', 'Manager');

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


User-Defined Types (UDTs)
8. Querying Data from Subtype Table:
Description: Retrieve data from a table with a subtype
constraint.
Query:
SELECT * FROM Employees WHERE Position = 'Manager';

9. Updating Data in Subtype Table:


Description: Update data in a table with a subtype constraint.
Query:
UPDATE Employees SET Position = 'Supervisor' WHERE
EmployeeID = 1;
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
User-Defined Types (UDTs)
10. Dropping UDT:
Description: Remove a user-defined data type.
Query:
DROP TYPE ColorType;

11. Dropping Subtype Table:


Description: Remove a table with subtype constraints.
Query:
DROP TABLE Employees;

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object Features of SQL-UDTs:
Description: UDTs allow you to create custom data types with
associated attributes and behaviors.
Object Features:
Attributes (Fields): Define attributes for the UDT.
Methods (Functions): Define methods that operate on the UDT.
Constructor: Specify a constructor to initialize UDT instances.
Destructor: Specify a destructor to clean up resources when
UDT instances are destroyed.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object Features of SQL-UDTs:
Inheritance:
Description: UDTs can be used to create hierarchical structures
through inheritance.
Example:
CREATE TYPE PersonType AS (
person_id INT,
person_name VARCHAR,
birth_date DATE);

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object Features of SQL-UDTs:

CREATE OR REPLACE FUNCTION getFullName(p


PersonType) RETURNS VARCHAR AS $$
BEGIN
RETURN p.person_name;
END;
$$ LANGUAGE plpgsql;

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object Features of SQL-UDTs:
Tables Using UDTs:
Description: UDTs can be used as columns in tables.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
EmployeeInfo PersonType
);

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object Features of SQL-UDTs:
Method Definition:
Description: Define methods that operate on UDT instances.
Example:
CREATE OR REPLACE FUNCTION getFullName(p PersonType)
RETURNS VARCHAR
AS $$
BEGIN
RETURN p.FirstName || ' ' || p.LastName;
END;
$$ LANGUAGE plpgsql;

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Object Features of SQL-UDTs:
Method Implementation:
Description: Implement the logic of a method for a UDT.
Example:
CREATE TYPE BODY PersonType AS
MEMBER FUNCTION getFullName RETURN VARCHAR
IS
BEGIN
RETURN FirstName || ' ' || LastName;
END;
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
MongoDB - Overview
 MongoDB is a cross-platform, document oriented database
that provides, high performance, high availability, and easy
scalability.
 MongoDB works on concept of collection and document.
Database
 Database is a physical container for collections. Each
database gets its own set of files on the file system.
 A single MongoDB server typically has multiple databases.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Collection
 Collection is a group of MongoDB documents.
 It is the equivalent of an RDBMS table.
 A collection exists within a single database.
 Collections do not enforce a schema.
 Documents within a collection can have different fields.
 Typically, all documents in a collection are of similar or
related purpose.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Document
 A document is a set of key-value pairs. Documents have
dynamic schema.
 Dynamic schema means that documents in the same
collection do not need to have the same set of fields or
structure, and common fields in a collection's documents
may hold different types of data.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents

Primary Key (Default key _id


Primary Key
provided by MongoDB itself)
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Document
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Document
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Document
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Advantages of MongoDB over RDBMS
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.
Deep query-ability. MongoDB supports dynamic queries on
documents using a document-based query language that's
nearly as powerful as SQL.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Advantages of MongoDB over RDBMS

Ease of scale-out − MongoDB is easy to scale.


Conversion/mapping of application objects to database objects
not needed.
Uses internal memory for storing the (windowed) working set,
enabling faster access of data.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Why Use MongoDB?
Document Oriented Storage − Data is stored in the form of
JSON style documents.
 Index on any attribute
 Replication and high availability
 Auto-Sharding
 Rich queries
 Fast in-place updates
 Professional support by MongoDB

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Where to Use MongoDB?
 Big Data
 Content Management and Delivery
 Mobile and Social Infrastructure
 User Data Management
 Data Hub

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


CRUD Operations
 The Create operation is used to insert new documents in the
MongoDB database.
 The Read operation is used to query a document in the
database.
 The Update operation is used to modify existing documents
in the database.
 The Delete operation is used to remove documents in the
database.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Create Operations
 For MongoDB CRUD, if the specified collection doesn’t
exist, the create operation will create the collection when
it’s executed.
 Create operations in MongoDB target a single collection,
not multiple collections.
 Insert operations in MongoDB are atomic on a single
document level.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Read Operations
 The read operations allow you to supply special query
filters and criteria that let you specify which documents you
want.
 The MongoDB documentation contains more information
on the available query filters.
 Query modifiers may also be used to change how many
results are returned.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Read Operations
MongoDB has two methods of reading documents from a
collection:
db.collection.find()
db.collection.findOne()

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Update Operations
 Like create operations, update operations operate on a
single collection, and they are atomic at a single document
level.
 An update operation takes filters and criteria to select the
documents you want to update.
 You should be careful when updating documents, as updates
are permanent and can’t be rolled back.
 This applies to delete operations as well.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Update Operations
For MongoDB CRUD, there are three different methods of
updating documents:

 db.collection.updateOne()
 db.collection.updateMany()
 db.collection.replaceOne()

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Delete Operations
 Delete operations operate on a single collection, like update
and create operations.
 Delete operations are also atomic for a single document.
 You can provide delete operations with filters and criteria in
order to specify which documents you would like to delete
from a collection.
 The filter options rely on the same syntax that read
operations utilize.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Delete Operations

MongoDB has two different methods of deleting records from


a collection:

 db.collection.deleteOne()
 db.collection.deleteMany()

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


CAP Theorem:
 The CAP theorem, originally introduced as the CAP
principle, can be used to explain some of the competing
requirements in a distributed system with replication.
 The three letters in CAP refer to three desirable properties
of distributed systems with replicated data: consistency
(among replicated copies), availability (of the system for
read and write operations) and partition tolerance (in the
face of the nodes in the system being partitioned by a
network fault).
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
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 same time in a
distributed system with data replication.

 The theorem states that networked shared-data systems can


only strongly support two of the following three properties:

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Consistency
 Consistency means that the nodes will have the same copies
of a replicated data item visible for various transactions.
 A guarantee that every node in a distributed cluster returns
the same, most recent and a successful write.
 Consistency refers to every client having the same view of
the data.
 There are various types of consistency models. Consistency
in CAP refers to sequential consistency, a very strong form
of consistency.
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Availability
 Availability means that each read or write request for a data
item will either be processed successfully or will receive a
message that the operation cannot be completed.
 Every non-failing node returns a response for all the read
and write requests in a reasonable amount of time.
 The key word here is “every”. In simple terms, every node
(on either side of a network partition) must be able to
respond in a reasonable amount of time.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Partition tolerance
 Partition tolerance means that the system can continue
operating even if the network connecting the nodes has a
fault that results in two or more partitions, where the nodes
in each partition can only communicate among each other.
 That means, the system continues to function and upholds
its consistency guarantees in spite of network partitions.
 Network partitions are a fact of life.
 Distributed systems guaranteeing partition tolerance can
gracefully recover from partitions once the partition heals.
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
CA(Consistency and Availability)
 The system prioritizes availability over consistency and can
respond with possibly stale data.
 Example databases: Cassandra, CouchDB, Riak, Voldemort.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


AP(Availability and Partition Tolerance)
 The system prioritizes availability over consistency and can
respond with possibly stale data.
 The system can be distributed across multiple nodes and is
designed to operate reliably even in the face of network
partitions.
 Example databases: Amazon DynamoDB, Google Cloud
Spanner.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Row-Oriented DB:
 Traditional Database follows a row-oriented design to store
the data.
 It is based on a fixed schema and easy to read and write.
 The row and all its column values persisted together in the
disk/memory.
 The Row-Oriented Database is suitable for OLTP systems
where you want to insert a bunch of attributes for one
record.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Row-Oriented DB:
 This system is not efficient for aggregation function as the
system has to scan the entire table and provide output.
 Ex: When we do a table scan, let’s say select column-b from
table where column-a=’Something’.
 In this case, the query will read every row in the table and
looks for the column-b in each row.
 This involves a lot of I/O costs.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Column-Oriented DB:
 Column-Oriented Databases stores data in disk column by
column. Data is also retrieved in columns.
 This is suitable for OLAP systems.
 Faster access to data during select queries.
 Efficiently perform aggregation because it has to deal with
only columns and involves less I/O cost.

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


HBase CRUD Operations:
HBase provides shell commands to directly interact with the
Database and below are a few most used shell commands.
Create a table with Namespace:
A namespace is nothing but a logical grouping of
tables.’company_empinfo’ is the namespace id in
the below command.

create 'company_empinfo:employee', 'Personal info',


'Professional Info'
Department of Artificial Intelligence and Data Science, Ramco Institute of Technology
Read:
‘get’ and ‘scan’ command is used to read data from HBase.
get: ‘get’ operation returns a single row from the HBase table.
Given below is the syntax for the ‘get’ method.
get 'table Name', 'Row Key‘

hbase(main):022:get 'employee', 1

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


scan:
‘scan’ command is used to retrieve multiple rows.
Select all:
The below command is an example of a basic search on the
entire table.
scan 'Table Name‘
hbase(main):074:> scan 'employee'

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Update
To update any record HBase uses ‘put’ command. To update any
column value, users need to put new values and HBase will
automatically update the new record with the latest timestamp.
put 'employee', 1, 'Personal info:empId', 30
The old value will not be deleted from the HBase table. Only the
updated record with the latest timestamp will be shown as query
output.
To check the old value of any row use below command.
get 'Table Name', 'Row Key', {COLUMN => 'Column Family',
VERSIONS => 3}

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Delete
‘delete‘ command is used to delete individual cells of a record.

delete 'Table Name' ,'Row Key','Column Family:Column‘


delete 'employee',1, 'Personal info:Name'

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology


Drop Table:
To drop any table in HBase, first, it is required to disable the
table. The query will return an error if the user is trying to
delete the table without disabling the table. Disable removes
the indexes from memory.
disable 'employee‘
drop 'employee'

Department of Artificial Intelligence and Data Science, Ramco Institute of Technology

You might also like