1

我想创建一个用户表,让我们为其命名USERS,我希望每个用户都能够指向更多用户。你可以把它想象成一个用户有一些朋友也是用户,还有更多的朋友也是用户等等。

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

例如,表(没有连接表的符号)可能看起来像:

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

4

3 回答 3

4

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

|UserID1|UserID2|
|      1|      2|
|      2|      3|
于 2012-09-06T12:25:57.303 回答
1

您需要保存用户 ID 的另一个表 n:n,例如,您的每个朋友都会有一条记录,包括 yourId 和 onefriendsId

于 2012-09-06T12:32:40.450 回答
0

正如您已经提到的,您有一个名为“User”的对象和另一个名为“Person”的对象。这两者有一个工厂关系,这意味着“每个'Person'肯定是'User'”但“每个'User'可能不是'Person'”,也意味着“每个'User'也是'Person'可以通过使用 'PersonOrder' 作为联结表来关联 'Person's”,另一个约束是作为联结表“每个子 '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
于 2012-09-06T12:42:04.430 回答