Dbms External Exam Notes
Dbms External Exam Notes
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.
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.
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.
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.
AS
BEGIN
END;
Example:
Let’s create a stored procedure that retrieves employee details based on their department.
@DeptName VARCHAR(50)
AS
BEGIN
FROM Employees
END;
● 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:
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.
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",
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:
Disadvantages:
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.insertMany([
"name": "John",
"age": 30
},
"name": "Alice",
])
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.
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"})
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:
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:
Disadvantages:
Applications:
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.
Create Nodes:
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)
Query Nodes:
MATCH (p:Person)
RETURN p
MATCH (a:Person)-[:FRIENDS_WITH]->(b:Person)
Neo4j Advantages:
Neo4j Disadvantages: