简体   繁体   English

了解一个表的主键如何也可以成为外键

[英]Understanding how a Primary Key of one Table can be a Foreign Key too

I've been given the code below: 我得到下面的代码:


-- Create Customers table -创建客户表

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 OrderItems table -创建OrderItems表

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 Orders table -创建订单表

CREATE TABLE Orders
(
order_num  int      NOT NULL ,
order_date datetime NOT NULL ,
cust_id    char(10) NOT NULL 
);

-- Create Products table -创建产品表

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 Vendors table -创建供应商表

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 
);

-- Define primary keys -定义主键

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);

-- Define foreign keys -定义外键

 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: 我有一些问题:

  1. What's the purpose of the Composite Key in table Orders ? Orders中的组合键的目的是什么?
  2. Since 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在另一个表中。
  3. Also, I don't understand the purpose of WITH NOCHECK when defining the Primary Keys. 另外,我在定义主键时不了解WITH NOCHECK的目的。

Thanks 谢谢

  1. What does order_item mean? order_item是什么意思? From the look of things, you can remove it from the PK. 从外观上,您可以将其从PK中删除。

  2. 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引用非主键列。

  3. 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.

相关问题 如何使一张表的主键,同表的外键 - How to make primary key of one table, the foreign key of same table 如何将一个表中的多个外键引用到 ms sql 中的单个主键 - How can I reference multiple foreign key in one table to a single primary key in ms sql 没有主键,只有一个外键的表,外键可以重复吗? - A table that does not have a primary key and has only one foreign key, can the foreign key be duplicated? 了解为什么外键在同一个表中引用主键? - Understanding why Foreign key references to Primary key in the same table? 一个表中的外键可以是第二个表的复合主键的非唯一键属性吗? - Can a foreign key in one table be non unique key attribute of the composite primary key of the second table? 如何主键只能是一个表中的一个 - How Primary key can be only one in a table 外键SQL:如何将一个表中的两个属性引用到在另一个表中包含3列的复合主键? - Foreign Key SQL: How can I reference two attributes in one table to a composite primary key that contains 3 columns in the other table? 如何创建一个包含五个外键的表,其中一个作为主键(也是一个VARCHAR) - How can I create a table with five foreign keys and one of them as primary key (which is also a VARCHAR) 更新一个表中的主键,这是另一表中的外键 - Update primary key in one table which is foreign key in another table 具有多个主键的表的外键 - foreign key to a table having more than one primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM