SQL 表关系和索引

SQL tables relationship and indexes

我想要 2 tables SalesOrderHeader 和 SalesOrderLine 具有一对多关系并且还想建立索引。

SalesOrderHeader table:

 SalesOrderNumber 
------------------
|                |
| 1              |
|                |
|----------------|
|                |
|                |
| 2              |
|                |
|                |
------------------

销售订单行table:

 SalesOrderNumber Line
-------------------------
|                | 1    |
| 1              |------|
|                | 2    |
|----------------|------|
|                | 1    |
|                |------|
| 2              | 2    |
|                |------|
|                | 3    |
-------------------------

请告知如何使用这些 table 的结构方法建立关系和索引?

创建外键

ALTER TABLE SalesOrderNumberLine
ADD FOREIGN KEY (SalesOrderNumberID) REFERENCES SalesOrderNumber (ID)

然后在该列上创建一个非聚集索引

CREATE NONCLUSTERED INDEX IX_SalesOrderNumberLine_SalesOrderNumberID
ON dbo.SalesOrderNumberLine(SalesOrderNumberID);