开发者

Modeling Group Membership in a Database

开发者 https://www.devze.com 2023-01-13 18:43 出处:网络
Just started designing a database which I have not done before.And am wondering the best way to handle group membership in the database.

Just started designing a database which I have not done before. And am wondering the best way to handle group membership in the database.

Say I have two tables.

CREATE TABLE [dbo].[Computers] (
    [ComputerID]        INT              IDENTITY (1, 1) NOT NULL,
    [Name]              NVARCHAR (50)    NOT NULL,
 );


CREATE TABLE [dbo].[ComputerGroups] (
    [ComputerGroupID] INT            IDENTITY (1, 1) NOT NULL,
    [Name]                NVARCHAR (50)  NOT NULL,
);

Am I better off adding a [MemberOfGroup] field to the Computers table, adding a [Member] field to the ComputerGroups table or creating a 3rd ju开发者_JAVA百科nction table and adding the [ComputerID] [ComputerGroupID] FK's.

I can surly get this working but which which is the best way to accomplish this and why?


If a computer can be a member of multiple groups, you have a Many-to-Many relationship. In normal SQL you need a junction-table to achieve this.

create table GroupMembership (
  ComputerID int not null,
  GroupID int not null,
  --here you can add info that concearns the Membership, like expiry date and such--
)

If a computer can only be a member of one group, you only need to add the groupid as a field in the Computer table.

0

精彩评论

暂无评论...
验证码 换一张
取 消