[英]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
我想使用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. 因此,当将一行插入到“收藏夹”表(例如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.