TSQL - 创建表设计

发布于 2024-12-04 04:40:32 字数 483 浏览 2 评论 0原文

我有一个 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-黛色若梦 2024-12-11 04:40:33

由于这是 1:1 关系,并且每个 Y 都是一个 Task 你可以使用这种方法:

Table Y 
----------
TaskId (bigint),
Data3 (varchar),
PRIMARY KEY TaskId, 
FOREIGN KEY TaskId REFERENCES Task(TaskId)

我想如果只有少数几个,这很有用任务是 Y 任务,而不是全部。而且您不希望表中出现 NULL。

Since this is 1:1 relationship, and every Y is a Task you can use this approach:

Table Y 
----------
TaskId (bigint),
Data3 (varchar),
PRIMARY KEY TaskId, 
FOREIGN KEY TaskId REFERENCES Task(TaskId)

I guess this is useful if only a few Tasks are Y-Tasks and not all of them. And you prefer not to have NULLs in your tables.

人事已非 2024-12-11 04:40:33

如果是一对一的关系,为什么要创建一个全新的表Y?只需将新的 Data3 列添加到表 X 中即可。

If it's a one-one relationship, why create a whole new table Y? Just add the new Data3 column to Table X.

比忠 2024-12-11 04:40:33

假设您实际上具有 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 on Data3 and only SELECT it, just INCLUDE (Data3).

骄傲 2024-12-11 04:40:33

如果必须将其分开并且确定始终保持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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文