且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server +复合键或唯一标识

更新时间:2022-10-18 12:27:00

复合键:



复合键是由多个列组成的,唯一的行在表中。
复合键可以是主键。



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 //无效的数据作为其形式复合主键
$ p>

第二个设计

 使用唯一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 table

CountryID
CountryName

Language table

LanguageID
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