Home >Database >Mysql Tutorial >What is the difference between stored procedures and triggers in database

What is the difference between stored procedures and triggers in database

清浅
清浅Original
2019-04-26 17:36:2425768browse

The differences between stored procedures and triggers are: 1. Stored procedures can use input parameters, but triggers cannot; 2. Stored procedures can return zero or n values, but triggers cannot return values; 3. Transactions can be used in stored procedures, but not triggers.

What is the difference between stored procedures and triggers in database

【Recommended course: MySQL Tutorial

Stored procedures and triggers are both very important knowledge in the database. Next, the article will compare the differences between the two through many aspects. It has a certain reference effect and I hope it will be helpful to everyone.

What is a trigger?

A trigger is a process (code segment) that is automatically executed when certain events occur in the tables/views in the database. Triggers are mainly used to maintain integrity in the database. Triggers are also used to enforce business rules, audit changes in the database, and replicate data. The most common triggers are Data Manipulation Language (DML) triggers that fire when data is manipulated. Some database systems support non-data triggers, which fire when data definition language (DDL) events occur. These triggers can be used specifically for auditing. Oracle database system supports schema-level triggers

What is a stored procedure?

Stored procedures are applications that can access relational databases. Typically, stored procedures are used to validate data and control access to the database. If some data processing operations require the execution of multiple SQL statements, such operations are implemented as stored procedures. When calling a stored procedure, you must use the CALL or EXECUTE statement. Stored procedures can return results (such as the results of a SELECT statement). These results can be used by other stored procedures or applications. The language used to write stored procedures usually supports control structures such as if, while, for, etc. Depending on the database system used, multiple languages ​​can be used to implement stored procedures

The difference between stored procedures and triggers

(1) A stored procedure is a set of SQL statements are created and stored in the database. So we can reuse the code over and over again. A trigger is a special stored procedure that is not directly called by the user. When a trigger is created, it is defined to trigger when a specific type of data modification is made to a specific table or column.

(2) Users can use Execute or Exec statements to directly call or execute stored procedures, but cannot directly call or execute triggers. Only the trigger is automatically executed when the relevant event is fired.

(3) Stored procedures can take input parameters, but parameters cannot be used as input in triggers. We cannot pass parameters as input to triggers.

(4) The stored procedure can return zero or n values, but the trigger cannot return a value.

(5) We can use transactions in stored procedures, transaction processing is not allowed in triggers

(6) Stored procedures are usually used to perform user-specified tasks, and triggers are usually used For audit work

The above is the detailed content of What is the difference between stored procedures and triggers in database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more