1,105,402 Community Members

dont understand f-key

Member Avatar
hwoarang69
Posting Pro
572 posts since Feb 2012
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

let say i have two tables called user and cart

user-table
id    username    password
1     dave        dave
2     dan         dan


cart-table
id    user-id   item_name   
1     2         car
2     2         table
3     1         laptop

now i understand that bother id's will be primary key.
i can also create user-id f-key bc its same as id from user.

but i dont understand the need for f-key. lets say i need to see who brought a car.

cant i just get user-id from cart. than look at id from user table and see the username?
does f-key some how makes it easier?

iam new to f-key

Member Avatar
urtrivedi
Posting Virtuoso
1,900 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 27 [?]
 
0
 

I guess you mean foreign key by f-key.

you table structure shows u already using f-key in your cart-table. Here your user_id column in cart-table is f-key because it refers to id column of user-table.

so I am not able to understand why u are confused?

Member Avatar
GhostWolf
Light Poster
27 posts since Dec 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

cant i just get user-id from cart. than look at id from user table and see the username?

Yep, that's exactly how a foreign key is used! "Foreign key" is just a fancy way of saying it, that's all.

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

You can use a foreign key to prevent inserting a non-existent user-id into your table.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article