TSQL - 创建表设计
我有一个 -
Table X
------------------------------------
TaskId (bigint-identity-primary key)
Data1 (varchar), Data2(varchar)
现在,我正在创建一个新表 -
Table Y
------------------------------------
Id (identity, Primary Key)
TaskId(FK to Table X)
Data3(varchar)
现在,由于表 X 和表 Y 将具有基于 TaskId 列的一对一关系,因此我应该定义什么以及如何 表 Y 上的主键、索引等?
另外,如果永远不会在 where 子句中使用标识列作为主键,是否值得使用它 ?我刚刚按照惯例添加了它。
I have a -
Table X
------------------------------------
TaskId (bigint-identity-primary key)
Data1 (varchar), Data2(varchar)
Now, I am creating a new Table -
Table Y
------------------------------------
Id (identity, Primary Key)
TaskId(FK to Table X)
Data3(varchar)
Now, since Table X and Table Y will have a one-one relationship based on the TaskId column, so what and how should do I define the Primary Key, Index, etc on Table Y ?
Also, is it worth having an identity column at all as Primary Key if it will never be used in where clause
? I have just added it by convention.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于这是
1:1
关系,并且每个Y
都是一个Task
你可以使用这种方法:我想如果只有少数几个,这很有用任务是 Y 任务,而不是全部。而且您不希望表中出现 NULL。
Since this is
1:1
relationship, and everyY
is aTask
you can use this approach:I guess this is useful if only a few Tasks are Y-Tasks and not all of them. And you prefer not to have
NULL
s in your tables.如果是一对一的关系,为什么要创建一个全新的表
Y
?只需将新的Data3
列添加到表X
中即可。If it's a one-one relationship, why create a whole new table
Y
? Just add the newData3
column to TableX
.假设您实际上具有 1:Many 关系...
将 PK 和聚集索引键保留在
ID
字段上。在
TaskID, Data3
上创建非聚集索引,或者,如果您从不筛选Data3
而仅SELECT
它,则只需INCLUDE (数据3)
。Assuming you actually have a 1:Many relationship...
Keep your PK and Clustered index key on the
ID
field.Create a non-clustered index on
TaskID, Data3
or, if you never filter onData3
and onlySELECT
it, justINCLUDE (Data3)
.如果必须将其分开并且确定始终保持1-1,则使用taskID作为PK。如果它可能是 1-many inteh future,请添加一个身份并在 taskid 上放置一个唯一索引。那么如果你选择 1-many,你所要做的就是删除唯一索引。
If you must separate it and you are sure that it will always remain 1-1, then use taskID as the PK. If it could be 1-many inteh future, add an identity and put a unique index on taskid. Then if you go 1-many all you have to do is drop the unique index.