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

Dbms External Exam Notes

Uploaded by

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

Dbms External Exam Notes

Uploaded by

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

1) Triggers in SQL

A trigger is a mechanism in SQL that automatically performs an action when a certain event
occurs in a table or view. It's like setting up a rule that the database must follow whenever
certain conditions are met. Triggers are often used to maintain data integrity, enforce
business rules, or automate tasks such as logging changes or validating data.

Triggers are linked to specific events, and when those events happen, the trigger fires and
executes some action automatically. Triggers can be fired before or after the event,
depending on how they are set up.

Three Components of a Trigger

1. Event:
○ This is the event that causes the trigger to activate. It is typically an operation
like INSERT, UPDATE, or DELETE.
○ For example, if the event is INSERT, the trigger will activate whenever a new
record is inserted into a specific table.
○ Events can be more specific, like triggering only when a particular column is
updated.
2. Condition (Optional):
○ The condition is a check that determines whether the trigger should run or
not. This step is optional, meaning you can have a trigger that always runs
when the event occurs, or one that only runs if certain conditions are met.
○ For example, a trigger might only fire if the inserted value in a column meets a
certain criterion, like a value being greater than 100.
3. Action:
○ The action is the set of SQL statements that the trigger will execute when it
fires. This could be anything from inserting a log, updating another table,
sending notifications, or even preventing certain actions from happening by
rolling back the operation.
○ The action part is where you define what you want to happen after the event
occurs.

Summary:

● Triggers allow automatic execution of actions based on specific database events like
INSERT, UPDATE, or DELETE.
● Triggers help automate tasks, enforce rules, and ensure data integrity within your
database.
2) Commit Point

A transaction reaches its commit point when all the operations it performed have
successfully completed and their effects are saved in the system log (a record of all actions).

Commit Record

Once a transaction reaches its commit point, it writes a commit record in the log. This is a
confirmation that the transaction is successfully completed.

System Failure and Recovery

If a system failure happens, the system looks in the log for transactions that started but didn’t
reach the commit point (they don't have a commit record). These transactions are then
rolled back (undone) to prevent incomplete changes from affecting the database.

3) Stored Procedure Language in SQL

A Stored Procedure is a pre-written and stored block of SQL code that performs a specific
task. Once created, it can be executed multiple times without rewriting the code. Stored
procedures help in automating tasks and making SQL code reusable.

Syntax of a Stored Procedure:

CREATE PROCEDURE procedure_name

AS

BEGIN

-- SQL statements go here

END;
Example:

Let’s create a stored procedure that retrieves employee details based on their department.

CREATE PROCEDURE GetEmployeesByDept

@DeptName VARCHAR(50)

AS

BEGIN

SELECT EmployeeName, Position

FROM Employees

WHERE Department = @DeptName;

END;

To execute the procedure:

EXEC GetEmployeesByDept 'Sales';

Stored procedures are useful in the following circumstances:

● Repetitive Tasks: When the same SQL code needs to be executed frequently.
● Improving Performance: They help in speeding up execution by pre-compiling the
SQL code.
● Data Security: Sensitive business logic can be hidden within the stored procedure,
limiting direct access to data.

To delete a stored procedure, you can use the DROP PROCEDURE command:

DROP PROCEDURE procedure_name;

EX: DROP PROCEDURE GetEmployeesByDept;


4) NoSQL And CAP Properties:

NoSQL is a type of DBMS that is designed to handle large amounts of unstructured and
semi-structured data. Unlike relational databases that use tables and predefined schemas to
store data, NoSQL databases are more flexible and capable of scaling horizontally to store
large amounts of data.

CAP Properties

● Consistency (C):
○ Ensures that all nodes have the same, updated data.
○ Every client sees the most recent data after a successful write.
● Availability (A):
○ Guarantees that every request receives a response, whether it is successful
or failed.
○ Every non-failing node must respond to read/write requests in reasonable
time.
● Partition Tolerance (P):
○ The system continues to function even if network partitions occur.
○ The system can recover and ensure consistency after the network issue is
fixed.

The CAP Theorem states that it is not possible to guarantee all three properties at the same
time in a distributed system with data replication.

A System Can Be:

● CA (Consistency and Availability):


○ Systems ensure that the data is consistent across all the nodes and remains
available, but may fail during network partitions.
○ Examples: Cassandra, CouchDB, Riak, Voldemort.
● AP (Availability and Partition Tolerance):
○ Systems prioritise availability and continue operating during network partitions
but may have inconsistent data.
○ Examples: Amazon DynamoDB, Google Cloud Spanner.
● CP (Consistency and Partition Tolerance):
○ Systems ensure consistent data and continue functioning during network
partitions but may become temporarily unavailable.
○ Examples: Apache HBase, MongoDB, Redis.
5) Document-Based NoSQL

