简体   繁体   English

我的主键和外键是什么?

[英]What will be my Primary and Foreign Keys?

I am newbie to Database: what will be my Primary and Foreign keys for the following database: user buys items and rates them by giving ratings(1,2,3,4,5) 我是数据库的新手:以下数据库的主键和外键是什么:用户购买商品并通过给定评级(1,2,3,4,5)对其进行评级
user(id, name) 用户(ID,名称)
item(id, description, category, price) buys(user, item, price) rates(user, item, rating) 项目(id,描述,类别,价格)购买(用户,项目,价格)费率(用户,项目,等级)

Primary key 首要的关键

  • id in User table 用户表中的ID
  • id in item table 项目表中的ID

Foreign key 外键

  • user_id (referring User table PK) and item_id (referring item table PK) in buys table 购买表中的user_id(参考用户表PK)和item_id(参考项目表PK)

  • user_id (referring User table PK) and item_id (referring item table PK) rates table user_id(参考用户表PK)和item_id(参考项目表PK)费率表

In the given situation acc. 在给定的情况下。 to me this is what you have to take following columns as: 对我来说,这是您必须采取的以下专栏:

Primary Key: 首要的关键:

Id in User table
Id in Item Table

Foreign Key: 外键:

User in Buys Table Of ID in User Table
Item in Buys Table Of ID in Item Table
User in Rates Table Of ID in User Table
Item in Rates Table Of ID in Item Table

PK = primary key FK = foreign key PK =主键FK =外键

user(PK userid, name)

item(PK itemid, description, category, price) 

buys(FK userid, FK itemid, price) 

rates(FK userid, FK itemid, rating)

buys needs a price only if the price can change. buys只有在价格可以改变时才需要价格。

The user and item tables need a primary key so they can be referenced by the buys and rates tables. 用户和项目表需要一个主键,以便可以通过购买和费率表进行引用。 I used different names than id so it is clear which columns to join over when doing a select with a join operation. 我使用了与id不同的名称,因此在使用联接操作进行选择时,很明显要联接的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM