Sql与同一个表有很多很多关系

时间:2012-09-06 12:22:40

标签: sql

我想创建一个用户表,让我们将其命名为USERS,我希望每个用户都知道 用户将能够指向更多用户。您可以将其视为具有一些朋友的用户,这些朋友也是用户,并且有更多朋友也是用户等等。

我是否需要在此表与自身之间使用多对多关系或使用联结表?

例如,表(没有连接表的表示法)可能如下所示:

| USER_ID(PK) | NAME | AGE | _ID(FK) |

3 个答案:

答案 0 :(得分:3)

应该使用联结表,其中每一行都包含两个用户之间“关系”的详细信息。

|UserID1|UserID2|
|      1|      2|
|      2|      3|

答案 1 :(得分:1)

您需要另一个表n:n,其中保存了usersID,例如,您的每个朋友都会有一个记录,包括yourId和onefriendsId

答案 2 :(得分:0)

正如您已经提到的,您有一个名为'User'的对象,另一个名为'Person'。这两者有一个工厂关系,这意味着“每个'人'肯定是'用户'”,但“每个'用户'可能不是'人'',也意味着”每个'用户'也是'人'可以通过使用'PersonOrder'作为联结表来关联'Person',另一个约束是作为联结表“每个子'Person'必须是唯一的。”

我知道这是你真正需要实现的,但它是一个规范化的结构。如果我是你,我会以这种方式实施。

图表链接:http://sdrv.ms/ROVtJc

干杯

USE [temp]
GO
/****** Object:  Table [dbo].[User]    Script Date: 09/06/2012 17:11:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Age] [tinyint] NOT NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Person]    Script Date: 09/06/2012 17:11:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Person](
    [Id] [bigint] NOT NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PersonOrder]    Script Date: 09/06/2012 17:11:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PersonOrder](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Person_Parent_Id] [bigint] NOT NULL,
    [Person_Child_Id] [bigint] NOT NULL,
 CONSTRAINT [PK_PersonOrder] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_FriendOrder_Child_Unique] UNIQUE NONCLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  ForeignKey [FK_Person_User]    Script Date: 09/06/2012 17:11:23 ******/
ALTER TABLE [dbo].[Person]  WITH CHECK ADD  CONSTRAINT [FK_Person_User] FOREIGN KEY([Id])
REFERENCES [dbo].[User] ([Id])
GO
ALTER TABLE [dbo].[Person] CHECK CONSTRAINT [FK_Person_User]
GO
/****** Object:  ForeignKey [FK_PersonOrder_Person_Child]    Script Date: 09/06/2012 17:11:23 ******/
ALTER TABLE [dbo].[PersonOrder]  WITH CHECK ADD  CONSTRAINT [FK_PersonOrder_Person_Child] FOREIGN KEY([Person_Parent_Id])
REFERENCES [dbo].[Person] ([Id])
GO
ALTER TABLE [dbo].[PersonOrder] CHECK CONSTRAINT [FK_PersonOrder_Person_Child]
GO
/****** Object:  ForeignKey [FK_PersonOrder_Person_Parent]    Script Date: 09/06/2012 17:11:23 ******/
ALTER TABLE [dbo].[PersonOrder]  WITH CHECK ADD  CONSTRAINT [FK_PersonOrder_Person_Parent] FOREIGN KEY([Person_Child_Id])
REFERENCES [dbo].[Person] ([Id])
GO
ALTER TABLE [dbo].[PersonOrder] CHECK CONSTRAINT [FK_PersonOrder_Person_Parent]
GO