更改表语法分配

时间:2011-11-19 23:24:28

标签: sql sql-server sql-server-2008

我有分配到期,因为我坚持了一个问题。

将“销售明细”表添加到数据库中。此表与Orders和Products表相关。它至少显示了订购的产品和数量(如果您愿意,可以添加其他字段,但解释您在纸上添加它们的原因)。

在提供的图表中没有对此表的描述。在这里使用您最好的数据库设计技巧!

Create Table SalesDetail
(
   SaleDetailID int,
   ProductID char(5),
   ManufactureID char(3) not null,
   OrderNo int,
   qtyOrdered int
   PRIMARY 
)

Alter Table SalesDetail
Add FOREIGN KEY (ProductID)
REFERENCES Products(ProductID)

我的错误是我无法将SalesDetail表链接到Products表。

Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'Products' that match the referencing column list in the foreign key 'FK__SalesDeta__Produ__5EBF139D'.

Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.


Create Table Customers
(
CustomerNo char(4) 
Constraint ck_CustomerNoHas4positionsWithNumbers
Check(CustomerNo like'[0-9],[0-9],[0-9],[0-9]'),
Company varchar(50) not null,
CustomerRep char(3),
CreditLimt money default(20000.00),
PRIMARY KEY(CustomerNo)
)

Create Table Salesreps
(
EmployeeNo char(3)
Constraint ck_EmployeeNoHasDigits check(EmployeeNo like'[0-9],[0-9],[0-9]'),
FirstName varchar(25) not null,
LastName varchar(25) not null,
Age int,
SalesRepOffice char(2) not null,
Title varchar(50),
HireDate Date not null,
Manager char(3) not null,
Quota money,
Sales money not null,
PRIMARY KEY(EmployeeNo)
)

Create Table Offices
(
Office char(2) Constraint ck_checkOfficeHasNumbersOnly check(Office like'[0-9],[0-9]'),
City varchar(25) not null,
Region varchar(10) not null,
Manager char(3) not null,
Target money,
Sales money not null
PRIMARY KEY(Office)
)

Create Table Orders
(
OrderNo int,
OrderDate Date not null,
CustomerNo char(4) not null,
SalesRep char(3) not null
PRIMARY KEY(OrderNo)
)

Create Table Products
(
ManufactureID char(3) 
Constraint ck_ManufactureIDifItHasLettersOnly check(ManufactureID like'[a-z],[a-z],[a-z]'),
ProductID char(5) 
Constraint ck_ProductIDhasTwoLettersAndThreeNumbers check(ProductID like'[0-9],[0-9],[a-z],[a-z],[a-z]'),
Description varchar(50) not null,
Price money not null,
QtyOnHand int not null,
PRIMARY KEY(ManufactureID, ProductID)
)

- 将外键添加到需要它们的所有表

Alter Table Customers
Add constraint fk_customerrep
FOREIGN KEY (CustomerRep) 
REFERENCES Salesreps(EmployeeNo)

Alter Table Salesreps
Add constraint fk_salesrepoffice
FOREIGN KEY (SalesRepOffice) 
REFERENCES Offices(Office),
constraint fk_manager
FOREIGN KEY (Manager)
REFERENCES Salesreps(EmployeeNo)

Alter Table Offices
Add constraint fk_officesmanger
FOREIGN KEY (Manager)
REFERENCES Salesreps(EmployeeNo)

Alter Table Orders
Add constraint fk_customerno
FOREIGN KEY (CustomerNo)
REFERENCES Customers(CustomerNo),
constraint fk_salesrep
FOREIGN KEY (SalesRep)
REFERENCES Salesreps(EmployeeNo)

3 个答案:

答案 0 :(得分:2)

Products有一个复合键(ManufactureID, ProductID),因此您不能仅通过ProductId唯一地标识产品。因此,您必须创建一个引用ManufactureIdProductID

的复合外键
Alter Table SalesDetail
Add FOREIGN KEY (ManufactureId, ProductID)
REFERENCES Products(ManufactureID, ProductID)

答案 1 :(得分:1)

ProductID不是错误所说的主键。在你的代码中

PRIMARY KEY(ManufactureID, ProductID)

这会创建一个主键,这两个列组合在一起。

答案 2 :(得分:1)

Products的主键是(ManufactureID, ProductID)。因此SalesDetail表应包含这两列,并且两者都应该是外键约束的一部分:

Alter Table SalesDetail
Add FOREIGN KEY (ManufactureID, ProductID)
REFERENCES Products(ManufactureID, ProductID)