SQL-插入后触发 [英] SQL - TRIGGER AFTER INSERT
问题描述
我有两个表-' clients '和' clients_address '
I have two tables - 'clients' and 'clients_address'
在表'客户列,名称为 电话。
我需要在表' clients_address '
There is on table 'clients' column with name 'phone'. I need to have the same column 'phone' in table 'clients_address'
我创建表 clients_address 中的 电话列,并使用此命令从 clients.phone 复制数据:
I create column 'phone' in table 'clients_address' and whit this command I copied the data from 'clients.phone':
UPDATE clients_addresses
SET clients_addresses.phone=(SELECT clients.phone
FROM clients
WHERE clients.id=clients_addresses.client_id);
所有内容均已正确复制,但不是自动更新。在新用户注册后,我需要再次执行此命令。我尝试使用触发器,但是SQL向我返回了sintax错误。这是我的尝试:
Everything is copied correctly, but it is not autoupdate. And after new user registration i need to execute this command again. I try with trigger, but SQL return me sintax error. This is what I try:
CREATE TRIGGER up
ON clients.phone
AFTER INSERT
BEGIN
UPDATE clients_addresses
SET clients_addresses.phone=(SELECT clients.phone
FROM clients
WHERE clients.id=clients_addresses.client_id)
END;
我在SQL中不是很好。请帮忙。
I am not very good in sql. Please help.
推荐答案
尝试以下操作:
CREATE TRIGGER up
AFTER INSERT
ON clients
FOR EACH ROW
BEGIN
UPDATE clients_addresses
SET clients_addresses.phone = :new.phone
WHERE clients_addresses.client_id = :new.id;
END;
您使用的是什么数据库?
What database are you using?
这篇关于SQL-插入后触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!