简体   繁体   English

如何使用外键链接3个表?

[英]How do I link 3 tables using foreign keys?

I have made a table named "reservations" which contains a customer id and a house id. 我制作了一个名为“预订”的表,其中包含一个客户ID和一个房屋ID。 I made tables for houses and customers as well. 我也为房屋和顾客做了桌子。 I have made a datagrid, which contains the reservations data, but I also want it to contain the customers surname and the house code. 我已经创建了一个数据网格,其中包含预订数据,但我也希望它包含客户姓氏和房屋代码。

My tables are (in SQL Server Express): 我的表是(在SQL Server Express中):

CREATE TABLE [dbo].[houses] 
(
    [Id]     INT IDENTITY (1, 1) NOT NULL,
    [Code]   VARCHAR(50) NULL,
    [Status] VARCHAR(50) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

CREATE TABLE [dbo].[customers] 
(
    [Id]       INT IDENTITY (1, 1) NOT NULL,
    [Forename] VARCHAR(50) NULL,
    [Surname]  VARCHAR(50) NULL,
    [Email]    VARCHAR(50) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

CREATE TABLE [dbo].[reservations] 
(
    [Id]         INT  IDENTITY (1, 1) NOT NULL,
    [HouseId]    INT  NULL,
    [CustomerId] INT  NULL,
    [StartDate]  DATE NULL,
    [EindDate]   DATE NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC), 
    CONSTRAINT [FK_HouseId] 
        FOREIGN KEY ([HouseId]) REFERENCES [houses]([Id]), 
    CONSTRAINT [FK_CustomerId] 
        FOREIGN KEY ([CustomerId]) REFERENCES [customers]([Id])
);

I already created all the tables, but I don't know how to link them properly. 我已经创建了所有表,但不知道如何正确链接它们。 I want to get the data and put it in a datagrid. 我想获取数据并将其放入数据网格中。

To select all data from Reservations, customers' Surname and house code, you need to run query: 要从预订,客户的姓氏和房屋代码中选择所有数据,您需要运行查询:

Select R.*, C.Surname, H.Code 
From [dbo].[reservations] R
inner join [dbo].[customers]  C on C.Id = R.CustomerId
inner join [dbo].[houses]  H on H.Id = R.HouseId

Try this: 尝试这个:

select r.*,c.surname,h.code from reservation r,customers c,houses h where 
r.customer_id=c.customer_id and r.house_id=h.house_id

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

相关问题 如何使用外键访问/修改数据库中的实体? - How do I access/modify entities in a Database using Foreign Keys? 如何使用外键在MVC中遍历多个类/表? - How can I move through multiple classes/tables in MVC using foreign keys? 如何使用外键在LINQ中插入两个以上的表 - How can I insert more than 2 tables in LINQ using foreign keys 如何使用实体框架连接两个表并匹配外键 - How to join two tables using Entity Framework and match foreign keys 如何指示实体框架选择两个表之间具有多个外键的正确关系? - How do I instruct Entity Framework to select the correct relationship between two tables with multiple foreign keys between them? 使用 dapper 扩展的外键和数据透视表 - Foreign keys and pivot tables using dapper extensions 如何使用Fluent NHibernate和SchemaUpdate.Execute()索引外键? - How do I index foreign keys using Fluent NHibernate and SchemaUpdate.Execute()? 如何将下拉菜单项与一组外键关联? - How do I associate dropdown menu items with a set of foreign keys? 如何更新只有外键的表 - How do I update a table that only has foreign keys 如何在Entity Framework中创建由外键组成的复合主键? - How can I create a composite primary key consisting of foreign keys to two tables in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM