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

Recommended Answers

All 3 Replies

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?

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.