如何在sql表中使用3个字段作为主键? [英] how to use 3 fields as primary key in sql tables?

查看:875
本文介绍了如何在sql表中使用3个字段作为主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我的数据库在sql server2008中
我必须使用3个字段作为主键。

如何做到这一点?

我需要一些关于sqlServer2008关系的帮助。



谢谢所有

解决方案

如果你分配主键在多个列上它将是复合主键

  CREATE   TABLE 曲目(
专辑 CHAR 10 ),
disk INTEGER,
posn INTEGER,
song VARCHAR 255 ),
PRIMARY KEY (专辑, disk ,posn)





将有独特之处专辑,磁盘,posn中的记录在一起,不能一起为空。

如前所述,如果这是您的问题,您可以拥有一个由多列组成的主键。



如果问题是如何单独制作列主键,这是不可能的。但是,您可以创建1个主键并添加两个唯一键。更多信息,请参阅: UNIQUE Con​​straints [ ^ ]。



关于关系,你使用 FOREIGN KEY [ ^ ]


无法添加三个表中的主键。只允许一个主键


Hi All
in my database in sql server2008 i have to use 3 fields as primary key.
how to do that?
and i need some help about relationships in sqlServer2008.

thanks all

解决方案

If you assign primary key on more than one column it will be composite primary key

CREATE TABLE track(
  album CHAR(10),
  disk INTEGER,
  posn INTEGER,
  song VARCHAR(255),
  PRIMARY KEY (album, disk, posn)
)



there will be unique records in album, disk, posn together and can not be null together.


As already stated you can have a single primary key which consists of multiple columns if this was your question.

If the question was how to make the columns primary keys separately, that's not possible. However, you can create 1 primary key and add two unique keys. More info, see: UNIQUE Constraints[^].

About the relationships, for them you use FOREIGN KEY[^]


it is not possible to add three primary keys in a table. only one primary key is allowed


这篇关于如何在sql表中使用3个字段作为主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