Меню Закрыть

Mysql trigger after insert

Содержание

I am new to MySQL. I have two tables total_loaner and available_loaner. I am trying to create a trigger for every new row added in total_loaner, I would to add that new row to available_loaner.

Here how my tables look like:

My trigger does not seem to work.

4 Answers 4

In your case you can rewrite your trigger like this

Note:

  • single quotes removed from table name total_loaner , because quotes effectively makes it a string literal instead of a proper identifier. You can use back ticks if you want but it’s unnecessary since it’s not a reserved word and it don’t contain any special characters.
  • since it’s a one-statement trigger now you don’t need to use DELIMITER command and BEGIN. END block

Here is SQLFiddle demo

You probably need to set your delimiter:

Right now, it’s confusing the semi-colon at the end of the INSERT statement with the end of the CREATE TRIGGER statement.

This one worked for me, more simplified version..

Not the answer you’re looking for? Browse other questions tagged mysql triggers or ask your own question.

Linked

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.1.14.35781

Базы данных

В этом учебном пособии вы узнаете, как создать триггер AFTER INSERT (после вставки) в MySQL с синтаксисом и примерами.

Описание

Триггер AFTER INSERT исполняется средствами MySQL после выполнения операции INSERT.

Синтаксис

Синтаксис создания ТРИГГЕРА AFTER INSERT в MySQL:

Читайте также:  Деление окружности на градусы

Параметры или аргументы

trigger_name — наименование создаваемого триггера.
AFTER INSERT — указывает, что триггер срабатывает после выполнения операции INSERT.
table_name — наименование таблицы, для которой создается триггер.

Ограничения

  • Вы не можете создавать триггер AFTER в представлениях (view).
  • Вы не можете обновить NEW (новые) значения.
  • Вы не можете обновить OLD (старые) значения.

Примечание

  • Смотрите также: как создать триггеры AFTER DELETE, AFTER UPDATE, BEFORE DELETE, BEFORE INSERT, и BEFORE UPDATE.
  • Смотрите также: как удалить триггер.

Пример

Рассмотрим пример, как создать триггер AFTER INSERT, используя инструкцию CREATE TRIGGER в MySQL.

Если вы создали таблицу следующей структуры:

This MySQL tutorial explains how to create an AFTER INSERT Trigger in MySQL with syntax and examples.

Description

An AFTER INSERT Trigger means that MySQL will fire this trigger after the INSERT operation is executed.

Syntax

The syntax to create an AFTER INSERT Trigger in MySQL is:

Parameters or Arguments

Restrictions

  • You can not create an AFTER trigger on a view.
  • You can not update the NEW values.
  • You can not update the OLD values.
  • See also how to create AFTER DELETE, AFTER UPDATE, BEFORE DELETE, BEFORE INSERT, and BEFORE UPDATE triggers.
  • See also how to drop a trigger.

Example

Let’s look at an example of how to create an AFTER INSERT trigger using the CREATE TRIGGER statement in MySQL.

If you had a table created as follows:

We could then use the CREATE TRIGGER statement to create an AFTER INSERT trigger as follows:

While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.

Читайте также:  Проверка телефона по имей на кражу

We use advertisements to support this website and fund the development of new content.

Рекомендуем к прочтению

Добавить комментарий

Ваш адрес email не будет опубликован.