更新时间:2022-11-17 13:53:40
当说表共享相同的主键时,只是表示每个表中有一个同名的字段,都设置为主键.
CREATE TABLE [Product (Chapter 2)](
SKU varchar(50) NOT NULL,
Description varchar(50) NULL,
Price numeric(18, 2) NULL,
CONSTRAINT [PK_Product (Chapter 2)] PRIMARY KEY CLUSTERED
(
SKU ASC
)
)
CREATE TABLE [ProductWebInfo (Chapter 2)](
SKU varchar(50) NOT NULL,
ImageURL varchar(50) NULL,
CONSTRAINT [PK_ProductWebInfo (Chapter 2)] PRIMARY KEY CLUSTERED
(
SKU ASC
)
)
ALTER TABLE [ProductWebInfo (Chapter 2)]
ADD CONSTRAINT fk_SKU
FOREIGN KEY(SKU)
REFERENCES [Product (Chapter 2)] (SKU)
如果表名只是单个单词(也不是关键字),则看起来可能会更简单一些,例如,如果表名只是 Product
和 ProductWebInfo
,没有附加 (第 2 章)
:
It may look a bit simpler if the table names are just single words (and not key words, either), for example, if the table names were just Product
and ProductWebInfo
, without the (Chapter 2)
appended:
ALTER TABLE ProductWebInfo
ADD CONSTRAINT fk_SKU
FOREIGN KEY(SKU)
REFERENCES Product(SKU)