1

我是这里的新手

我创建了一个具有主键 customer_id 的表,以及另一个具有外键 customer_id 的表,以将其加入第一个表

我的问题

当我想在两个表中输入数据时,我应该插入 customer_id 两次(一个在第一个表中,另一个在第二个表中)。

每次插入数据时我都应该这样做吗?谢谢 :)

4

1 回答 1

2

Your CustomerId table represents each customer in the Customer table. So whenever a new customer arrives, you create an id for that customer.

For other tables that "relate" to the customer, you insert a customer_id for each entry.

E.g.

Customer
CustomerId, CustomerName

Each customer has a unique id..

ProductSold
ProductId, ProductName, CustomerId

You can now tell which customer bought a product because of the foreign key in the Product table. So for each product, you insert the customer's id that bought it. I hope that makes sense.

-- A new customer, requires a new id (when you insert a new customer) -- A product bought by customer, requires a foreign CustomerId to identify its buyer.

So 2 CustomerId inserts.

So yes.. you are right lol :P

于 2013-07-13T04:53:36.757 回答