简体   繁体   English

在另一个表上插入值后,MySQL触发

[英]MySQL Trigger After Insert Value on Another Table

I have a table with a list of my cellphone contacts this is the structure of contacts table. 我有一张带有我的手机通讯录列表的表,这是通讯录表的结构。

在此处输入图片说明

And the other one is smartteleco table. 另一个是smartteleco桌子。

在此处输入图片说明

The ContactId of smartteleco table is FOREIGN KEY to the ContactId of contacts . smartteleco表的使用ContactID是外键触点 的ContactID。

I'm creating a trigger that if I INSERT a value into contacts table and the ContactNumber1 value starts with 0918,0919,0920,0921 it will be stored in the CellNumber field of smartteleco table. 我正在创建一个触发器,如果​​我在联系人表中插入一个值并且ContactNumber1值以0918,0919,0920,0921开头 ,它将存储在smartteleco表的CellNumber字段中。

These are the triggers I've already tried but there is a syntax error and the error is always inside the IF STATEMENT . 这些是我已经尝试过的触发器,但是存在语法错误,并且该错误始终在IF STATEMENT内部。

CREATE TRIGGER addSmart
    AFTER INSERT ON contacts
        FOR EACH ROW
        BEGIN
             IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
                SET CellNumber = substring(ContactNumber1,1,11);
             END IF;
        END



CREATE TRIGGER addSmart
    AFTER INSERT ON contacts
        FOR EACH ROW
        BEGIN
             IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
                INSERT INTO smartteleco (CellNumber) VALUES (ContactNumber1);
             END IF;
        END



CREATE TRIGGER addSmart
        AFTER INSERT ON contacts
            FOR EACH ROW
            BEGIN
                 IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
                    INSERT INTO smartteleco (NEW.CellNumber) VALUES (NEW.ContactNumber1);
                 END IF;
            END


CREATE TRIGGER addSmart
    AFTER INSERT ON contacts
        FOR EACH ROW
        BEGIN
             IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
                SET CellNumber = ContactNumber1;
             END IF;
        END



  CREATE TRIGGER addSmart
    AFTER INSERT ON contacts
        FOR EACH ROW
        BEGIN
             IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
                SET NEW.CellNumber = NEW.ContactNumber1;
             END IF;
        END

A row trigger cannot directly access the columns of the table to which it is attached. 行触发器不能直接访问它所连接的表的列。 It can access the values in the newly-inserted record by using the pseudorecord NEW . 可以使用伪记录NEW 访问新插入的记录中的值。 To make your triggers work, just replace the references to ContactNumber1 with references to NEW.ContactNumber1 . 为了使触发器起作用,只需将对ContactNumber1的引用替换为对NEW.ContactNumber1引用。

You also need to use the correct syntax for your IF structures, which is 您还需要为IF结构使用正确的语法,即

IF <contition> THEN  --< You need this keyword
  <statement>
END IF;

You might also need to cast ContactNumber1 as CHAR to use substring() on it. 您可能还需要将ContactNumber1CHAR才能在其上使用substring()

So the correct trigger syntax should be 所以正确的触发语法应该是

CREATE TRIGGER addSmart
  AFTER INSERT ON contacts
  FOR EACH ROW
BEGIN
  IF(substring(CAST(NEW.ContactNumber1 AS CHAR(11)), 1, 4) IN ('0918', '0919', '0920' ,'0921'))
  THEN
    INSERT INTO smartteleco (ContactId, CellNumber)
      VALUES (NEW.ContactId, NEW.ContactNumber1);
  END IF;
END

EDIT: Syntax corrected. 编辑:语法已更正。 I also discovered that you will need to change the column ContactNumber1 from INT(11) to CHAR(11) , which is actually the preferred data type since you will never be performing arithmetic on a phone number. 我还发现,您将需要将ContactNumber1列从INT(11)更改为CHAR(11) ,这实际上是首选的数据类型,因为您将永远不会对电话号码执行算术运算。

Try this: 尝试这个:

    BEGIN
         IF SUBSTRING(NEW.ContactNumber1,1,4) IN ('0918','0919','0920','0921') THEN
             INSERT INTO smartteleco (CellNumber) VALUES (NEW.ContactNumber1);
         END IF;
    END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM