我有两个桌子:
create table customer
(
cust_id
cust_name
)
create table customerphone
(
customerphone_phone,
cust_id,
)
两个表中都必须存在cust_id,如何检查?
最佳答案
仅凭声明性参照完整性(DRI)不能做到这一点。添加外键约束只是解决方案的一部分。您还需要围绕2个表的插入内容包装事务和业务逻辑。我建议在存储过程中执行此操作,以便从应用程序的角度来看它可以作为原子操作进行操作。
Begin Transaction
Logic around inserting a Customer
Logic around inserting CustomerPhone row
If the newly added Customer has a CustomerPhone
Commit Transaction
Else
Rollback Transaction