1.11M Members

dont understand f-key

 
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

 
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?

 
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.

 
0
 

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

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

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