使用trigger将新行插入另一个表中的一个表?

时间:2021-04-25 19:37:31

I have 2 tables

我有2张桌子

Favourites Table

----------------------------------------------------------------------
| FavouritesID | CompanyID | CustomerID | ProfilePhoto | CompanyName |
----------------------------------------------------------------------
|                                                                    |
|                                                                    |
|                                                                    |
----------------------------------------------------------------------

Profile Table

-------------------------------------------
| CompanyID | CompanyName | ProfilePhoto  |
-------------------------------------------
|      1    | Nike        |http://loca..  |
|      2    | Adidas      |http://loca..  |
|      3    | PaulSmith   |http://loca..  |
-------------------------------------------

I want to INSERT CompanyName and ProfilePhoto into the Favourites Table using the CompanyID

我想使用CompanyID将CompanyName和ProfilePhoto插入到收藏夹表中

So when a row is inserted to Favourites table ex CompanyID=1, the CompanyName=Nike and ProfilePhoto=htpp://local... will be inserted to favourites table, getting info from the Profile Table.

因此,当一行插入到公司ID = 1的收藏夹表格中时,CompanyName = Nike和ProfilePhoto = htpp:// local ...将被插入到收藏夹表格,从“配置文件表”获取信息。

This is what I have so far:

这是我到目前为止:

CREATE TRIGGER `Favourites` BEFORE INSERT ON `Favourites`
 FOR EACH ROW BEGIN
    INSERT INTO Favourites SET ProfilePhoto = (SELECT ProfilePhoto FROM Profile WHERE NEW.CompanyID = CompanyID);
END

What are the errors in my code? It does not work

我的代码中有哪些错误?这是行不通的

2 个解决方案

#1


0  

Why do you need to store the same data twice? I was always taught not to store the same information in multiple tables. If you need a favorites table why not just store:

为什么需要存储两次相同的数据?我总是被告知不要在多个表中存储相同的信息。如果你需要一个收藏夹表,为什么不只是存储:

| FavouritesID | CompanyID | CustomerID |

Then if you need other information from the profile table find it with company ID from the favorites table

然后,如果您需要配置文件表中的其他信息,请从收藏夹表中找到公司ID

#2


0  

I think U can use AFTER INSERT .............

我想你可以使用AFTER INSERT .............

CREATE OR REPLACE TRIGGER orders_after_insert
AFTER INSERT
   ON orders
   FOR EACH ROW

DECLARE
   v_username varchar2(10);

BEGIN

   -- Find username of person performing the INSERT into the table
   SELECT user INTO v_username
   FROM dual;

   -- Insert record into audit table
   INSERT INTO orders_audit
   ( order_id,
     quantity,
     cost_per_item,
     total_cost,
     username )
   VALUES
   ( :new.order_id,
     :new.quantity,
     :new.cost_per_item,
     :new.total_cost,
     v_username );

END;

#1


0  

Why do you need to store the same data twice? I was always taught not to store the same information in multiple tables. If you need a favorites table why not just store:

为什么需要存储两次相同的数据?我总是被告知不要在多个表中存储相同的信息。如果你需要一个收藏夹表,为什么不只是存储:

| FavouritesID | CompanyID | CustomerID |

Then if you need other information from the profile table find it with company ID from the favorites table

然后,如果您需要配置文件表中的其他信息,请从收藏夹表中找到公司ID

#2


0  

I think U can use AFTER INSERT .............

我想你可以使用AFTER INSERT .............

CREATE OR REPLACE TRIGGER orders_after_insert
AFTER INSERT
   ON orders
   FOR EACH ROW

DECLARE
   v_username varchar2(10);

BEGIN

   -- Find username of person performing the INSERT into the table
   SELECT user INTO v_username
   FROM dual;

   -- Insert record into audit table
   INSERT INTO orders_audit
   ( order_id,
     quantity,
     cost_per_item,
     total_cost,
     username )
   VALUES
   ( :new.order_id,
     :new.quantity,
     :new.cost_per_item,
     :new.total_cost,
     v_username );

END;