简体   繁体   English

使用触发器将新行从另一表插入到一个表?

[英]Use trigger to INSERT new row to one table from another table?

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 我想使用CompanyIDCompanyNameProfilePhoto插入收藏夹表中

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. 因此,当将一行插入到“收藏夹”表(例如CompanyID = 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 这是行不通的

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的信息

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;

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

相关问题 创建触发器以在创建新行时将主键从一个表插入到 MySQL 中另一个表的列中 - Create trigger to insert primary key from one table upon creation of new row, into column of another table in MySQL MySQL:触发在另一个表中插入一行时在一个表中插入新行 - MySQL: Trigger to Insert New Row in One Table When Another Table has a Row Inserted MySQL触发器在更新后将新行插入另一个表 - Mysql trigger to insert a new row into another table after an update 从一个链接表插入行到另一个 - Insert row from one linked table to another 在一个表的插入上创建触发器,根据另一个(第3个)表上的行数在另一个表上创建新行 - Creating a trigger on insert on a table, that creates new rows on another table, according to amount of row on another (3rd) table SQL Server触发将新行中的值插入到具有多对多关系的另一个表中 - SQL Server trigger insert values from new row into another table with many-to-many relationship 使用来自另一个表 mysql 的值将新行插入到表中 - Insert new row into a table with values from another table mysql 将表中的许多行插入另一表中的唯一行 - Insert many rows from a table into one unique row in another table 触发删除时将行插入到另一个表中 - Trigger to insert a row into another table on delete 查找并将行插入到另一个表中的mysql触发器 - find and insert row to another table mysql trigger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM