Use triggers in SQL via basic examples

Tram Ho

What is a trigger?

Simply put, the trigger is a stored procedure with no parameters. The trigger executes automatically when one of the three Insert, Update and Delete statements changes the data on the trigger table. ?

Trigger’s syntax

What does trigger use?

  • Triggers are often used to check constraints on multiple relationships (multiple tables / tables) or on multiple lines (multiple records) of the table.
  • In addition to using Trigger, the program has implicit functions to serve but finite cases and is often not used for business or transaction purposes. Read more here

Problem set.

  • You have 2 tables of warehouses and orders linked together by item code.

  • When the user orders, automatically update the inventory number.

Solution

  • When users order, we only have 3 main types of operations with the database: Insert, Delete, Update
  • So just creating 3 corresponding triggers is ok ?
  • User ordered : Quantity left in stock = Quantity left – Number of orders
  • User cancels no order: Quantity left in stock = Quantity left + Number of orders
  • User update Set quantity => Quantity also increases or decreases arbitrarily

Problem

  • In two cases insert and delete we do normally. But in case of updating the quantity of inventory will occur in the following 3 cases.
  1. The initial set amount = 5 then increases to 10 => the number in stock will decrease by 10 respectively
  2. The number placed at this time = 10 then reduced to 3 => the number in stock will increase by 7 respectively
  • Take advantage of update statement sql = Insert new row To Delete old row means that when updating database in sql, it will run the insertion of new data first and then delete the old table.

Problem solving

  • Taking advantage of using a Trigger that always has two inserted and deleted tables, we will draw a medium update formula in all cases

Maybe you want to see:

Optimize WHERE statements in MySQL
Introducing LevelDB and comparing performance with MySQL

Follow the small example

  1. Initially add data and select it ?

  1. Order 5 products with code 1

  1. Update to 10

  1. Update on 3

  1. Update some other information that is not related to quantity

  1. Delete orders

Source code all ?

  1. Additional trigger

  1. Trigger Delete

  1. Trigger Edit


Conclude

The thing that you use Trigger is not mandatory and we often think that no one uses it completely wrong ?. But Trigger, as far as I know, there are still many places that will use it for their own purposes.

Thanks for reading.

Filter customer Insights with data – the decision maker’s decision factor
Use triggers in SQL via basic examples

Analyzing Northern lottery data ”]

Share the news now

Source : Viblo .ud668ec66be69dc69301e8f8128269600 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .ud668ec66be69dc69301e8f8128269600:active, .ud668ec66be69dc69301e8f8128269600:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .ud668ec66be69dc69301e8f8128269600 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .ud668ec66be69dc69301e8f8128269600 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .ud668ec66be69dc69301e8f8128269600 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .ud668ec66be69dc69301e8f8128269600:hover .postTitle { text-decoration: underline!important; } Chắt lọc Insights khách hàng bằng data - nhân tố ra quyết định của người làm sản phẩm .ubb90869ccc0c84751307dc1db1f76903 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .ubb90869ccc0c84751307dc1db1f76903:active, .ubb90869ccc0c84751307dc1db1f76903:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .ubb90869ccc0c84751307dc1db1f76903 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .ubb90869ccc0c84751307dc1db1f76903 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .ubb90869ccc0c84751307dc1db1f76903 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .ubb90869ccc0c84751307dc1db1f76903:hover .postTitle { text-decoration: underline!important; } Sử dụng trigger trong SQL qua ví dụ cơ bản Phân tích dữ liệu xổ số miền Bắc”]