值必须在两个表
答
这不能与声明引用完整性(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
答
使cust_id
成为表customer
上的主键。
然后在表customerphone
上制作cust_id
一个外键,该表上的指向customer
。
这时如果cust_id
在customer
已经存在,你只能行添加到customerphone
。
答
您可以使用CUST_ID从客户作为主键,使用外键限制在customerphone CUST_ID。
您可以使用本手册指导自己: https://technet.microsoft.com/en-us/library/ms175464(v=sql.105).aspx
您是否考虑过外键? – HoneyBadger
不,我不知道如何解释,但每个客户都必须有一个客户电话 – Yakalent
如何将客户电话放在客户桌上而不是自己的桌上?或者客户可以有多个电话号码?由于无法同时插入两个表(尽管存在交易),因此无法确保记录必须存在于两个表中。 –