SQL多个外键作为主键

时间:2021-11-12 01:57:42

If I declare the table below does it implicitly imply that both the foreign keys make a unique primary key or do I need to do something more to make both attributes as a primary key?

如果我声明下面的表是否隐含地暗示外键都是唯一的主键,还是我需要做更多的事情才能将这两个属性作为主键?

CREATE TABLE Report_has_Items
(
    ReportID int REFERENCES Report(ReportID) NOT NULL,
    ItemID int REFERENCES Item(ItemID) NOT NULL
)

Essentially both attributes which are foreign keys from other tables, together would form a unique key.

基本上这两个属性都是来自其他表的外键,它们一起形成一个唯一的键。

3 个解决方案

#1


10  

No it doesn't. The above table has no primary key. If you want to use the fields as a primary key use:

不,不。上表没有主键。如果要将字段用作主键,请使用:

CREATE TABLE Report_has_Items(
    ReportID int REFERENCES Report(ReportID) NOT NULL,
    ItemID int REFERENCES Item(ItemID) NOT NULL,
    PRIMARY KEY (ReportID, ItemID)
)

or something similar depending on your sql dilect.

或类似的东西取决于你的sql dilect。

#2


6  

Let's name our constraints, eh?

让我们说出我们的约束,呃?

CREATE TABLE dbo.Report_has_Items(
    ReportID int NOT NULL,
       CONSTRAINT [FK_RHI_Report] (ReportId) REFERENCES dbo.Report(ReportID),
    ItemID int NOT NULL,
       Constraint [FK_RHI_Item] (ItemId) REFERENCES dbo.Item(ItemID),
    CONSTRAINT [PK_RHI] PRIMARY KEY (ReportID, ItemID)
)

#3


3  

I am not sure i understand your question completely but i assume you are trying to create a composite primary key (primary key with more than one attribute). You could do the following.

我不确定我完全理解你的问题,但我假设你正在尝试创建一个复合主键(具有多个属性的主键)。您可以执行以下操作。

CREATE TABLE Report_has_Items(
  ReportID int references Report(ReportID),
  ItemID int references Item(ItemID),
  PRIMARY KEY (ReportID , ItemID )
);

Note:The pair (ReportID , ItemID ) must then be unique for the table and neither value can be NULL.

注意:该对(ReportID,ItemID)必须对于表是唯一的,并且这两个值都不能为NULL。

Here is a very useful link for SQL Queries

这是SQL查询的一个非常有用的链接

#1


10  

No it doesn't. The above table has no primary key. If you want to use the fields as a primary key use:

不,不。上表没有主键。如果要将字段用作主键,请使用:

CREATE TABLE Report_has_Items(
    ReportID int REFERENCES Report(ReportID) NOT NULL,
    ItemID int REFERENCES Item(ItemID) NOT NULL,
    PRIMARY KEY (ReportID, ItemID)
)

or something similar depending on your sql dilect.

或类似的东西取决于你的sql dilect。

#2


6  

Let's name our constraints, eh?

让我们说出我们的约束,呃?

CREATE TABLE dbo.Report_has_Items(
    ReportID int NOT NULL,
       CONSTRAINT [FK_RHI_Report] (ReportId) REFERENCES dbo.Report(ReportID),
    ItemID int NOT NULL,
       Constraint [FK_RHI_Item] (ItemId) REFERENCES dbo.Item(ItemID),
    CONSTRAINT [PK_RHI] PRIMARY KEY (ReportID, ItemID)
)

#3


3  

I am not sure i understand your question completely but i assume you are trying to create a composite primary key (primary key with more than one attribute). You could do the following.

我不确定我完全理解你的问题,但我假设你正在尝试创建一个复合主键(具有多个属性的主键)。您可以执行以下操作。

CREATE TABLE Report_has_Items(
  ReportID int references Report(ReportID),
  ItemID int references Item(ItemID),
  PRIMARY KEY (ReportID , ItemID )
);

Note:The pair (ReportID , ItemID ) must then be unique for the table and neither value can be NULL.

注意:该对(ReportID,ItemID)必须对于表是唯一的,并且这两个值都不能为NULL。

Here is a very useful link for SQL Queries

这是SQL查询的一个非常有用的链接