Difference between Assertions and Triggers in DBMS
Last Updated :
06 Sep, 2024
In DBMS, there is always the need to make sure that data in the database is both consistent and intact. This can be done with the help of mechanisms such as assertions and triggers or any other similar one. They both encourage compliance with rules and conditions but they do it in different ways and at different times. This article focuses on decoding the major differences between assertions and triggers to let the reader know precisely when to use one in database management.
What are Assertions?
When a constraint involves 2 (or) more tables, the table constraint mechanism is sometimes hard and results may not come as expected. To cover such a situation SQL supports the creation of assertions that are constraints not associated with only one table. An assertion statement should ensure a certain condition will always exist in the database. DBMS always checks the assertion whenever modifications are done in the corresponding table.
Syntax -
CREATE ASSERTION [ assertion_name ]
CHECK ( [ condition ] );
Example -
CREATE TABLE sailors (sid int,sname varchar(20), rating int,primary key(sid),
CHECK(rating >= 1 AND rating <=10)
CHECK((select count(s.sid) from sailors s) + (select count(b.bid)from boats b)<100) );
In the above example, we enforcing CHECK constraint that the number of boats and sailors should be less than 100. So here we are able to CHECK constraints of two tablets simultaneously.
What are Triggers?
A trigger is a database object that is associated with the table, it will be activated when a defined action is executed for the table. The trigger can be executed when we run the following statements:
- INSERT
- UPDATE
- DELETE
And it can be invoked before or after the event.
Syntax -
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Example -
create trigger t1 before UPDATE on sailors
for each row
begin
if new.age>60 then
set new.age=old.age;
else
set new.age=new.age;
end if;
end;
$
In the above example, we are creating triggers before updates. so, if the new age is greater than 60 we should not update else we should update. We can call this trigger by using "$" symbol.
Difference Between Assertions and Triggers
Assertions | Triggers |
---|
We can use Assertions when we know that the given particular condition is always true. | We can use Triggers even particular condition may or may not be true. |
When the SQL condition is not met then there are chances to an entire table or even Database to get locked up. | Triggers can catch errors if the condition of the query is not true. |
Assertions are not linked to specific table or event. It performs task specified or defined by the user. | It helps in maintaining the integrity constraints in the database tables, especially when the primary key and foreign key constraint are not defined. |
Assertions do not maintain any track of changes made in table. | Triggers maintain track of all changes occurred in table. |
Assertions have small syntax compared to Triggers. | They have large Syntax to indicate each and every specific of the created trigger. |
Modern databases do not use Assertions. | Triggers are very well used in modern databases. |
Purpose of assertions is to Enforces business rules and constraints. | Purpose of triggers is to Executes actions in response to data changes. |
Activation is checked after a transaction completes | Activation is activated by data changes during a transaction |
Granularity applies to the entire database | Granularity applies to a specific table or view |
Syntax Uses SQL statements | Syntax Uses procedural code (e.g. PL/SQL, T-SQL) |
Error handling Causes transaction to be rolled back. | Error handling can ignore errors or handle them explicitly |
Assertions may slow down performance of queries. | Triggers Can impact performance of data changes. |
Assertions are Easy to debug with SQL statements. | Triggers are more difficult to debug procedural code |
Examples- CHECK constraints, FOREIGN KEY constraints | Examples - AFTER INSERT triggers, INSTEAD OF triggers |
Conclusion
Assertions and triggers are both important in managing the databases because of the following reasons. Assertions are especially useful for expressing and enforcing compound existing and future conditions across multiple tables, on the other hand, trigger are all-inclusive and are broadly used for other purposes such as automating responses to alterations to data. Comparing the described above two mechanisms makes understanding of what tool to choose for enforcing business rules and maintaining data integrity in the hands of a database administrator.
Similar Reads
What is OSI Model? - Layers of OSI Model The OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re
13 min read
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Introduction of ER Model The Entity-Relationship Model (ER Model) is a conceptual model for designing a databases. This model represents the logical structure of a database, including entities, their attributes and relationships between them. Entity: An objects that is stored as data such as Student, Course or Company.Attri
10 min read
DBMS Tutorial â Learn Database Management System Database Management System (DBMS) is a software used to manage data from a database. A database is a structured collection of data that is stored in an electronic device. The data can be text, video, image or any other format.A relational database stores data in the form of tables and a NoSQL databa
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read