简体   繁体   English

表的多个主键

[英]Multiple Primary Keys For Table

Hi I'm relatively new to SQL and i am using Microsoft SQL Server 嗨,我相对较新的SQL,我使用的是Microsoft SQL Server

I wish to make a pivot table that links two tables (A and B) together. 我希望制作一个将两个表(A和B)链接在一起的数据透视表。 However the linking is intended to be dynamic. 然而,链接旨在是动态的。 Which means that Links from A to B will be created and removed frequently. 这意味着将经常创建和删除从A到B的链接。

The relationship between A and B is one to many (one A can have many B's) A和B之间的关系是一对多(一个A可以有很多B)

I want to use a clustered primary key but seem to be unable to make one because the type of the link to A is varchar(max) and the link to B is int. 我想使用集群主键,但似乎无法创建一个,因为A链接的类型是varchar(max),而B的链接是int。

In Microsoft SQL Server Management Studio i would normally select both columns in the design window and make them both primary keys. 在Microsoft SQL Server Management Studio中,我通常会在设计窗口中选择这两列,并使它们成为主键。 However The option is grayed out. 但是该选项显示为灰色。

Is there are way to make many different columns of different types a clustered primary key or am i approaching this problem the wrong way? 有没有办法让不同类型的许多不同列成为集群主键,或者我是以错误的方式处理这个问题?

No, Not possible. 不,不可能。 PRIMARY means FIRST, MAIN - there can only be one. PRIMARY意味着第一,主要 - 只能有一个。

You can have an unlimited number (practically) of unique indixes, but only one can ever be the primary key and only one (not necessarily the same one) can be the clustered index. 您可以拥有无​​限数量(实际上)的唯一indix,但只有一个可以是主键,只有一个(不一定是同一个)可以是聚簇索引。

Now, towards using a varchar(max) in a foreign key relationship - that is the core issue you should fix. 现在,在外键关系中使用varchar(max) - 这是你应该修复的核心问题。 SOmeone really f* * * that database design up epically. SOmeone真的f * * *数据库设计史诗。 We can talk about using varchar in general in a key, but varchar(max) is meant for hugh text storage and can not be part of any index (outside a full text index that can not be used in relationships). 我们可以讨论在密钥中一般使用varchar,但varchar(max)用于休眠文本存储,不能是任何索引的一部分(在关系中不能使用的全文索引之外)。 I would suggest fixing that first. 我建议先修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM