需要帮助编写触发器
我有三个表:
订单: orderid、valuedid、valuedesc
客户: customerid, cutomdesc
组: groupid, groupdesc
valueid
- 客户或群组的 ID
valuesdesc
- 必须填写适合插入的 valueid
描述来自客户
或组
取决于用户在客户端中选择的内容(客户或组)。
因此,当客户端发送 Orders
的插入查询时,它包含新订单的 orderid
和 valuedid
。在客户端,我知道用户选择了什么:组或客户。
我需要什么:如果在 Orders
中插入新行,则插入 valuesdesc
中的客户或群组的 valuedid
对应的 valuesdesc
> 栏目。
我有一个想法插入新的订单记录 valuesdesc
,其中包含键 - 假值来选择正确的字典(客户或组),但不幸的是,如何创建该触发器我现在不知道。
I have three tables:
Orders:orderid, valuedid, valuesdesc
Customers:customerid, cutomdesc
Groups:groupid, groupdesc
valueid
- id of a customer or a group
valuesdesc
- must filled with appropriate for inserted valueid
description from Customers
or Groups
depend on what (customer or group) user selected in the client.
So, when client send an insert query for Orders
it consists orderid
for new order and valuedid
. And on a client side I know what user selected: group or customer.
What I need: if a new row inserted in Orders
, corresponding valuesdesc
for valuedid
from Customers or Groups inserted in valuesdesc
column.
I have an idea to insert with new order record valuesdesc
which would contain key - fake value to pick the right dictionary (customers or groups), but how to create that trigger I unfortunately don't know for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我必须说这是一个非常不常见的设计。当您将记录插入
Orders
时,您可以使用valuesdesc
来提示valueid
引用的表。但一旦valuesdesc
被触发器填充,valueid
基本上就失去了任何意义。也就是说,您没有告诉我们您有任何其他方法来区分对Customers
的引用和对Groups
的引用。因此,您也可以完全删除valueid
并使用valuesdesc
传递“字典”提示和该表中的引用。除此之外,由于
valueid
可以引用多个表,因此您不能使用 外键对其进行约束。无论如何,在您当前的设计中,您可以尝试以下操作:
字符串
'Customers'
和'Groups'
意味着字典说明符。您可以将它们替换为实际值。触发器使用 LEFT JOIN 连接
Customers
和Groups
,然后使用customdesc
或 < code>groupdesc,取决于哪一个不为空。First, I must say this is a very uncommon design. When you are inserting a record into
Orders
, you usevaluesdesc
to hint at the table thatvalueid
references. But as soon asvaluesdesc
gets filled by the trigger,valueid
essentially loses any meaning. That is, you haven't reveal us you've got any other way to tell a reference toCustomers
from a reference toGroups
. So, you could just as well dropvalueid
altogether and usevaluesdesc
to pass both the 'dictionary' hint and the reference within that table.Other than that, because
valueid
can reference more than one table, you cannot use a foreign key constraint on it.Anyway, in your present design you could try this:
The strings
'Customers'
and'Groups'
are meant as the dictionary specifiers. You replace them with the actual values.The trigger uses LEFT JOIN to join both
Customers
andGroups
, then fillsOrders.valuesdesc
with eithercustomdesc
orgroupdesc
, depending on which one is not null.