SQL Server +复合键或唯一标识 [英] SQL Server + Composite key or Unique Id
问题描述
我对数据库设计相当新,对于多对多的关系,为
创建复合键和唯一id有什么区别和影响? code>国家表
CountryID
CountryName
语言
表
LanguageID
LangugageName
表 - 使用组合:
CountryID Pkey
pre>
LanguageID Pkey
或
使用唯一ID:
AutoID Pkey
CountryID
LanguageID
解决方案复合键:
复合键是由多个列组成的,唯一的行在表中。
复合键可以是主键。
PRIMARY键
PRIMARY KEY约束唯一标识数据库表中的每个记录。
因此它取决于您的需求
第一个设计
多对多表:
使用composite:
CountryID Pkey
LanguageID Pkey
如果使用此设置,CountryID和LanguageID是复合主key.ie此处
表格数据将为
CountryId LaguageID
1 1 // valid
1 2 // valid
1 3 // valid
1 1 //无效的数据作为其形式复合主键
和第二个设计
使用唯一ID:
AutoID Pkey
CountryID
LanguageID
AutoID成为主键,因此这将允许数据lke thsi
AutoId CountryId LaguageID
1 1 1 // valid
2 1 2 // valid
3 1 3 // valid
4 1 1 // valid as AutoID is primary key
1 2 3 / / invalid as AutoID is prinary key
希望此演示文稿帮助您理解差异
I am fairly new to database design, for many to many relationship, what is the differences and implications of creating a composite key and a unique id for e.g.
Country
tableCountryID CountryName
Language
tableLanguageID LangugageName
Many to Many table - using composite:
CountryID Pkey LanguageID Pkey
OR
Using unique Id:
AutoID Pkey CountryID LanguageID
解决方案Composite Key :
A composite key is a combination of more than one column to identify a unique row in a table. composite key can be a primary key .
PRIMARY KEY
The PRIMARY KEY constraint uniquely identifies each record in a database table.
so its all depend on your requirement
in first design
Many to Many Table: Using composite: CountryID Pkey LanguageID Pkey
if you use this desing than CountryID and LanguageID is composite primary key.i.e here
data of the table will be
CountryId LaguageID 1 1 //valid 1 2 //valid 1 3 //valid 1 1//not valid data as its form composite primary key
and in second design
Using Unique Id: AutoID Pkey CountryID LanguageID
AutoID is become primary key so this will allow data lke thsi
AutoId CountryId LaguageID 1 1 1 //valid 2 1 2 //valid 3 1 3 //valid 4 1 1 //valid as AutoID is primary key 1 2 3 // invalid as AutoID is prinary key
hope this presentation help you to understand difference
这篇关于SQL Server +复合键或唯一标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!