简体   繁体   English

更改表语法分配

[英]Alter Table syntax Assignment

I have Assignment due in which i'm stuck on a question. 我有分配,因为我坚持一个问题。

Add a “Sales Detail” table to your database. 将“销售明细”表添加到数据库中。 This table is related to the Orders and Products tables. 此表与Orders和Products表相关。 It shows the product and quantity ordered at least (add other fields if you wish but explain why you added them on your paper). 它至少显示了订购的产品和数量(如果您愿意,可以添加其他字段,但解释您在纸上添加它们的原因)。

There is no description of this table on the diagram provided. 在提供的图表中没有对此表的描述。 Use your best database design skills here! 使用您最好的数据库设计技巧!

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)

My Error is I can not get it to link SalesDetail table to Products table. 我的错误是我无法将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)
)

--Add Foreign Keys to all tables who needs them - 将外键添加到需要它们的所有表中

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)

The table Products has a composite key (ManufactureID, ProductID) , so you cannot uniquely identify a product by just the ProductId. Products具有复合键(ManufactureID, ProductID) ,因此您无法仅通过ProductId唯一标识产品。 Therefore you have to create a composite foreign key that references to both ManufactureId and ProductID : 因此,您必须创建一个引用ManufactureIdProductID的复合外键:

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

ProductID is not a primary key like the error says. ProductID不是错误所说的主键。 In your code 在你的代码中

PRIMARY KEY(ManufactureID, ProductID)

This creates a primary key that both of those columns combined. 这会创建一个主键,这两个列组合在一起。

The primary key for Products is (ManufactureID, ProductID) . 主键Products(ManufactureID, ProductID) So the SalesDetail table should contain both these columns, and both should be part of the foreign key constraint: 因此SalesDetail表应包含这两列,并且两者都应该是外键约束的一部分:

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

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

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