Hussein Emam
Hussein Emam

Reputation: 9

Primary key VS Foreign key

Im a newbie here

I created one table with primary key customer_id , and another table with a foreign key customer_id to join it to the first table

my question

when I want to enter data in the two tables , should I insert the customer_id twice ( one in the first table and the other in the second ) .

should I do that in every time I insert data ?? thanks :)

Upvotes: 1

Views: 1280

Answers (1)

Lews Therin
Lews Therin

Reputation: 10995

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

Upvotes: 2

Related Questions