如何将UserID和ProductID列作为下载表中的外键?

时间:2015-11-17 04:14:30

标签: mysql sql-server

这是我到目前为止所做的!!

创建数据库 MyWebDB ;

创建表用户

( UserID Int Primary Key,
  EmailAddress Varchar(50) Not Null, 
  FirstName Varchar(50) Not Null, 
  LastName Varchar(50) Not Null)

创建表下载

( DownloadID int Primary Key, 
  DownloadDate SmallDatetime Null, 
  FileNames Varchar(100) Not Null, 
  Foreign Key (UserID) References Users(UserID),
  Foreign Key (ProductID) References Products(ProductID))

创建表产品

(ProductID int primary key, ProductName Varchar(50))

我的Sql查询

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须为下载表UserID和ProductID

设置数据类型
 Userid int FOREIGN KEY REFERENCES Users(UserID)
 Productid int FOREIGN KEY REFERENCES Products(ProductID)

Userid int,
Productid int,
FOREIGN KEY (Userid) REFERENCES Users(UserID),
FOREIGN KEY (Productid) REFERENCES Products(ProductID))

喜欢:

创建表下载

( DownloadID int Primary Key, 
  DownloadDate SmallDatetime Null, 
  FileNames Varchar(100) Not Null, 
  Userid int,
  Productid int,
  FOREIGN KEY (Userid) REFERENCES Users(UserID),
  FOREIGN KEY (Productid) REFERENCES Products(ProductID))