views:

20

answers:

1

Hello, 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 junction 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?

A: 

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.

Christian W