[英]Understanding how a Primary Key of one Table can be a Foreign Key too
I've been given the code below: 我得到下面的代码:
CREATE TABLE Customers
(
cust_id char(10) NOT NULL ,
cust_name char(50) NOT NULL ,
cust_address char(50) NULL ,
cust_city char(50) NULL ,
cust_state char(5) NULL ,
cust_zip char(10) NULL ,
cust_country char(50) NULL ,
cust_contact char(50) NULL ,
cust_email char(255) NULL
);
CREATE TABLE OrderItems
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL ,
item_price decimal(8,2) NOT NULL
);
CREATE TABLE Orders
(
order_num int NOT NULL ,
order_date datetime NOT NULL ,
cust_id char(10) NOT NULL
);
CREATE TABLE Products
(
prod_id char(10) NOT NULL ,
vend_id char(10) NOT NULL ,
prod_name char(255) NOT NULL ,
prod_price decimal(8,2) NOT NULL ,
prod_desc varchar(1000) NULL
);
CREATE TABLE Vendors
(
vend_id char(10) NOT NULL ,
vend_name char(50) NOT NULL ,
vend_address char(50) NULL ,
vend_city char(50) NULL ,
vend_state char(5) NULL ,
vend_zip char(10) NULL ,
vend_country char(50) NULL
);
ALTER TABLE Customers WITH NOCHECK ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (cust_id);
ALTER TABLE OrderItems WITH NOCHECK ADD CONSTRAINT PK_OrderItems PRIMARY KEY CLUSTERED (order_num, order_item);
ALTER TABLE Orders WITH NOCHECK ADD CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED (order_num);
ALTER TABLE Products WITH NOCHECK ADD CONSTRAINT PK_Products PRIMARY KEY CLUSTERED (prod_id);
ALTER TABLE Vendors WITH NOCHECK ADD CONSTRAINT PK_Vendors PRIMARY KEY CLUSTERED (vend_id);
ALTER TABLE OrderItems ADD
CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (order_num) REFERENCES Orders (order_num),
CONSTRAINT FK_OrderItems_Products FOREIGN KEY (prod_id) REFERENCES Products (prod_id);
ALTER TABLE Orders ADD
CONSTRAINT FK_Orders_Customers FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
ALTER TABLE Products ADD
CONSTRAINT FK_Products_Vendors FOREIGN KEY (vend_id) REFERENCES Vendors (vend_id);
I have a couple of questions: 我有一些问题:
Orders
? 表Orders
中的组合键的目的是什么? order_num
is a Primary Key as well as Foreign Key, I can't understand how it can be Clustered
in one table and Non-Clustered
in another. 由于order_num
既是主键又是外键,所以我不明白如何将其Clustered
在一个表中而将其Non-Clustered
在另一个表中。 WITH NOCHECK
when defining the Primary Keys. 另外,我在定义主键时不了解WITH NOCHECK
的目的。 Thanks 谢谢
What does order_item
mean? order_item
是什么意思? From the look of things, you can remove it from the PK. 从外观上,您可以将其从PK中删除。
An FK basically says "the value here must be one of the values in that other table". FK基本上说“这里的值必须是该其他表中的值之一”。 Think of a dropdown box: the list of available choices is provided that the column in the REFERENCES ... (...)
clause. 考虑一个下拉框: REFERENCES ... (...)
子句中的列提供了可用选项的列表。 CLUSTERED
has no impact on it. CLUSTERED
对它没有影响。 You can make an FK references non-primary key column too. 您也可以使FK引用非主键列。
When you create the primary key, SQL Server checks the table to ensure that the PK column contains no duplicate value. 创建主键时,SQL Server会检查表以确保PK列不包含重复值。 WITH NOCHECK
disables check on existing data, but will check for any new data coming into the table. WITH NOCHECK
禁用对现有数据的检查,但将检查表中是否有任何新数据。 Its usage is not recommended . 不建议使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.