Definition:
Document-based NoSQL systems store data in the form of documents rather than in
traditional tables (like in SQL). These documents are typically in formats such as JSON,
BSON, or XML.

Structure:
Each document contains data in the form of key-value pairs. Unlike SQL tables, documents
in the same collection do not need to have the same structure. For example, one document
can have fields like name and age, while another might have name and address.

"name": "John",

"age": 30

"name": "Alice",

"address": "123 Main St"

Flexibility:
There is no fixed schema, which means you can store different types of data in each
document. This allows for more flexibility in handling unstructured or semi-structured data.

Advantages:

● Flexibility: Different documents can have different structures.


● Scalability: Handles large amounts of data and scales easily across servers.

Disadvantages:

● No Standard Schema: Can lead to data inconsistency.


● Complex Queries: Queries can be more complex than in SQL databases.

Example:
MongoDB: One of the most popular document-based NoSQL databases. In MongoDB, a
collection stores documents (just like a table stores rows in an SQL database), but each
document is independent and can have different structures.
CRUD Operations in MongoDB

CRUD stands for Create, Read, Update, Delete. These are the four basic operations you
can perform on data.

Create:
In MongoDB, data is created using the insert() function.

db.collection.insert({name: "John", age: 30})

The above example inserts a new document into the collection.

db.collection.insertMany([

"name": "John",

"age": 30

},

"name": "Alice",

"address": "123 Main St"

])

Read:
To read data, we use the find() function.

db.collection.find({name: "John"})

db.collection.find({})

Update:
Updating data is done using the update() or updateOne() function.

db.collection.updateOne({name: "John"}, {$set: {age: 31}})

This updates the document where the name is "John" by setting the age to 31.
Delete:
Deleting data is done using the deleteOne() or deleteMany() function.

db.collection.deleteOne({name: "John"})

This deletes the document where the name is "John".

Also refer to the lab program for crud operations…

6) NoSQL Graph-Based Database: Neo4j

Definition:
A NoSQL graph database stores data in nodes (vertices) and edges (relationships) that
connect these nodes. It’s designed to handle data with complex relationships.

Structure:

● Nodes: Represent entities (e.g., people, places, or things).


● Edges: Represent relationships between nodes (e.g., "friend of," "located in").
● Properties: Both nodes and edges can have properties (e.g., a person node might
have properties like name and age, while an edge might have a property like "since"
to indicate when the relationship started).

Key Features:

● Flexible Schema: No predefined schema is required. You can add new types of
nodes or relationships easily.
● Efficient Relationship Handling: Graph databases are designed to efficiently
handle complex relationships and connections between data points.

Advantages:

● Efficient Relationships: Fast at querying complex connections.


● Flexible Schema: No fixed structure, adapting easily to data changes.

Disadvantages:

● Complexity: More complex to model and query compared to SQL.


● Scalability: May have challenges scaling for very large graphs.

Applications:

● Social Networks: Manage and analyse user connections and interactions.


● Recommendation Engines: Suggest products or content based on user behaviour
and preferences.
Neo4j

Definition:
Neo4j is a popular NoSQL graph database that uses a graph structure to store and manage
data. It is widely used for applications that require deep and complex relationship querying.

Features:

● Graph Model: Stores data as nodes (entities) and edges (relationships). Makes it
easy to represent and query complex connections between data.
● Cypher Query Language: A special language for querying graph data. Designed to
be intuitive for finding and analysing relationships.
● Performance: Optimized for fast queries on connected data. Quickly handles
complex relationships.

Neo4j Example Code (Simplified):

Create Nodes:

CREATE (p:Person {name: 'John', age: 30})

CREATE (a:Person {name: 'Alice', address: '123 Main St'})

Creates two people: John with age and Alice with an address.

Create Relationships:
MATCH (a:Person {name: 'John'}), (b:Person {name: 'Alice'})

CREATE (a)-[:FRIENDS_WITH]->(b)

Links John and Alice as friends.

Query Nodes:

MATCH (p:Person)

RETURN p

Finds all people in the database.


Find Relationships:

MATCH (a:Person)-[:FRIENDS_WITH]->(b:Person)

RETURN a.name, b.name

Lists all friend connections and their names.

Neo4j Advantages:

1. Efficient Relationships: Fast at managing and querying complex connections.


2. Flexible Schema: Adapts easily to changing data structures.

Neo4j Disadvantages:

1. Complexity: Can be harder to use and understand compared to SQL databases.


2. Scalability: May struggle with very large datasets.

You might also like